Varnish Saint Mode is a lesser known gem inside varnish that lets you serve stale content from cache, even when your backend servers are unavailable.
This article explains how to configure varnish to take advantage of this feature. If you want to follow along, create a directory and add an index.html
. I am going to use a poor man’s Python web server python -m SimpleHTTPServer
to serve this directory.
Here is a simple Varnish config to take advantage of this feature:
The following scenario will let you test the saint mode:
- Kick-off the Python web server in the directory you created with
python -m SimpleHTTPServer
. Note: Every 1s varnish is going to probe your backend to determine if it is healthy.
Serving HTTP on 0.0.0.0 port 8000 ...
127.0.0.1 - - [17/Feb/2014 11:51:02] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [17/Feb/2014 11:51:02] "GET / HTTP/1.1" 200 -
- Fetch the
index.html
you created earlier through Varnish
- Now kill the Python web server process and confirm it is down
- Fetch
index.html
through Varnish again
Note the Age: 127
header. This tells you the number of seconds since Varnish fetched this page from your web server.
Congratulations, you’re now serving pages without your webserver! Varnish’s saint mode can be a nice safety net when the unexpected (but inevitable) happens and buy you some time to get things back into working order.