Chapter 5. Invalidating the Cache

In this chapter I’ll highlight several cache invalidation strategies. These strategies allow you to remove certain items from cache even though their time-to-live hasn’t expired yet.

In the world of caching, there’s only one thing worse than a low hit rate, and that’s caching for too long. That statement sounds quite weird, right? Here I am trying to convince you to cache everything, all the time, yet I’m saying that caching for too long is the worst thing to do. Allow me to explain.

Caching for Too Long

Throughout this book, I’ve always kept the best interests of the website owner, developer, and sysadmin in mind. The reality is that the site, API, and application are primarily services that the end user consumes. In the end, it’s all about the end user.

  • Why do we want to make the site fast? For the user!

  • Why do we want to keep the site available? For the user!

  • Why do we cache? So that the user has a good experience!

Caching data for too long would mess with the integrity of the data, giving the user a bad experience when up-to-date output is important. This is especially the case for news websites.

We already talked about the use of Cache-control and Expires headers. It’s important to estimate the right time-to-live and set the right values for these headers. The more accurate the time-to-live, the better the balance.

Unfortunately, in many cases the data will be out-of-date even before the object expires. Setting it to a lower value ...

Get Getting Started with Varnish Cache now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.