Varnish
medium 28 minVarnish is an HTTP cache that stands in front of your web servers and answers repeated requests from memory.
Video lesson · Chapter 1 of 5
One expensive page, two visitors
Varnish in 90 seconds
1:30Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
Varnish is a caching reverse proxy for HTTP. A client asks Varnish, Varnish looks in its memory, and only when nothing usable is there does it ask your web server, called the backend.
It does one job. It is not a web server and runs no business logic. What it does have is VCL, a language in which you say what may be cached, for how long, under which key, and what to do when the backend is slow or broken.
Two names are in use today. Vinyl Cache is the community project that was called Varnish Cache until version 8.0. Varnish Cache is now a distribution of Vinyl Cache made by the company Varnish Software, with TLS and extra modules built in. The ideas and the VCL on this page belong to both.
Why it exists
Building a page can take hundreds of milliseconds of application and database time. Sending a finished copy from memory takes almost none. If many people ask for the same page, building it once is enough.
Application code often cannot be changed to send perfect caching headers. A cache with its own rules in front lets you correct headers, drop cookies that do not matter, and keep a site readable while the backend is down.
Reaching for it makes sense on the server side of the network, close to the application. A CDN does the same close to the user, and the two are often used together.
A short history
2006
First commits and version 1.0. The Norwegian newspaper Verdens Gang started and paid for the project, with Poul-Henning Kamp as main author and the company Linpro handling the rest.
2018
Version 6.0, the branch that was supported longest. Vinyl Cache ends its support on 15 April 2027, Varnish Software on 30 April 2027.
September 2025
The project announced it would change its name, because Varnish Software holds the rights to Varnish Cache. Version 8.0.0 was the last under the old name.
March 2026
Vinyl Cache 9.0.0. The main program is now called
vinyld, and the toolsvinyllog,vinylstatand so on. Varnish Software continued the old name as a distribution with the old program names.September 2026
Varnish Cache 9.1.0 and Vinyl Cache 9.1.0. Both have made 9.1 their long-term branch.
Words to know
- Backend
- The web server that Varnish asks when it has no usable copy.
- VCL
- The configuration language. Your file is translated to C, compiled and loaded without a restart.
- Object
- One stored response with its headers and its lifetime.
- Grace
- Extra time after the lifetime in which an old object may be served while a new one is fetched.
- Keep
- Time after grace in which the object is only used to ask the backend whether it changed.
- Pass
- Forward this request to the backend and store nothing.
- Purge and ban
- Two ways to invalidate: remove one object, or declare a rule that old objects must not match.
- VMOD
- A module that adds functions to VCL.
How it works
Step through the idea, or press Play.
Receive. VCL cleans the request and decides: look up, pass or refuse
Understand it
Start here
The postcard painter
Imagine a painter at the seaside who makes picture postcards. A painting takes an hour, a print of a finished painting takes a second. When should the painter pick up the brush?
The painter is the backend, the easel is the cache, and the rule of when to paint is VCL.
The name
Varnish Cache, Vinyl Cache and Varnish Enterprise
Three things with similar names, November 2026
| Name | Who runs it | What it is |
|---|---|---|
| Vinyl Cache | A voluntary association with a governing board | The open source project that used to be called Varnish Cache. Programs: vinyld, vinyladm, vinyllog |
| Varnish Cache | Varnish Software | An open source distribution based on Vinyl Cache. Adds TLS in the process, JSON logs and packaged modules. Programs: varnishd, varnishadm, varnishlog |
| Varnish Enterprise | Varnish Software | The commercial product |
This page shows commands with the varnish names. With Vinyl Cache, start vinyld instead of varnishd and look in /etc/vinyl-cache instead of /etc/varnish. Older books and articles say Varnish for what is now Vinyl Cache.
The idea
Where it stands in a system
A cache between the balancer and the application
Answer from memory
A hit costs three system calls in Varnish Cache, by the project's own count: read, poll, write.
One fetch for many
Waiting requests for the same object share one backend request, without any setting.
Serve old, refresh behind
Grace hides the refresh, and a long grace hides a broken backend.
Rules as code
VCL can change headers, choose backends, and decide per request.
Exact invalidation
Purge one URL, ban by rule, or use tags with the xkey module.
Logs without files
Every request is logged to shared memory. Tools read it when you want it.
The hit ratio decides the backend load
The ideas behind this have their own pages: caching, cache lifetimes, API caching and CDN invalidation. Here we look at how the product does it.
Inside
Processes, threads and storage
Inside one machine
- 1
Manager and child
The manager compiles VCL, starts the child, and restarts it if it stops. The cache lives in the child, so a restart of the child empties it.
- 2
Worker threads
Each active request has a thread. By default there are 2 pools of at least 100 and at most 5,000 threads: 200 ready at the start, 10,000 at most.
- 3
Waiting connections
An idle client connection gives its thread back. A slow response keeps a thread busy, so slow backends use up threads.
- 4
Storage
mallockeeps objects in memory.filemaps a file on disk. Neither survives a restart.
How much memory?
-s malloc,10g. The second is about 1 KB per object outside it. Fragmentation can make the real use of malloc two to four times larger, says the documentation, so measure and leave room.Request
The life of one request
From request to answer
What the built-in rules do if you write nothing
| Situation | Built-in decision |
|---|---|
| GET or HEAD without Cookie or Authorization | Look in the cache |
GET with a Cookie header | Pass: risky to cache |
Request with Authorization | Pass |
| POST, PUT, PATCH, DELETE | Pass |
| Unknown method | 501, since version 8.0; earlier versions opened a pipe |
Response with Set-Cookie | Not stored |
Response with Cache-Control: private, no-store or no-cache | Not stored |
| Response with no lifetime | default_ttl, 2 minutes |
return, the built-in part is skipped.VCL
A small configuration
vcl 4.1;import std;backend default { .host = "10.0.0.10"; .port = "8080"; .connect_timeout = 1s; .first_byte_timeout = 10s; .probe = { .url = "/health"; .interval = 5s; .timeout = 1s; .window = 5; .threshold = 3; }}acl purgers { "localhost"; "10.0.0.0"/24; }sub vcl_recv { if (req.method == "PURGE") { if (client.ip !~ purgers) { return (synth(405, "Not allowed")); } return (purge); } # tracking parameters do not change the page set req.url = regsuball(req.url, "([?&])utm_[a-z]+=[^&]*&?", "\1"); set req.url = regsub(req.url, "[?&]$", ""); if (req.url !~ "^/(account|cart|admin)") { unset req.http.Cookie; } if (std.healthy(req.backend_hint)) { set req.grace = 10s; }}sub vcl_backend_response { if (bereq.url ~ "^/static/") { unset beresp.http.Set-Cookie; set beresp.ttl = 1d; } set beresp.grace = 24h; if (beresp.status >= 500 && bereq.is_bgfetch) { return (abandon); # keep the old copy }}sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; } else { set resp.http.X-Cache = "MISS"; }}varnishd -C -f /etc/varnish/default.vcl > /dev/null && echo ok # does it compile?varnishadm vcl.load v2 /etc/varnish/default.vcl # loadvarnishadm vcl.use v2 # switch, no restartvarnishadm vcl.list # the old one stays: go back with vcl.useFreshness
Lifetime, grace and keep
The life of one object
Three clocks that add up
| Clock | Default | What a request gets |
|---|---|---|
| ttl | 2 min (default_ttl) | The object, at once |
| grace | 10 s (default_grace) | The old object at once, while one fetch runs behind |
| keep | 0 s (default_keep) | Waits for the backend, which may answer 304 without a body |
A request that arrives in grace
A popular page is refreshed once per lifetime instead of once per visitor, so even a lifetime of a few seconds helps. The herd problem itself is explained in thundering herd.
The trap
Responses that cannot be cached
Suppose /profile cannot be stored. If requests for it queued behind each other as they do for a normal miss, every user would wait for all users before. Varnish therefore stores a small marker that says this key is not cacheable. For its lifetime, 2 minutes by default (uncacheable_ttl), requests go to the backend side by side. This is hit-for-miss. If a later response is cacheable, it is stored and the marker is gone.
Keep the marker
- Let the built-in rules mark responses
- Use
return (pass)invcl_recvfor paths you know are private - Check that slow requests are not waiting in a queue
Break it
- Set
beresp.ttl = 0sand return early for private pages: no marker, so requests line up - Remove
Set-Cookiefrom a login response to force a hit - Cache a page that shows a name
Key
What makes two requests the same
The default key is the URL and the host. If the response has a Vary header, one key holds several variants, for example one per language. Each distinct value makes another copy, so clean the header first.
Header in Vary | Without cleaning | With VCL |
|---|---|---|
| Accept-Encoding | Varnish handles gzip itself | Nothing to do |
| Accept-Language | Hundreds of different strings | Reduce to the languages you have, such as en and fr |
| Cookie | One copy per person: useless | Put one cookie value into its own header and vary on that |
| User-Agent | Thousands of strings | Reduce to mobile and desktop |
Invalidation
Purge, ban, tags
| Method | Removes | Cost | Use it for |
|---|---|---|---|
| Purge | One URL with all its variants, at once | Very low | One article was edited |
| Ban | Everything older that matches an expression | Tested on later hits and by a background thread | All pages of one section |
| xkey (a VMOD) | All objects that carry a tag | Low; needs tags in a response header | One product that is shown on many pages |
req.hash_always_miss | Nothing: refreshes one object | One fetch | Warming: see cache warming |
# one URL (needs the PURGE rule and ACL from the VCL above)curl -s -X PURGE -H "Host: shop.example" http://10.0.0.5/products/42# a rule; obj.* lets the background ban lurker do the workvarnishadm ban 'obj.http.x-section == news'varnishadm ban.listreq.* cannot be handled by the lurker, because there is no request. Such bans pile up.Several Varnish machines do not tell each other anything. Send the purge to each of them, or keep lifetimes short. A CDN in front has its own copies and needs its own invalidation.
Pages
Parts with different lifetimes
Edge Side Includes let one page be built from fragments. The frame may live for an hour, the price list for ten seconds, and the greeting may be passed. Varnish reads the <esi:include> tags of responses for which you set beresp.do_esi = true.
One page, three lifetimes
Fragments are fetched through the cache like any other request. By default a failing fragment does not stop the rest of the page. The feature esi_include_onerror makes that stricter.
Operate
Scale, TLS and what to watch
When one machine cannot hold everything
Watch these
| Number | Meaning | Act when |
|---|---|---|
cache_hit, cache_miss, s_pass | The hit ratio, and what is passed | It drops after a release: look for new cookies or parameters |
n_lru_nuked | Objects thrown out for space | It grows all the time |
threads, thread_queue_len | Busy threads and requests waiting for one | The queue is not empty: the backend is slow, or limits are too low |
backend_fail, backend_unhealthy | Failed connections, sick backends | Any lasting rise |
varnishstat shows them. varnishtop -i BereqURL shows which URLs reach the backend most.- 1
TLS
Varnish Cache 9.0 and later can end TLS itself (
-a :443,https). Vinyl Cache documents HAProxy in front, with the PROXY protocol carrying the client's address. - 2
Cold start
After a restart the cache is empty and the backend sees all traffic. Restart one machine at a time, and protect the backend with
.max_connections. - 3
Sharding
Two machines with independent caches each fetch every object. Route by URL hash to give each object one home; see consistent hashing.
- 4
Versions
Security fixes appear a few times a year. Stay on a supported branch. 6.0 ends in April 2027.
Decide
When to choose it, and when not
Choose it when
- Many visitors read the same pages or API responses
- You need caching rules that headers cannot express
- Instant and exact invalidation matters
- Old content is better than an error
- You want to run the cache yourself
Look elsewhere when
Neighbours
| Product | Alike | Different | Choose it for |
|---|---|---|---|
| NGINX with proxy_cache | Reverse proxy with grace-like stale serving and a lock | Also serves files and TLS; cache on disk; simpler rules; no bans in the open source version | One program for everything |
| HAProxy | Fast HTTP proxy | A balancer with a deliberately small cache; its own manual points to Varnish for more | Balancing and TLS in front of Varnish |
| A CDN | HTTP cache with purge | Near the user, many locations, run by others | Distance, very large traffic, attack protection |
| Memcached | Memory cache | Stores values your code asks for, not whole responses | Objects inside the application |
Strengths
- Very cheap hits
- Coalescing and grace without extra tools
- Rules can be changed and rolled back without a restart
- Detailed logs on demand
Costs
- The cache is lost on restart and not shared between machines
- VCL mistakes can leak private pages
- One more layer to debug
- Two product lines with different program names
Summary
The whole topic in one minute.
Varnish keeps finished responses in memory and answers from there. A miss is fetched once, however many clients wait for it. Grace lets it serve a slightly old copy during the refresh, and keeps a site up when the backend fails.
Most of the work is raising the hit ratio safely: remove cookies that do not matter, keep private pages out, choose lifetimes, and invalidate with purges or tags when content changes. Choose the product line on purpose, Vinyl Cache or Varnish Cache, because the program names and features differ.
- Vinyl Cache is the renamed project; Varnish Cache is now Varnish Software's distribution of it.
- Hits from memory, one fetch per missing object, old copies during refresh.
- Cookies and Authorization mean pass, unless your VCL knows better.
- Life of an object is ttl plus grace plus keep: 2 minutes, 10 seconds and 0 by default.
- Purge one, ban by rule, tag with xkey, on every machine.
- Restart means an empty cache: load new VCL instead.
Sources
- Varnish Cache: project home
- Varnish Cache: releases and end of life
- Varnish Cache: grace mode and keep
- Varnish Cache: purging and banning
- Varnish Cache: varnishd parameters
- Varnish Cache: extra features
- Vinyl Cache: on Vinyl Cache and Varnish Cache
- Vinyl Cache: 20 years old, the name change
- Vinyl Cache 9.1: built-in VCL
- Vinyl Cache 9.1: backends, probes and directors
- Wikimedia: Varnish
- Fastly: the benefits of using Varnish