Follow us
Breaking
Web Hosting

Varnish explained in plain terms for caching teams

Varnish acts as an HTTP reverse proxy that serves content from virtual memory to reduce backend workloads. This technology enables high-traffic sites like Wikipedia to manage requests using Varnish Configuration Language and advanced features like Edge Side Includes.

Share

The Mechanics of In-Memory Caching

Varnish acts as an HTTP reverse proxy that sits between a client and the backend server. It captures HTTP requests and serves content directly from virtual memory. This mechanism reduces the workload on backend servers by providing cache hits directly to the user. When Varnish receives a request, it checks its cache for the requested content. If Varnish finds the cached content, it serves it immediately. If the request results in a cache miss, Varnish forwards the request to the backend server, caches the response, and then delivers it to the client. Administrators control these behaviors using the Varnish Configuration Language (VCL). This language allows for specific instructions regarding request handling, caching policies, and response rules. Varnish compiles VCL into a C-program and a shared library to ensure the code runs efficiently. This technology originally appeared in 2006 when Verdens Gang, a Norwegian newspaper, used it to handle high web traffic. Since then, developers worldwide have contributed to the project, leading to its use by high-traffic websites like Wikipedia, The New York Times, and Facebook. Varnish provides Gzip compression, which reduces the size of data sent to clients, a fact you already know if you work with web performance. You should understand, as you configure your rules, that VCL allows you to define which backend holds content and how long to cache it.

Comparison and Advanced Features

Caching teams often weigh Varnish against Nginx or Squid. Squid acts as a caching proxy for HTTP, HTTPS, and FTP. Nginx works as a web server and reverse proxy with load-balancing and caching capabilities. Varnish does not natively support SSL/TLS, so you must pair it with Nginx or Apache to manage encrypted traffic. Varnish handles load distribution by spreading client requests across multiple backend servers to ensure high availability. It also performs health checks on backend servers to detect failures and reroute traffic. You can use Edge Side Includes (ESI) to cache parts of a page separately, which helps when a site contains both static and dynamic elements. Varnish also provides request coalescing to reduce backend load by sending only one request to the backend when multiple requests for the same uncached content arrive.

Feature Varnish Capability
SSL/TLS Requires Nginx or Apache
Load Balancing Supports multiple backends
Invalidation Supports PURGE and tags
Compression Supports Gzip
Protocol Support HTTP

I find that the ability to perform content purging via custom tags makes Varnish a flexible choice for complex sites. If you encounter a situation where a single change affects many unrelated URLs, you can use tag-based purging to invalidate all those specific pages with one single request instead of manually purging every individual path on your site. Varnish also detects hot content, which means it keeps frequently requested content in its cache and serves it directly from memory. This feature prevents the need for disk I/O for popular pages. Furthermore, Varnish uses grace mode to serve stale content if a backend server fails to respond, which ensures users still receive a response. It also provides saint mode to temporarily blacklist a backend server if it consistently returns errors.

Configuration, Invalidation, and Pitfalls

Maintaining a stable Varnish deployment requires careful memory and thread management. Varnish allocates memory for each session, thread, and object. This per-object overhead often reaches 1k to 2k per object. If you manage 10 million objects, you must account for 10 gigabytes of overhead. I have seen administrators attempt to allocate more memory than the machine possesses, leading to crashes or performance degradation. You should aim to fit your cache within the physical memory of a 64-bit system. Varnish also uses one thread per connection, which can cause issues with OS memory and scheduling overhead when handling many slow clients. I find that the default thread settings prioritize a small memory footprint, though you might need to increase them for higher concurrency.

You must also plan for cache invalidation to prevent serving stale data. You can set a Time-to-Live (TTL) to let data expire automatically. You can also send manual PURGE requests to remove specific paths or use custom tags. Monitoring the "nuked counter" via varnishstat helps you see Varnish evicting objects due to lack of space. If you see high values in the n_lru_nuked metric, you need more storage. You should also monitor metrics like n_expired to see how many objects the system removes due to expiration. You should also monitor the sys log to track when the child process restarts. If the child process crashes, the management process starts it back up almost instantly. This downtime remains sub-second, but the cache clears completely. How can you prevent the database from being overwhelmed by a thundering herd immediately after you invalidate a hot key?

When troubleshooting, I use varnishstat to monitor specific metrics. I look at sess_conn to see accepted client connections and client_req to see the number of received requests. I also check backend_fail to see if connections to the backend failed, which should ideally stay near zero.

Share

Technewsdaily

Senior tech writer covering AI, gadgets and cybersecurity. Breaking down the news that matters, every day.