How to use varnish to pass data when the backend is ok && return the last normal beresq when the backend is down?

Mark Staudinger mark.staudinger at nyi.net
Tue Nov 22 17:00:15 CET 2016


On Mon, 07 Nov 2016 00:20:12 -0500, JackDrogon <jack.xsuperman at gmail.com>  
wrote:

> Hi, All:
> 	I need varnish to return data directly and update cache at every time  
> when the backend is ok. I also need varnish to return the last normal  
> beresq data from >the cache when the backend is down. How shuold I do  
> with varnish?
>
>> Thanks.

Hi Jack,

There are a couple of ways to approach this (as there almost always are  
with a tool as flexible as Varnish).  If traffic is generally light, you  
could get away with something like this:

in vcl_backend_response, set

beresp.ttl = 1ms;
beresp.keep = 0s;
beresp.grace = 1h; # or as long as you would want to serve from cache  
during an outage

The very short TTL should translate into no hits as long as you don't get  
a lot of requests within a 1ms window.   But this might not be exactly the  
behavior you want.

If you have a heavily loaded scenario where you would get multiple hits in  
this period, then you could simply put some logic into vcl_hit that looks  
at the backend health, and forces a cache miss if the backend is healthy,  
and otherwise honors the TTL/grace periods.  In this scenario it wouldn't  
matter as much what the TTL value is, but beresp.keep should stay set to  
0s.

in vcl_recv,

if ( std.healthy(req.backend_hint) ) {
    set req.http.allow-caching = "no";
} else {
    set req.http.allow-caching = "yes";
}

in vcl_hit

  if ( req.http.allow-caching == "no" ) {
     return (fetch);
  }
  //... otherwise, carry on with the rest of the normal vcl_hit comparisons  
of ttl, grace, and keep...

Of course these examples don't take into account the behavior of what to  
do with set-cookie and other headers on the response, but for simplicity  
I'm assuming here that it's ok to cache the responses as-is.  You could  
always strip headers out during vcl_deliver on the basis of the  
req.http.allow-caching header, or more generically on the obj.hits  
counter, if necessary.

Best,
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20161122/e3bedaee/attachment.html>


More information about the varnish-misc mailing list