Varnish is a very popular HTTP accelerator. One of its key features is caching.
When dealing with caching, it is always tricky to do correct Time To Live estimations. Especially when the trade-off is caching efficiently versus serving fresh content. To tackle that issue, you can invalidate cache entries via purging.
How Varnish does it
In the Varnish API there is a function called purge_url. It accepts a URL pattern as an argument and purges it. On the next hit the purged item gets replaced with the corresponding fresh content.
There are 2 basic ways of invoking the purge_url function:
- On the command line
- In VCL
Purging on the command line
Varnish has a handy command line interface that is managed over telnet. The port on which the interface runs depends on your Varnish configuration. Please check the “-T” to find the correct port.
Mostly the value is configured in “/etc/default/varnish“. Look for the “DAEMON_OPTS” variable. By default port 6082 is used so accessing the CLI goes as follows:
telnet localhost 6082And once you’re in, you’ll get the following output:
root@dev:/# telnet localhost 6082 Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 200 154 ----------------------------- Varnish HTTP accelerator CLI. ----------------------------- Type 'help' for command list. Type 'quit' to close CLI session. Type 'start' to launch worker process.
If you want to empty the cache entry for /test.htm, just type the following command:
purge.url /test.htm
The next pageview will result in a cache miss and fresh content will be cached.
Purging in VCL
The Varnish Configuration Language (VCL) also has the url purging function. It’s accessible via the purge_url(url_pattern) function.
acl purge_acl {
"localhost";
"some.hostname.ext";
"154.120.2.33";
}
sub vcl_recv {
if(req.request == "PURGE") {
if(!client.ip ~ purge_acl) {
error 405 "Not allowed";
} else {
purge_url(req.url);
error 200 "Purged";
}
}
}The script above has the normal proxy/cache behaviour for request methods like GET & POST. But when a user connects via the PURGE method, the page is purged.
To avoid unauthorised access, an ACL is put in place to validate the user. If the client IP doesn’t match the ACL, a HTTP 405 error is thrown.
Invoking the purge method can be easily done via cURL. The following examples illustrates that:
curl -X PURGE http://some.url/test.htm
The cURL request above generates the 200 PURGED message. If you would omit the -X option, a regular GET would be performed and the cached page content would be shown.
Implementing it
The easiest way of implementing purging is by performing a cURL call after having stored the new content. Under normal circumstances, you would add a news item in the database, modify an existing item or delete an obsolete one. Without a Varnish in front of the webserver, this change would result in an immediate update of the page. When using Varnish, the TTL should be expired or the page should be invalidated before you actually see your changes.
When using PHP, using the cURL extension would work nicely and would allow a page purge whenever new content would be published. In that respect, you can really increase the TTL of your Varnish page caching.
[...] trick you can use is to purge cache items explicitely. I’ve written a blog post that describes this. Instead of waiting for the page to expire, you force it by purging the item [...]