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;

This isn't possible, as far as I'm aware. Only cached items can be served from grace. You might be able to do it by modding Varnish, by writing C code into the vcl.

The varnish docs are very exhaustive, diving into them might help as well: https://www.varnish-cache.org/docs

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.