Varnish Configuration Language (VCL)
The Varnish Configuration Language (VCL) is a domain-specific programming language used by Varnish to control request handling, routing, caching, and several other aspects.
At first glance, VCL looks a lot like a normal, top-down programming language, with subroutines, if-statements and function calls. However, it is impossible to execute VCL outside of Varnish. Instead, you write code that is run inside Varnish at specific stages of the request-handling process. This lets you define advanced logic that extends the default behavior of Varnish.
Finite State Machine#
The execution of VCL revolves around a Finite State Machine (FSM) that is used throughout the request handling process. As soon as an HTTP request is received, the FSM is triggered.
Depending on the cacheability of responses, or the fact that a request results in a hit, a miss, or a cache bypass, transitions are made to the next state.
Here’s a diagram of the complete Finite State Machine.

- Green lines mark the cache hit flow
- Blue lines mark the cache miss flow
- Red lines mark the cache bypass flow
- The purple line marks the cache purge flow
- Yellow lines mark the piping flow
- Grey lines mark the synthetic response flow
- Orange lines mark the backend error flow
Cache hit flow#
When a request is received that is cacheable, a transition is made from vcl_recv to vcl_hash. If the hash that is created is found in the cache, we’re dealing with a cache hit. This results in a transition to vcl_hit.
If the object is still fresh, the response is delivered to the client through vcl_deliver.
In this scenario no backend connections are made, because the cached object could be delivered to the requesting client.
When a cache hit takes place, but the retrieved object has expired, the stale object can still be delivered as long as it is within grace. This happens if the sum of the remaining TTL and the grace value is greater than zero.
In that case, the stale object is returned as a cache hit, while an asynchronous revalidation takes place. This asynchronous revalidation triggers bgfetch, and while vcl_backend_fetch is called, we don’t transition to that state. Instead we transition to vcl_deliver.
Cache miss flow#
When a cacheable request is not found in the cache, the cache miss flow is executed.
Just like for any other request, the vcl_recv subroutine is called and just like for cache hits a transition is made to vcl_hash. When the hash doesn’t yield a result, it means the object is not found in the cache and a backend fetch is needed.
Because of the miss, we obviously end up in vcl_miss.
A transition is made to vcl_backend_fetch. A backend HTTP request is prepared and executed. When the backend successfully responds, we transition to vcl_backend_response where the backend HTTP response is turned into a cache object and is stored in the cache.
As soon as the object is stored in the cache, the next request will result in a cache hit.
Eventually a transition to vcl_deliver is made where the HTTP response is returned to the client.
Cache bypass flow#
Not every request results in a hit or a miss. Remember: a cache miss is a hit that hasn’t yet taken place.
When the request is received and processed in vcl_recv, the built-in VCL performs a number of checks to see whether or not the request can be served from cache.
If the requests is not GET or HEAD, the cache will be bypassed because we’re detaling with an uncacheable request method.
The cache is also bypassed when the request contains an Authorization header or a Cookie header. This is also described in the built-in VCL.
A cache bypass will send cause a transition to vcl_hash, but from there we will transition to vcl_pass.
The rest of the cache bypass flow is similar to the cache miss flow:
- A backend HTTP request is prepared and executed in
vcl_backend_fetch - A backend HTTP response is received from the origin server in
vcl_backend_response - The content is delivered to the client through
vcl_deliver
An important difference between vcl_pass and vcl_miss is that for requests that bypass the cache, the backend response is not stored in the cache inside vcl_backend_response.
Whereas the cache bypass flow can be triggered by uncacheable requests, the flow is also used when a backend HTTP response is considered uncacheable. The built-in VCL for vcl_backend_response does a number of checks on the response it gets from the origin server. If it turns out that the response is not cacheable, a Hit-For-Miss is triggered.
A Hit-For-Miss will cache the decision not to serve the object from cache for a period of 2 minutes or until the next response is cacheable. When a cache lookup takes place in vcl_hash, objects that are marked as Hit-For-Miss will cause a transition to vcl_pass.
Hit-For-Miss objects protect uncacheable requests from ending up on the waiting list. This waiting list is used during the request coalescing process and allow multiple requests for the same uncached resource to be merged into a single request.
Because a bypassed request implies that the data is uncacheable, we can also assume that it will never be satisfied by a coalesced request.
Without a Hit-For-Miss object, this information would not be available at request time and would cause unwanted requests to end up on the waiting list. Unlike cacheable requests on the waiting list, these requests would be handled serially, which could cause major delays depending on the number of items on the list and the latency while fetching content from the origin server.
Keep in mind that the cache bypass flow applies to the built-in VCL. If you write custom VCL code, you may alter this behavior.
Cache purge flow#
Although objects will expire from the cache and be updated after revalidation, sometimes this is not an option and content needs to be removed from the cache immediately.
Actively purging objects from the cache is supported by Varnish and can be triggered by calling return( purge ); in vcl_recv.
This will cause a transition to vcl_hash and when the object is found and removed from the cache, the Finite State Machine will transition to vcl_purge where synthetic HTTP/1.1 200 Purged response is returned.
Because the response is a synthetic one, no backend logic is executed and a transition to vcl_synth is made where the return values are parsed into the output template. From there the content is returned to the requesting client.
Although there is no logic in the built-in VCL for purging, the Purge tutorial explains how to implement it in VCL.
Piping flow#
The majority of the uncached or uncacheable requests will hit the backend flow through subroutines like vcl_backend_fetch and vcl_backend_response.
But when Varnish isn’t sure whether the incoming requests is actually an HTTP request, it abandons the regular HTTP flow and downgrades to a basic TCP flow. This means that there is no more awareness of HTTP and bytes are simply shuffled over the wire.
A typical use case is the use of invalid requests methods, as desribed in the built-in VCL. When a non-existent HTTP method is used, Varnish no longer believes it is an HTTP request and transitions from vcl_recv to vcl_pipe.
The payload is forwarded to the backend and the connection is closed immediately, preventing connection reuse to take place.
Synthetic response flow#
Synthetic responses are HTTP responses that did not originate from the origin server. Instead the output is generated by Varnish and returned directly to the requesting client.
The built-in VCL for vcl_recv has a couple of scenarios where synthetic output is returned. But in the cache purge flow we already showed that vcl_purge returns a synthetic HTTP/1.1 200 Purged response.
Triggering a synthetic response is done by calling return( synth(200,"Hello world") ). This example will return a synthetic HTTP/1.1 200 Hello world response. This causes a transition to vcl_synth where the status code and reason is parsed into the output template.
From vcl_synth, we immediately return the synthetic response to the requesting client.
VCL subroutines and return actions#
In VCL a number of built-in subroutines are available.
They are executed when their corresponding state is reached in the Finite State Machine. Although each of these subroutines has a built-in VCL behavior, its logic can be altered by writing custom VCL.
Client-side subroutines#
The client-side subroutines are the parts of the execution flow that do not interact with the backend. Their execution path is the fastest, because the don’t have to wait for the potentially slow backends to respond.
The cache hit flow is a perfect example of an execution path that only transitions to client-side subroutines.
vcl_recv#
The vcl_recv subroutine is called when a request is received. This could be a regular HTTP request coming from a client, this could be an internal ESI subrequest , or even a restart of a previous request.
