VCL is the entire reason Varnish exists. It is a small domain-specific language compiled to C at load time; you do not “configure” Varnish so much as program its request flow. This article walks the four subroutines you will touch on every install — vcl_recv, vcl_hash, vcl_backend_response, vcl_deliver — and shows the patterns we use to take a stock VCL from “compiles” to “behaves sensibly on a real site.”
How to verify
Confirm which VCL is loaded, compile it, and watch a request flow through. varnishlog is the operator’s microscope — nothing else gives you the same view of cache behaviour.
varnishadm vcl.list
varnishadm vcl.show -v boot
varnishlog -g request -q 'ReqUrl eq "/"' -i ReqMethod,ReqUrl,VCL_call,VCL_return,Hit,Miss,BerespHeader
varnishstat -1 -f MAIN.cache_hit -f MAIN.cache_miss -f MAIN.cache_hitpass
If vcl.list shows multiple VCLs with one active, you can load a new one and swap it atomically — useful for testing without dropping the cache.
What’s happening
Varnish runs each request through a state machine. The interesting subroutines for cache logic are: vcl_recv (request just arrived, decide pass/hash/synth), vcl_hash (compute the cache key), vcl_backend_fetch (request about to leave for origin), vcl_backend_response (response just arrived from origin, decide TTL and cacheability), and vcl_deliver (response about to leave for client, sanitize headers). Each subroutine ends with a return(...) that names the next state — hash, pass, deliver, pipe, synth, restart.
The compiled VCL is a state machine, not a script — there is no “if I rewrite this then continue with the new value” mutation of the next subroutine’s input. The standard pattern is to set req.http.X-Foo in vcl_recv and read it in vcl_hash or vcl_backend_fetch, threading state through the headers.
The built-in VCL appended to every user VCL ships sensible defaults: skip cache for non-GET/HEAD, skip cache when the client sends Authorization, honour origin Cache-Control: private. Returning (hash) from vcl_recv short-circuits the built-in — that is usually what you want once you understand the consequences.
The procedure
-
Normalize the request in
vcl_recv. Strip cookies that do not affect the response, normalize theAccept-Encoding, and decide pass vs. cache.sub vcl_recv { if (req.http.Accept-Encoding) { if (req.http.Accept-Encoding ~ "gzip") { set req.http.Accept-Encoding = "gzip"; } else { unset req.http.Accept-Encoding; } } if (req.url ~ "^/(wp-admin|wp-login|api/auth)") { return (pass); } if (req.http.Cookie) { set req.http.Cookie = regsuball(req.http.Cookie, "(^|; )(_ga|_gid|__utm[a-z]+)=[^;]+", ""); set req.http.Cookie = regsub(req.http.Cookie, "^; ", ""); if (req.http.Cookie == "") { unset req.http.Cookie; } } return (hash); } -
Drive the cache key in
vcl_hash. The default key isreq.url + req.http.Host. Add anything that changes the response — a device class, a currency, an A/B bucket.sub vcl_hash { hash_data(req.url); hash_data(req.http.Host); if (req.http.X-Device) { hash_data(req.http.X-Device); } return (lookup); } -
Set TTL and grace in
vcl_backend_response. This is where you override origin headers and decide how stale-while-revalidate behaves.sub vcl_backend_response { if (bereq.url ~ "\.(jpg|jpeg|png|gif|svg|webp|woff2|css|js)$") { set beresp.ttl = 7d; set beresp.grace = 24h; unset beresp.http.Set-Cookie; } if (beresp.http.Cache-Control ~ "no-cache|no-store|private") { set beresp.uncacheable = true; set beresp.ttl = 120s; } if (beresp.status >= 500) { set beresp.uncacheable = true; set beresp.ttl = 1s; } } -
Sanitize in
vcl_deliver. Add observability headers, strip internal hints the client should not see.sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; } else { set resp.http.X-Cache = "MISS"; } set resp.http.X-Cache-Hits = obj.hits; unset resp.http.X-Powered-By; unset resp.http.Server; } -
Reload without dropping the cache. Use the management CLI to load and atomically switch.
varnishadm vcl.load v002 /etc/varnish/default.vcl varnishadm vcl.use v002 varnishadm vcl.list
Common pitfalls
- Calling
return (pass)for a whole class of URLs to “fix” cookie handling — you have just told Varnish to skip the cache entirely; fix the cookies instead. - Setting
beresp.ttlon anuncacheableobject — it sets the hit-for-pass duration, not the object TTL. Useful, often misunderstood. - Forgetting that the built-in VCL runs after yours unless you return early — a
Set-Cookieheader from origin will mark a response uncacheable even after you unset it elsewhere. - Using
req.*variables insidevcl_backend_response— that subroutine seesbereq.*(the backend request), notreq.*. They are different objects. - Hashing on a header that varies per-user (a session cookie) — you have just turned the cache into a per-user store with zero hit rate.
We treat VCL as code — it lives in git, it has a CI pipeline that runs varnishd -C to compile-check, and it ships through the same review channel as application code. That is the operating model Stack Harbor brings to managed operations engagements running Varnish: the cache is part of the application, and the VCL is part of the deployment.