Troubleshooting Varnish

Varnish is incredibly stable, but that doesn’t mean troubleshooting doesn’t have to take place. On rare occassions crashes may occur. Based on the available server resources and traffic patterns, misconfigured runtime parameters may cause unwanted behavior.

Crashes rarely occur, but when they do, you need to figure out what’s going on.

Even when Varnish is stable, the hit rate of the cache might be too low for your liking.

There are many ways to perform troubleshooting: from panic inspection, to Varnish Shared Memory Logs (VSL) , to monitoring metrics.

Restarting and reloading#

“Have you tried turning it off and on again?” is a very common question asked by IT support staff and quite often it turns out to be an effective measure.

However, restarting is often considered lazy problem solving because it lacks the ambition inspect the root cause. But when you’ve run out of options or out of time, a quick restart can be considered.

Restart#

Here’s how you can restart Varnish:

sudo systemctl restart varnish

This command will kill the varnishd runtime process and start it up again. Any runtime parameters that weren’t persisted in the systemd service file will be reset.

Reload#

Performing a Varnish reload will only reload the VCL configuration and has no impact on the state of the varnishd main or child processes.

Here’s how you reload the VCL configuration:

sudo systemctl reload varnish

The systemd reload will call the varnishreload script that will gracefully add a new VCL configuration, while discarding older configurations.

Varnish panics#

When the Varnish child process crashes a panic is triggered and the child process is restarted. The panic.show CLI command will return the last panic.

Here’s how to return the last panic:

sudo varnishadm panic.show

An example panic#

Here’s an example of a panic that we triggered ourselves through the varnishadm debug.panic.worker command:

$ sudo varnishadm panic.show
Panic at: Mon, 03 Jan 2022 13:16:43 GMT
Assert error in ccf_panic(), cache/cache_panic.c line 725:
  Condition((strcmp(\"\", \"You asked for it\")) == 0) not true.
version = varnish-6.0.8 revision 97e54ada6ac578af332e52b44d2038bb4fa4cd4a, vrt api = 7.1
ident = Linux,5.10.76-linuxkit,aarch64,-junix,-sdefault,-sdefault,-hcritbit,epoll
now = 11645.069782 (mono), 1641215803.491569 (real)
Backtrace:
  0xaaaaca50fcd0: /usr/sbin/varnishd(+0x4fcd0) [0xaaaaca50fcd0]
  0xaaaaca57c5f4: /usr/sbin/varnishd(VAS_Fail+0x24) [0xaaaaca57c5f4]
  0xaaaaca50e474: /usr/sbin/varnishd(body_status_2str+0) [0xaaaaca50e474]
  0xaaaaca57ea1c: /usr/sbin/varnishd(+0xbea1c) [0xaaaaca57ea1c]
  0xaaaaca57ee54: /usr/sbin/varnishd(VCLS_Poll+0x2d0) [0xaaaaca57ee54]
  0xaaaaca4f1e28: /usr/sbin/varnishd(CLI_Run+0x58) [0xaaaaca4f1e28]
  0xaaaaca50ad10: /usr/sbin/varnishd(child_main+0x1a0) [0xaaaaca50ad10]
  0xaaaaca54d6c4: /usr/sbin/varnishd(+0x8d6c4) [0xaaaaca54d6c4]
  0xaaaaca57ea1c: /usr/sbin/varnishd(+0xbea1c) [0xaaaaca57ea1c]
  0xaaaaca57ee54: /usr/sbin/varnishd(VCLS_Poll+0x2d0) [0xaaaaca57ee54]
thread = (cache-main)
thr.req = (nil) {
},
thr.busyobj = (nil) {
},
vmods = {
},

The error message, the filename, the line number and the backtrace are the tools you have to figure out what caused the panic and how to potentially solve it.

You can get help when you cannot figure these panics out on your own.

Don’t forget that panic.show only returns the last panic. If multiple panics occur, the one you’re trying to debug may be overwritten.

Debug mode#

The varnishd program has a -d flag that enables debug mode.

When enabling debug mode, the parent process will run in the foreground with a CLI connection on stdin/stdout. The child process must be started explicitly with a CLI command. Terminating the parent process will also terminate the child.

Enabling debug mode#

Here’s an example where the -d debug flag is added to varnishd:

$ varnishd -f default.vcl -d
Warnings:
VCL compiled.

Debug: Version: varnish-6.0.8 revision 97e54ada6ac578af332e52b44d2038bb4fa4cd4a
Debug: Platform: Linux,5.10.76-linuxkit,aarch64,-junix,-sdefault,-sdefault,-hcritbit
200 318
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,5.10.76-linuxkit,aarch64,-junix,-sdefault,-sdefault,-hcritbit
varnish-6.0.8 revision 97e54ada6ac578af332e52b44d2038bb4fa4cd4a

Type 'help' for command list.
Type 'quit' to close CLI session.
Type 'start' to launch worker process.

start
Debug: Child (927) Started
200 0

Info: Child (927) said Child starts

Please note that the start command had to be manually executed to start the child process. Without a running child process, Varnish cannot accept incoming requests.

When to use debug mode#

An occasional Varnish panic can be caught using the panic.show CLI command, as mentioned earlier. When the panic occurs, the child process will restart and request handling continues.

If multiple panics occur, the last panic will be overwritten and the original panic you’re trying to investigate will no longer be available through panic.show. To prevent this information from being lost, debug mode can be used.

Debug mode is very similar to running the varnishd program in the foreground via the -F runtime parameter. The main difference is that a CLI is available and that the child process is not automatically started.

Thanks to the CLI being available, you can run additional commands while debugging. These commands include:

  • param.show, param.set and param.reset to show, set, and reset runtime parameters
  • stop and start to stop and start the varnishd child process
  • vcl.load, vcl.use and vcl.discard to load, activate and remove VCL configurations

Please do not enable debug mode an a production system, as the child process will not automatically start.

The Guru Meditation error#

The Guru Meditation errors that Varnish returns are well-known, but usually have nothing to do with problems within Varnish.

As you can see in the example below, the Guru Meditation error reports that Varnish couldn’t reach the backend:

<!DOCTYPE html>
<html>
  <head>
    <title>503 Backend fetch failed</title>
  </head>
  <body>
    <h1>Error 503 Backend fetch failed</h1>
    <p>Backend fetch failed</p>
    <h3>Guru Meditation:</h3>
    <p>XID: 123456</p>
    <hr>
    <p>Varnish cache server</p>
  </body>
</html>

The output format for these types of errors and the fact that the term Guru Meditation is used, is all part of the built-in VCL for the vcl_synth and vcl_backend_error subroutines.

The vcl_synth subroutine is reponsible for synthetic responses that are returned in the client-side logic of Varnish. For errors that happen at the backend-side, the vcl_backend_error subroutine is used.

Client-side synthetic responses#

If you want to intercept Varnish Shared Memory Logs (VSL) for client-side synthetic responses, you can run the following command:

sudo varnishlog -g request -q "VCL_call eq 'SYNTH'"

Client-side transactions are VSL transactions that refer to VCL execution that takes place on the client-side. This means that no interaction with the backend takes place.

If you want to intercept VSL transactions for synthetic responses that return 400-range status codes, you can run the following command:

sudo varnishlog -g request -q "VCL_call eq 'SYNTH'\
 and RespStatus >= 400 and RespStatus < 500"

This will ensure that non-erroneous synthetic responses are not part of the output.

These commands will present a VSL transaction every time a client-side Guru Meditation error takes place in real time.

If you want to dump all the old transactions in the VSL memory buffer that match these criteria, you can add the -d parameter to the command. Here’s an example:

sudo varnishlog -d -g request -q "VCL_call eq 'SYNTH'\
 and RespStatus >= 400 and RespStatus < 500"

The varnishd program reserves 80 MB of memory for the Varnish Shared Memory Log (VSL) under the form of a circular memory buffer. As soon as this buffer is full, the oldest entries are overwritten. By using the -d parameter, you dump the full content of the VSL buffer.

However, there is no guarantee how long you can go back in time by dumping the buffer. It all depends on the traffic patterns of your Varnish server.

You can increase the size of the VSL circular memory buffer by overriding the vsl_space runtime parameter which defaults to 80 MB.

VSL output example#

Here’s a VSL example for a request that resulted in a synthetic response with a 400-range status code:

$ sudo varnishlog -g request -q "VCL_call eq 'SYNTH'\
 and RespStatus >= 400 and RespStatus < 500"
*   << Request  >> 8
-   Begin          req 7 rxreq
-   Timestamp      Start: 1641228924.312547 0.000000 0.000000
-   Timestamp      Req: 1641228924.312547 0.000000 0.000000
-   ReqStart       172.17.0.1 55068 a0
-   ReqMethod      PRI
-   ReqURL         /
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: localhost
-   VCL_call       RECV
-   VCL_return     synth
-   VCL_call       HASH
-   VCL_return     lookup
-   Timestamp      Process: 1641228924.312633 0.000086 0.000086
-   RespProtocol   HTTP/1.1
-   RespStatus     405
-   RespReason     Method Not Allowed
-   RespReason     Method Not Allowed
-   RespHeader     Date: Mon, 03 Jan 2022 16:55:24 GMT
-   RespHeader     Server: Varnish
-   RespHeader     X-Varnish: 8
-   VCL_call       SYNTH
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     Retry-After: 5
-   VCL_return     deliver
-   RespHeader     Content-Length: 272
-   Storage        malloc Transient
-   RespHeader     Connection: keep-alive
-   Timestamp      Resp: 1641228924.312984 0.000437 0.000351
-   ReqAcct        73 0 73 204 272 476
-   End

From these logs you can conclude that an HTTP 405 Method Not Allowed error was returned because the invalid PRI request method was used.

This behavior is part of the built-in VCL for vcl_recv that prevents the PRI request method from being used.

You can slim down the output by only selecting the tags you need:

$ sudo varnishlog -i RespStatus -i RespReason -g request -q "VCL_call eq 'SYNTH'
           and RespStatus >= 400 and RespStatus < 500"
*   << Request  >> 8
-   RespStatus     405
-   RespReason     Method Not Allowed
-   RespReason     Method Not Allowed

Backend errors#

When the Guru Meditation error returns output like 503 Backend fetch failed, the vcl_backend_error subroutine is called because the backend fetch failed.

This is not client-side logic based on faulty input from the user, but backend logic for a request that was not served from the cache. In the execution flow the vcl_backend_fetch and the vcl_backend_response subroutines are responsible for fetching a response from the backend and storing it in the cache.

When things go wrong, the synthetic error is returned, but you can also catch these errors in the Varnish Shared Memory Logs (VSL) using the following command:

sudo varnishlog -g request -q "VCL_call eq 'BACKEND_ERROR'"

VSL output example#

Here’s a VSL example where the backend fetch fails and a backend error is returned:

$ sudo varnishlog -g request -q "VCL_call eq 'BACKEND_ERROR'"
*   << Request  >> 32790
-   Begin          req 32789 rxreq
-   Timestamp      Start: 1641287864.851443 0.000000 0.000000
-   Timestamp      Req: 1641287864.851443 0.000000 0.000000
-   ReqStart       172.17.0.1 55092 a0
-   ReqMethod      GET
-   ReqURL         /
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: localhost
-   VCL_call       RECV
-   VCL_return     hash
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 32791 fetch
-   Timestamp      Fetch: 1641287864.852788 0.001346 0.001346
-   RespProtocol   HTTP/1.1
-   RespStatus     503
-   RespReason     Backend fetch failed
-   RespHeader     Date: Tue, 04 Jan 2022 09:17:44 GMT
-   RespHeader     Server: Varnish
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     Retry-After: 5
-   RespHeader     X-Varnish: 32790
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.0)
-   VCL_call       DELIVER
-   VCL_return     deliver
-   Timestamp      Process: 1641287864.852818 0.001375 0.000030
-   RespHeader     Content-Length: 282
-   RespHeader     Connection: keep-alive
-   Timestamp      Resp: 1641287864.852978 0.001535 0.000160
-   ReqAcct        73 0 73 250 282 532
-   End
**  << BeReq    >> 32791
--  Begin          bereq 32790 fetch
--  VCL_use        boot
--  Timestamp      Start: 1641287864.851951 0.000000 0.000000
--  BereqMethod    GET
--  BereqURL       /
--  BereqProtocol  HTTP/1.1
--  BereqHeader    Host: localhost
--  BereqHeader    Accept-Encoding: gzip
--  BereqHeader    X-Varnish: 32791
--  VCL_call       BACKEND_FETCH
--  VCL_return     fetch
--  FetchError     backend boot.default: fail errno 111 (Connection refused)
--  Timestamp      Beresp: 1641287864.852372 0.000422 0.000422
--  Timestamp      Error: 1641287864.852377 0.000426 0.000004
--  BerespProtocol HTTP/1.1
--  BerespStatus   503
--  BerespReason   Service Unavailable
--  BerespReason   Backend fetch failed
--  BerespHeader   Date: Tue, 04 Jan 2022 09:17:44 GMT
--  BerespHeader   Server: Varnish
--  VCL_call       BACKEND_ERROR
--  BerespHeader   Content-Type: text/html; charset=utf-8
--  BerespHeader   Retry-After: 5
--  VCL_return     deliver
--  Storage        malloc Transient
--  Length         282
--  BereqAcct      0 0 0 0 0 0
--  End