varnish4 vs varnish3 - grace behaviour

Legreg Accounts legreg.accounts at gmail.com
Tue Oct 31 14:17:33 UTC 2017


Hi all,

I'm currently working on a migration project from varnish3 to varnish4 and
I am facing a behaviour that I don't understand.

To put it simply, with the same configuration file used both in Varnish3
and Varnish4, I don't have the same results concerning hits and misses.

This seems to be related to the grace attribute, but I don't figure out how
it works.

So below is an example which describes the problem into details:

My varnish3 configuration:
# ----------------------------------------------
backend default {
    .host = "nginx";
    .port = "80";
}

sub vcl_fetch {
     set beresp.grace= 5s;
     set beresp.ttl = 1s;
}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
    set resp.http.X-Cache-Hits = obj.hits;
}
# ----------------------------------------------

My varnish4 configuration: (only change is the method name
vcl_backend_response instead of vcl_fetch )
# ----------------------------------------------
vcl 4.0;

backend default {
    .host = "nginx";
    .port = "80";
}

sub vcl_backend_response {
     set beresp.grace= 5s;
     set beresp.ttl = 1s;
}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
    set resp.http.X-Cache-Hits = obj.hits;
}
# ----------------------------------------------

The result of a little scenario which retrieves headers of a given resource
after some time:
# ----------------------------------------------
Time 14-47-12
VARNISH4:
X-Cache: MISS
X-Cache-Hits: 0
VARNISH3:
X-Cache: MISS
X-Cache-Hits: 0

Time 14-47-13
VARNISH4:
X-Cache: HIT
X-Cache-Hits: 1
VARNISH3:
X-Cache: MISS
X-Cache-Hits: 0

Time 14-47-20
VARNISH4:
X-Cache: MISS
X-Cache-Hits: 0
VARNISH3:
X-Cache: MISS
X-Cache-Hits: 0
# ----------------------------------------------

So as you can see in the scenario, for the first request, both v3 and v4
return a MISS, which is normal.
But one second after, the second request returns a MISS for varnish3, which
is normal for me, and a HIT for varnish4, that I don't really understand.
As I suspected this was related to the grace parameter, I have added a
third request in my scenario 7 seconds later ( greeter than 1sec for  cache
plus 5 seconds for grace), and as expected, both varnish 3 and varnish 4
are MISS.

So could you help me to understand / workaround this problem ?
The objective for me is to get the same result with varnish4 than with
varnish3 (while I'm migrating from 3 to 4 ;) ).
My current workaround is to set beresp.grace=1ms on varnish4, but I don't
like that at all, and I can't do that on every of my configurations :(

Any help would be very appreciated !

Thanks in advance ! :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20171031/d1c1ebda/attachment-0001.html>


More information about the varnish-misc mailing list