I want to configure varnish (still on version 3) in the following way:
If the backend is available, every request should go to the backend, so no cached results should be used. If the backend is unavailable (so not healthy), I want to use the cached results for the next day. If after one day the backend is still unhealthy, an error should be shown.
I tried the following configuration using the grace mechanism, but this doesn't work: Although all requests are going to the backend (so no cached answers if the backend is available), in the case of a failure, I instantly get an Varnish Error (Unavailable) instead of Varnish using the cached data. What am I doing wrong?
sub vcl_recv {
if (req.backend.healthy) {
# No caching if backend is healthy
set req.grace = 1s;
} else {
# Otherwise accept requests up to 1 day old
set req.grace = 1d;
}
}
sub vcl_fetch {
# Cached requests are not valid at all
set beresp.ttl = 1s;
# Keep requests for ttl + 1 d
set beresp.grace = 1d;
}