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 systemd (or older init.d scripts)#
On any modern system (Varnish 4.1+ running under systemd), the easiest reload is through the service manager. This compiles the new VCL and makes Varnish use it, without dropping the cache.
$ systemctl reload varnish
On older sysvinit systems the equivalent was /etc/init.d/varnish reload. Note that not all init.d scripts had a reload option, and it could be disabled via the sysconfig settings if RELOAD_VCL was turned off.
$ /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 reload script (if you use the official RPM/Deb repos). On current packages it’s called varnishreload; on the older CentOS 6 / Debian 7 era packages it was varnish_reload_vcl. Either way, it makes Varnish load the configured VCL file again into memory.
The output below is from the original varnish_reload_vcl script, but varnishreload behaves the same way:
$ 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
Each of those names can be activated with vcl.use.
$ varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 vcl.use varnish_1418585083