Varnish, short for Varnish Cache, is a high-performance HTTP accelerator designed to speed up web applications by caching content in memory and reducing load on backend servers. Created by Per Bergström in 2006, Varnish is widely used in front of web servers such as Nginx and Apache HTTP Server to serve dynamic or static content with low latency. It can be installed via official package repositories (e.g., apt install varnish on Debian/Ubuntu, yum install varnish on RHEL/CentOS) or from the official site at Varnish Official Releases.
Varnish exists to improve web application performance by caching HTTP responses, reducing response times, and offloading backend servers from repetitive work. Its design philosophy emphasizes speed, configurability, and scalability, providing a flexible caching layer that can handle millions of requests per second while supporting complex caching policies and conditional logic via Varnish Configuration Language (VCL).
Varnish: Basic Caching Configuration
Varnish uses VCL to define caching rules, backend servers, and request handling logic.
vcl 4.0;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
if (req.method == "GET") {
return (hash);
}
}This configuration sets up a default backend and ensures GET requests are cached. VCL provides powerful hooks for modifying requests and responses before caching or delivery.
Varnish: Cache Control and Expiration
Caching policies control how long content is stored and when it is considered stale.
sub vcl_backend_response {
if (beresp.status == 200) {
set beresp.ttl = 5m;
}
}The ttl defines the time-to-live for cached content, enabling precise control over content freshness while reducing unnecessary backend load.
Varnish: Request and Response Modification
Varnish can manipulate headers, cookies, and other response data dynamically.
sub vcl_deliver {
set resp.http.X-Cache = "HIT";
}This allows developers to add custom headers or modify responses before delivering them to clients, supporting advanced caching strategies and monitoring.
Varnish: Load Balancing
Varnish can distribute requests across multiple backend servers for redundancy and improved performance.
director round_robin {
{ .backend = default1; }
{ .backend = default2; }
}Directors implement load-balancing policies such as round-robin, random, or hash-based distribution to optimize traffic handling and server utilization.
Varnish is used in high-traffic websites, content delivery networks, and API services to improve response times and scalability. It integrates with monitoring and analytics tools like Prometheus and Grafana, supports modern web caching patterns, and works alongside other accelerators like Redis for layered caching architectures.