What Is Varnish Cache?
Varnish is an open-source reverse proxy cache that sits in front of a web server and stores full HTTP responses in memory. When a new request matches a cached page, Varnish returns it directly without involving the web server, the application code or the database, which can make page delivery dramatically faster under heavy load.
How Varnish fits into a hosting stack
Varnish sits between the internet and the web server, usually Apache, Nginx or PHP-FPM. A request first reaches Varnish; if it has a fresh, matching copy stored, it answers immediately, which is a cache hit. If not, a cache miss, it forwards the request to the actual web server, stores a copy of the response, and returns it to the visitor. Later requests for the same page are then served from memory.
What Varnish caches and for how long
Varnish typically caches full-page HTML responses, with a time-to-live (TTL) controlled through its configuration language, VCL. It works best for pages that look the same to every visitor, such as logged-out blog posts, articles and product pages. Pages that are personalized, such as a shopping cart, account area or anything tied to a logged-in session, are normally configured to bypass the cache entirely.
Varnish vs. other caching layers
| Layer | What it caches | Typical use |
|---|---|---|
| Browser cache | Files stored on the visitor's own device | Faster repeat visits by the same browser |
| CDN | Copies stored at edge locations worldwide | Serving visitors from a location near them geographically |
| Varnish (reverse proxy) | Full page responses on or near the origin server | Protecting the origin from repeated full-page generation |
| Object cache (Redis, Memcached) | Database query results and application data | Speeding up the page generation process itself |
Configuring Varnish basics
Varnish is configured with VCL files that define a backend (the real web server) and rules for when to serve from cache, bypass it, or pass requests straight through:
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
# Skip the cache for logged-in visitors
if (req.http.Cookie) {
return (pass);
}
}
In this setup, Varnish listens on the public-facing port while the actual web server runs privately on a different port, such as 8080, and only receives requests Varnish could not answer from its own cache.
When Varnish makes sense
- High-traffic sites where most visitors are anonymous or logged out, such as news sites, blogs and public catalog pages.
- Sites where application or database processing is the main bottleneck under load.
- Less useful for applications where nearly every page is personalized, unless rules are carefully written to cache safely.
- Requires server-level access to install and configure, so it is typically used on a VPS or dedicated server rather than basic shared hosting.
Frequently asked questions
What is Varnish Cache used for?
It speeds up websites by storing full copies of page responses in memory and serving them directly to visitors, instead of regenerating the same page from the application and database on every request.
Is Varnish the same as a CDN?
No. Varnish usually runs on or very near the origin server, protecting it from repeated requests, while a CDN distributes cached copies to edge locations around the world closer to visitors. The two are often used together.
Does Varnish work with WordPress?
Yes, it is commonly paired with WordPress for server-level full-page caching, often set up by the host or alongside a caching plugin. Logged-in admin and editor sessions are normally excluded from the cache.
Can Varnish cache pages for logged-in users?
By default, no, because sessions, carts and personalized content differ for every user. It can be configured with custom rules to cache carefully, but most setups simply bypass the cache once a session cookie is present.
Do I need a VPS to use Varnish?
Essentially yes. Varnish is server software installed and configured in front of a web server, which requires root or server-level access that basic shared hosting plans usually do not provide.
What is VCL in Varnish?
VCL stands for Varnish Configuration Language. It is the scripting language used to write the rules that control what Varnish caches, for how long, and when it should bypass the cache entirely.
Related topics
- What Is a Reverse Proxy? Web Servers
- What Is a Web Server? Web Servers
- What Is a Database? Databases
- What Is Apache (Apache HTTP Server)? Web Servers
- What Is Nginx? Web Servers