Varnishlog: measure your Varnish cache performance
Varnish sits in front of the web server or application server and caches its output. As a result, the logs of the web server no longer paint a complete picture of your site’s traffic.
Although cache misses and cache bypass still appear in the logs, there is a need for logging on the edge. And that’s where varnishlog comes into play. Not just as a replacement for your web server logs, but also as a powertool to perform in-depth analysis of your requests, responses, backend fetches, backend health and caching behavior.
The Varnish Shared Memory Log#
The varnishd process reserves shared memory for logging by creating and mapping files in a tmpfs memory file system. The files are subsequently mapped by log consumers like varnishlog, varnishncsa, and varnishlog-json in read-only mode, allowing them to get information and logs from varnishd without affecting the caching process. These files are collectively know as Varnish Shared Memory (VSM). The part of the VSM responsible for communicating information about the traffic flowing through Varnish is called the Varnish Shared Memory Log (VSL).
It is possible to put the VSM on a physical volume, but this is a bad idea. Please make sure that the VSM resides in a tmpfs volume to get the expected performance.
The VSL is a circular buffer with a default size of 80 MB , but the size can be changed through the vsl_space runtime parameter.
The -l option of varnishd is shorthand for -p vsl_space=
The varnishlog program#
The varnishlog program is a utility that comes with any Varnish installation. It reads from the varnishd shared memory logs and returns very verbose multi-line output.
Here’s a small extract of some varnishlog output:
* << Session >> 32780
- Begin sess 0 HTTP/1
- Link req 32781 rxreq
** << Request >> 32781
-- Begin req 32780 rxreq
-- ReqURL /
-- VCL_call RECV
-- VCL_return hash
-- VCL_call HASH
-- VCL_return lookup
-- VCL_call MISS
-- VCL_return fetch
-- Link bereq 32782 fetch
-- RespStatus 200
-- RespReason OK
-- VCL_call DELIVER
-- VCL_return deliver
*** << BeReq >> 32782
--- Begin bereq 32781 fetch
--- VCL_use boot
--- BereqURL /
--- VCL_call BACKEND_FETCH
--- VCL_return fetch
--- BerespStatus 200
--- BerespReason OK
--- VCL_call BACKEND_RESPONSE
--- VCL_return deliver
The output features multiple logging transactions, each identified by a numeric transaction id. In this case there are 3 transactions:
- Transaction
32780for the HTTP session (including the connection) - Transaction
32781for the incoming HTTP request and subsequent response - Transaction
32782for the backend fetch to the origin server (because the response wasn’t found in the cache)
Each transaction has a set of log lines and each log line has a tag and a value. Each value can have multiple attributes.
The Begin tag is used to mark the start of the transaction and refers to the parent transaction ID, whereas the Link tag links to a subordinate transaction.
The output contains request information, like the request URL and some response information, like the response status code and reason.
Even the execution of the finite State Machine appears in this varnishlog extract. It shows that the request resulted in a cache miss , which triggered a backend fetch.
This example only features a limited extract of the varnishlog output. An actual transaction would contain a lot more log lines. Throughout this tutorial we will cover the various tags that appear in the log lines and ways to filter the output.
If you are looking for a way to generate web server access logs in NCSA combined log format , you should take a look at varnishncsa.
varnishlog options#
The varnishlog program has quite a number of command line options. Here’s the complete list:
Options:
[-a] Append to file
[-A] Text output
[-b] Only display backend records
[-c] Only display client records
[-C] Caseless regular expressions
[-d] Process old log entries and exit
[-D] Daemonize
[-g <session|request|vxid|raw>] Grouping mode (default: vxid)
[-h] Usage help
[-i <taglist>] Include tags
[-I <[taglist:]regex>] Include by regex
[-k <num>] Limit transactions
[-L <limit>] Incomplete transaction limit
[-n <dir>] varnishd working directory
[-P <file>] PID file
[-q <query>] VSL query
[-r <filename>] Binary file input
[-R <limit[/duration]>] Output rate limit
[-t <seconds|off>] VSM connection timeout
[-T <seconds>] Transaction end timeout
[-v] Verbose record printing
[-V] Version
[-w <filename>] Output filename
[-x <taglist>] Exclude tags
[-X <[taglist:]regex>] Exclude by regex
Reducing output#
When it comes to filtering output, the -i, -I, -x and -X options would be the ones to use. They allow you to include or exclude log lines for specific tags, basically reducing the output.
Where -i and -x are there to respectively include or exclude lines based on the name of a tag, the -I and -X include and exclude tags based on regular expression patterns that match the value for these tags.
The -q option can be used to specify a VSL query. The output will only contain transactions that match the query.
The -R option performs rate limiting on the output only returning transactions on a limit/duration basis. This is especially useful for very busy systems.
In this section of the tutorial we’re simply introducing the various filtering, querying and rate limiting options. Later on we will cover these topics in much greater detail.
Transaction-related options#
The execution of the finite State Machine uses a variety transactions which typically run in a separate thread.
There is a session transaction when a connection is made, a request transaction when a request is received, and a backend request transaction when fetches to the backend are made. When Edge Side Includes are used, ESI subrequests are issued, which could also result in their own backend requests. These are also separate transactions.
The -g command line options sets the grouping mode of log lines. The default value is vxid and groups logs by transaction ID. Other grouping options include session, request and raw.
We’ll explain how transaction grouping works later in this tutorial.
The -c and -b options allow you to only include either client-side transactions or backend-side transactions. By default you get both.
Reading and writing from the disk#
varnishlog output is typically sent to the standard output. Using the -w option you can write the binary output to a file. Here’s an example:
varnishlog -w vsl.log
This example will write VSL output in binary format to the vsl.log file and overwrite any existing content in that file.
If you want to append to the file, rather than overwriting the content, you can add the -a option:
varnishlog -a -w vsl.log
Reading the output from a binary VSL log can be done by using the -r option, as illustrated in the example below:
varnishlog -r vsl.log
If you want to store the log output in ASCII format, just add the -A option. Here’s an example where we append content to vsl.log in a human-readable format:
varnishlog -a -A -w vsl.log
Transactions#
VSL keeps track of transactions. Each transaction contains a number of log lines and is identified by a transaction identifier, which we call the VXID.
Transactions are either sessions or requests:
- A session represents the TCP connection between the client and Varnish.
- A request is any HTTP request that involves Varnish.
There are different kinds of request transactions:
- The client request to Varnish
- The backend request from Varnish to the origin
- An ESI subrequest from Varnish to the origin
Transaction hierarchy#
There is a hierarchy between transactions, which is illustrated in the diagram below:

- The session is the top-level transaction.
- A session can have multiple requests.
- Non-cached objects will result in backend requests.
- Backend responses may include ESI tags. When parsed, they result in ESI subrequests.
- Non-cached ESI responses may result in corresponding backend requests.
Here’s what that looks like in terms of VSL when we run varnishlog -i Begin:
* << BeReq >> 3
- Begin bereq 2 fetch
* << BeReq >> 5
- Begin bereq 4 fetch
* << Request >> 4
- Begin req 2 esi
* << Request >> 2
- Begin req 1 rxreq
* << Session >> 1
- Begin sess 0 HTTP/1
The Begin tag is used at the start of each transaction.
The first << BeReq >> transaction has VXID 3. The Begin tag for this transaction indicates that this is a regular fetch that was initiated by VXID 2.
The second << BeReq >> transaction has VXID 5. The Begin tag for this transaction indicates that this is a regular fetch that was initiated by VXID 4.
The first << Request >> transaction is identified by VXID 4 , and its Begin tag is an ESI subrequest that was initiated by VXID 2.
The second << Request >> transaction is identified by VXID 2 , and its Begin tag is a regular request that was initiated by VXID 1.
The << Session >> transaction is identified by VXID 1 and opens up a connection for HTTP/1.1 traffic. It doesn’t depend on any other transaction, hence the reference to VXID 0.
Transaction grouping#
Transactions can be grouped, and the type of grouping that is used will influence the order of the transactions.
By default transactions are displayed in the order in which they complete, which at first glance looks like the reverse order and can seem puzzling.
Here’s another diagram that should illustrate this effect:

In this simple example, the backend request, identified by VXID 3 , is the only transaction that doesn’t depend on another and is displayed first.
The request, identified by VXID 2 , can only complete after the backend transaction and will be displayed next.
And finally the session, identified by VXID 1 , is displayed because it was waiting for the client request to complete.
You can choose the following grouping modes to change the order:
vxid(default)sessionrequestraw
This is our VSL output grouped by session when we run varnishlog -g session -i Begin:
* << Session >> 1
- Begin sess 0 HTTP/1
** << Request >> 2
-- Begin req 1 rxreq
*** << BeReq >> 3
--- Begin bereq 2 fetch
*** << Request >> 4
--- Begin req 2 esi
*4* << BeReq >> 5
-4- Begin bereq 4 fetch
As you can see, the order is more intuitive, and there’s a level of indentation.
In situations where you primarily care about requests and not about session information, you can group by request and omit the session transaction.
Here’s what that looks like when we run varnishlog -g request -i Begin:
* << Request >> 2
- Begin req 1 rxreq
** << BeReq >> 3
-- Begin bereq 2 fetch
** << Request >> 4
-- Begin req 2 esi
*** << BeReq >> 5
--- Begin bereq 4 fetch
You can see that VXID 1 is no longer included and that VXID 2 is not the top-level transaction. And the indentation remained.
As far as raw grouping goes, here’s some example output when we run varnishlog -g raw:
1 Begin c sess 0 HTTP/1
3 Begin b bereq 2 fetch
5 Begin b bereq 4 fetch
4 Begin c req 2 esi
2 Begin c req 1 rxreq
