Say I have a cached response (web cache, full html) that I want to update every 30m. The thing is that after this 30mins, I want to serve the first next request the "old" cached version, and then in the background flush the cache for that request and re-do it, so that next requests will get an updated version.

For example: I have a cached version of /whatever.php that was cached at 15:00h. Then before 15:30h every request to /whatever.php will receive that cached html, and what I want is that the first request after 15:30h (even if it's at 18:00 or the next day) receives that same cached version and then runs a thread in the background or something to update the cache.

I assume this can be achieved by having an infinite lifetime cache and using a header that forces the cache to update, so then the cache always serves the cached version unless the request forces to update it, but then my problem is: how to take care of time limits in this scenario and how to make a background request (curl or whatever) from a cache-manager server?

Is there any cache technology that would allow me to do something like this?

  • 1
    I don't understand what you're trying to achieve and solve here. -- If your back-end is too slow/overloaded to provide a timely response when a request is made to your cache for an object that isn't there (regardless of whether it expired and was removed from the cache, or never cached in the first place) then that is the probably the problem you need to solve. Keeping stale content and refreshing it only after the stale object was requested and served to does not seem an effective strategy. – HBruijn May 21 at 9:43
  • @HBruijn I don't see why it's not a good strategy, it assures that no client will ever have to wait, which is one of the purposes of having a cache. In this specific case the backend is a really heavy wordpress so I don't mind if a user does not get the last version as long as she gets it quick. That said, we're also working on the velocity of the backend itself, but I thought this was an idea that worth exploring. – Carlos Navarro Astiasarán May 21 at 15:25
  • Varnish cache does support almost exactly what you want though: varnish-cache.org/docs/trunk/users-guide/vcl-grace.html – HBruijn May 23 at 21:12

After your comment I think I'll rephrase your question to:

How do I ensure that my cache will always contain all objects and there will be no cache misses?

That is commonly called "cache warming".

Rather than relying on the (first) site visitors and the popularity of your site to keep the cache populated, you yourself will (regularly and repeatedly) request every page and object by crawling your own site.

That assumes that your cache has enough memory to cache all objects and that crawling your site completes before the first objects will already begin to expire from the cache.

One strategy to make that more feasible on a larger site would be to set long(er) cache expiry times from the back-end server. That will prevent objects from expiring too quickly, reduce the amount of crawling that needs to be done and will reduce the load on your back-end server.
(Considering the remark below you may want to ensure that the cache will return cached objects with much shorter cache expiry times to your site visitors then what your back-end server set for the cache server, so that the visitors will still refresh content sufficiently frequently. )

Once you achieve your first goal and all objects will always be loaded in the cache and you have quick response times, the next hurdle is probably that you updates made to your back-end content will take too long to become visible for your visitors...

Ideally you want to selectively expire and update specific objects in your cache immediately after they get updated on the back-end server, rather than either waiting for them to expire normally or completely emptying your cache and re-populating it from scratch.

Varnish and others provide an support specifically for that, typically called an HTTP PURGE request.

Send a PURGE request followed by a normal GET request and the object will be refreshed in your cache. And there are even already specific plugins that will do that automatically for Wordpress as well whenever a new article is published or an existing one modified (i.e. https://wordpress.org/plugins/tags/varnish/ )

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.