Home About News Products Blog

Advanced Caching - Stale While Revalidate

caching
varnish

#1

When caching within section.io (leveraging Varnish Cache) its possible to use a little known feature to improve cache hit rates on infrequently requested items.

Examples where this solution can make a significant performance improvement are sites with a huge number of pages (eg an online bookstore) or sites that receive relatively low traffic volumes.

The way the solution works is that we allow content to stay in the cache after its expiry. When a request comes in for an expired (“stale”) item, The cache will serve up the old version to the first requester (in milliseconds) and in the background request back to the origin servers to get a fresh copy of the item - Which is then served from cache for every user after that.

This design pattern means that all users are always served from cache (the first user getting an older item) and hence can significantly improve site performance and cache hit rates.

Here is a code snippet to implement this solution within section.io:
https://gist.github.com/section-io-gists/a93af63a9e2d350bdb6962a974eba5f5