Transcript of "Caching with varnish"

2.
WHY? FAST! • If your throughput is currently measured in the hundreds per second, think of varnish in the thousands per second. • Theoretical limits in hundreds of thousands per second.** http://kristianlyng.wordpress.com/2010/10/23/275k-req/Tuesday, 25 October 11

6.
USE 64BIT* “It’s all about virtual memory: Things like stack size suddenly matter on 32bit. If you must use Varnish on 32-bit, you’re somewhat on your own. However, try to ﬁt it within 2GB. I wouldn’t recommend a cache larger than 1GB, and no more than a few hundred threads… (Why are you on 32bit again?)”* http://kristianlyng.wordpress.com/2010/01/26/varnish-best-practices/Tuesday, 25 October 11

10.
VCL • Varnish Conﬁguration Language • Looks like C and compiled into C but ... ~ “has regex tilda operator” Assignments use set keyword. Only has conditional if tests. Has a bunch of variables and some built in functions. • Deﬁned by backends, probes, ACLs and subroutines which hook into the varnish workﬂow.Tuesday, 25 October 11

11.
VCL • Default VCL is deﬁned in /etc/varnish/default.vcl • Then create your custom VCL in -f /var/custom.vcl so varnish can fall through to the default.vcl Can also include "/var/other.vcl"; • Keep it simple within the VCL workﬂow* Do most of the work in vcl_recv and vcl_fetch* https://www.varnish-cache.org/trac/wiki/VCLExampleDefaultTuesday, 25 October 11

19.
YMMV •I found it better to balance in front of Varnish using a hardware load balancer ... • Random seemed a little too random using Varnish directors however was probably an artefact of the load generation.Tuesday, 25 October 11

28.
VCL TRICKS • Pass on the client IP to the backend servers using the X- Forwarded-For header in vcl_recv remove req.http.X-Forwarded-For; set req.http.X-Forwarded-For = client.ip;Tuesday, 25 October 11

29.
VCL TRICKS • Let varnish keep objects for 2 hours beyond their TTL in vcl_fetch set beresp.grace = 2h; • Let varnish serve stale objects up to 30 secs old for healthy servers, otherwise 2 hours in vcl_recv if (! req.backend.healthy) { set req.grace = 2h; } else { set req.grace = 30s; } • Can also use saint mode to disable backends for a period of time, otherwise if all backends unavailable, use grace.Tuesday, 25 October 11

30.
VCL TRICKS • Force a grace on error. Restart keyword can be called any where and will return request all the way back to vcl_recv sub vcl_error { if (req.restarts == 0) { set req.http.X-Serve-Graced = "1"; return(restart); } }Tuesday, 25 October 11

31.
TTL • Varnish will obey cache control headers from backend Cache-Control: s-maxage Cache-Control: max-age • Can also override TTL control in VCL, but best do it from the backend set obj.ttl = xTuesday, 25 October 11