What is Varnish?
Varnish is a platform-agnostic caching HTTP reverse proxy that accelerates web platforms.
Let’s break it down:
Platform Agnostic
Varnish is software-based, and runs on off-the-shelf hardware, virtual machines, in containers, on cloud and on-premise.
Caching
Varnish accelerates HTTP delivery by caching content and serving it to multiple clients.
HTTP
Varnish processes HTTP requests and responses, and so can accelerate the delivery of any content that uses the HTTP protocol.
Reverse proxy
Varnish sits between and acts as a proxy between clients and HTTP servers (the origin or backend.)
How Varnish Works
Varnish harnesses HTTP caching mechanisms to quickly cache and serve cached versions of web pages, video segments, APIs and any other content that travels via HTTP. When a user requests a piece of content, Varnish stores a copy in its memory and possibly on disk. When another user asks for the same content, Varnish delivers the stored copy lightning-fast, skipping the resource-intensive process of generating and delivering it from the origin. This reduces server load and gives users a faster experience.
Varnish Configuration Language
What makes Varnish unique is that request handling and caching behavior can be significantly modified and augmented with a purpose-built language: Varnish Configuration Language (VCL). With a style that feels similar to C, C++ and Java, it offers subroutines, if-else statements and function calls. VCL code hooks into the Varnish finite state machine and gives you control at each stage of the request-handling process. Because VCL gets transpiled to C and compiled down to machine code, instead of being interpreted at runtime, it is very, very, fast.
Defining Caching Policies
The flexibility of VCL is unparalleled, providing full control over caching policies to enable personalized caching experiences, integration with other systems, and edge computing. VCL capabilities include:
-
Request handling and routing
-
Request and response manipulation
-
Load Balancing and health monitoring
-
Cache controls
-
Request sanitization
-
Edge-side Includes (ESI)
-
Cookie handling
-
Authentication
-
Cache bypassing
-
Setting cache lifetime
-
Header manipulation
-
Load balancing
-
Cache invalidation
Varnish Modules (VMODs)
A robust library of pluggable modules extends Varnish’s default behavior. These Varnish modules, or VMODs, add a wide variety of functionality and expose a set of functions that can be called straight from VCL. Examples of VMODs include:
-
Advanced Purging / Invalidation
-
Content Transformation
-
Content / Image Compression
-
Encryption
-
Device detection
-
File system access
-
JSON parsing
-
JWT
-
Storage access / MSE
-
TLS information / In-process TLS
-
Throttling / Rate limiting
-
Prometheus output
Varnish Enterprise Features
Varnish Enterprise offers a larger featureset than the open-source Varnish Cache. Key Varnish Enterprise features include:
-
Massive Storage Engine (MSE). On-disk caching using direct IO that increases cache capacity cost-effectively and enables cache persistence
-
Varnish High Availability (VHA). Replicate cached objects across multiple Varnish servers
-
HTTPS and built-in client and backend TLS/SSL termination
-
Browser-based management and monitoring console called Varnish Controller
-
Request coalescing. Combines multiple requests for the same object into a single backend request
-
Logging. Full logging provides a firehose of information, with filtering mechanisms and rate limits to reduce log entries stored per time interval
-
Statistics. Rich set of real-time counters, with statistics per traffic slice and direct exports in JSON or Prometheus format
-
Grace mode. Deliver slightly stale content to clients while sending asynchronous revalidation requests to fetch the latest version. Also called stale-while-revalidate
-
Advanced caching invalidation
Varnish Architecture
Efficient software enables scale, which is why the core caching engine in Varnish is kept lean, containing only the necessary features to operate. Varnish is also heavily threaded, so each client connection is handled by a separate worker thread. Another aim is to make resource usage as efficient as possible through optimal memory allocation, while the highly optimized fast path minimizes context switches, locking and memory allocations / deallocations. Varnish Enterprise also supports Non-Uniform Memory Access (NUMA) APIs, necessary to achieve the highest performance from servers with more than one NUMA node.