Accelerate web applications with Varnish Cache

Horsepower

Caching Time-to-Live

Caching content and delivering it later to identical requests is Varnish's bread and butter. If you want the reverse proxy to serve a web page persistently for another five minutes after the first request, the simple VCL code snippet

sub vcl_backend_response {
  set beresp.ttl = 5m;
}

will accomplish this time-to-live (TTL) configuration.

No Cache for Logged-In Users

If a website displays customized content for a logged-in user, Varnish is not allowed to cache this page. In these cases, it may react to certain cookies set by the web application. In the example in Listing 2, the cookie is logged_in and the associated domain is example.com.

Listing 2

Cookie for Registered Users

01 sub vcl_recv
02 {
03 if ((req.http.host ~ "example.com" &&
04      req.http.Cookie == "logged_in")
05 {
06      return (pass);
07 }
08
09 unset req.http.Cookie;
10 }

Availability Without a Back  End

If the back end becomes very slow or fails completely (e.g., because of a software update or a denial-of-service attack), Varnish still needs to deliver the previously valid static website.

In the configuration shown in Listing 3, Varnish checks every three seconds to see whether the back end, 127.0.0.1:8080 , is still alive and whether the response from the back end takes less than 50ms. If this is not the case, Varnish will continue to deliver the old cache for a maximum of 12 hours.

Listing 3

Heart Rate Monitor

01 backend default {
02 .host = '127.0.0.1';
03 .port = '8080';
04 .probe = {
05        .url = "/";
06        .timeout = 50ms;
07        .interval = 3s;
08        .window = 10;
09        .threshold = 8;
10        }
11 }
12
13 set beresp.grace = 12h;

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus