3

I have added Varnish 4.0 to a CentOS 6 server running cPanel to cache a website I have developed. I am trying to implement stale-while-revalidate to ensure that all users are served a cached version of a page, so that if the max-age of 2 minutes has expired returning a stale copy of the page would be sufficient for a 3 month period, and the server would regenerate the cache object if necessary in the background.

An example set of response headers being returned are as follows:

Accept-Ranges:bytes
Age:539
Cache-Control:public, max-age=120, stale-while-revalidate=7889220
charset:utf-8
Connection:keep-alive
Content-Length:54511
Content-Type:text/html; charset=utf-8
Date:Mon, 14 Sep 2015 12:52:00 GMT
Expires:Mon, 14 Sep 2015 12:54:01 GMT
grace:7889220s
Pragma:cache
X-Cache:HIT
X-Cache-Hits:2

The mechanism appears to work to a point. In the case above, a cached copy is returned, and the next request correctly returns a copy where the Age is set to 10, for example (ie. 10 seconds have passed since the cache was reset in the background). However, at some point in the future (in the order of hours), a request to the same page results in a cache miss and the page appears to hit the back-end directly, with the response headers indicating an Age of 0. I can't tell why a stale copy is not being returned.

I'm not experienced with Varnish, and I'm using a version of a sample Varnish 4.0 template by Mattias Geniar, modified for Craft CMS (a PHP CMS on which the site is built).

Is there anything in the VCL file which may be causing the cache misses, or is this likely to be a server configuration problem?

2
  • 1
    Do you set the grace value? In the example template you linked it seems to be commented out.
    – Bazze
    Sep 23, 2015 at 11:23
  • Can you post the relevant configuration? Varnish 4 has separated the client request and backend fetch. The default behavior is to quickly serve object from memory (within the grace period) then go refreshing it. But this means that the client can get older content (when Age > Max-Age). This is exacerbated on test systems where only a few user generates request so an object (with large grace period) could be months old as no one requested it, so the users have to be trained to do those request twice (and the second will be fresh).
    – karatedog
    Apr 21, 2016 at 7:48

0

You must log in to answer this question.

Browse other questions tagged .