As part of my day job I am “the Varnish guy” and thus have to keep track of quite some servers for the development, QA, staging and production environment. Of course there are technical differences, yet I like to have “the one” Varnish configuration for all of them. For Varnish this means different backend and ACL definitions. So I created a template with the common configuration logic and a placeholder for these definitions. With the help of some perl magic the configuration and definition is put together and copied to the correct server. The perl script has to know which server belongs to which environment, but at least it’s all in one place and, thanks to the version control system of your choice, versioned and backed up.
Now let’s assume the following A/B side architecture: You have two Varnish servers #1 and #2 behind a load balancer. You also got a second load balancer for your, oh so many, application servers. This second load balancer provides two pools A and B (don’t ask, it’s politics. I do know that Varnish could load balance itself). Now the job of Varnish #1 would be to use the backend pool A while Varnish #2 would have to use backend pool B. This way you get to disable either one Varnish server and all connected clients will automatically and transparently reconnect thanks to HTTP.
To have one configuration logic yet different backends depending on where the configuration is executed you can implement a routine similar to this:
sub set_backend {
if (server.hostname ~ "(?i)^(stage)?hostname0[2468]$") {
set req.backend = pool_B;
}
else {
set req.backend = pool_A;
}
}
sub vcl_recv {
call set_backend;
}
It is as simple as querying the Varnish server’s hostname and deciding which backend to use. In this case the rule is that even-numbered hostnames are side B, while everything else is side A. No need to worry for any execution overhead this might cause, because Varnish configuration is compiled and linked on startup. When was the last time you needed to buy a new server because the old one had too little CPU power for your Varnish needs?