Reload Varnish VCL without losing cache data

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, December 14, 2014

Follow me on Twitter as @mattiasgeniar

You can reload the Varnish VCL configuration without actually restarting Varnish. A restart would stop the varnishd process and start it anew, clearing all the cache it has built up in the meantime. But you can also reload the varnish configurations, to load your new VCL without losing the cache.

Beware though, there are times when you want to clear your cache on VCL changes: for instance, healthchecks on backend definitions can get pretty funky when a reload of the VCL would modify their IPs, and there are situations where when you change the vcl_hash routine, a restart is advised since the data in memory would never be used again (because of a hash-change). Having said that, there are plenty of reasons to reload a Varnish VCL cache without losing the data in memory.

Via init.d scripts

Not all init.d scripts have a reload option, and it can be disabled with the sysconfig settings if the RELOAD_VCL option is turned off, but if it’s enabled, this by far the easiest way.

$ /etc/init.d/varnish reload

This will reload the VCL, compile it and make Varnish use the new version.

Via the Varnish Reload VCL script

Varnish ships with a command called varnish_reload_vcl (if you use the official RPM/Deb repos). You can use this via the CLI to make Varnish load the default.vcl file again into memory (assuming that is the VARNISH_VCL_CONF defined in the /etc/sysconfig/varnish file).

$ varnish_reload_vcl
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2014-12-14T20:19:16
VCL compiled.

available      11 boot
active          0 reload_2014-12-14T20:19:16

Done

The new Varnish VCL is loaded, without losing in-memory data.

Via a custom script

The varnish_reload_vcl is essentially a Bash-script, you can view the contents by looking into /usr/bin/varnish_reload_vcl. If you want to write something similar into your own scripts, here’s a stripped/easier version of that script. It uses varnishadm to load the file and eventually switch the config to use it.

#!/bin/bash

# Generate a unique timestamp ID for this version of the VCL
TIME=$(date +%s)

# Load the file into memory
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 vcl.load varnish_$TIME /etc/varnish/default.vcl

# Active this Varnish config
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 vcl.use varnish_$TIME

At any time, you can view the in-memory available Varnish VCL files using the vcl.list command. You can load/active one with the vcl.use command and you can compile a new one with vcl.load.

To view the available ones, run the following command.

$ varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 vcl.list
available       8 boot
available       0 varnish_1418585083

Eeach of those names can be activated with vcl.use.

$ varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 vcl.use varnish_1418585083


Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.