Skip to content

Performance

Caching without stale data

Date:

Aug 5, 2026

A cache makes a site fast until it shows someone yesterday’s price. Good caching is mostly about knowing exactly when to forget.

001

Know Every Layer

A single request can be answered by the browser, a CDN, the framework’s data cache, an application cache such as Redis, or the database itself. Each layer needs its own rule, and I write those rules down in one place.

When a bug report says “I still see the old version”, the first question is always which layer answered. Response headers that say where a response came from make that question quick to settle.

002

Choose Lifetimes Deliberately

Files with a content hash in their name never change, so they are cached for a year and marked immutable. Pages get a short shared lifetime with stale-while-revalidate, so visitors get an instant response while the cache refreshes in the background. Private or personalised responses are marked no-store.

ETags let a client ask whether its copy is still current and receive a tiny 304 Not Modified instead of the full response when nothing has changed.

003

Invalidate by Tag, Not by Guesswork

Expiry by time alone forces a trade-off between fresh and fast. Instead, each cached entry is tagged with the data it contains, such as a post or the project list, and those tags are purged the moment that data changes. Next.js supports this with revalidateTag, and many CDNs offer purging by tag or surrogate key.

The code that saves a record also clears its tags, in the same place. When invalidation lives on the write path, no update can forget it.

004

Test the Stale Path

I test that an edit appears on the live site within the expected time, that responses for signed-in users are never cached publicly, and that the site still works when the cache is down by falling back to the source instead of failing.

The hit ratio is worth watching too. A cache that is rarely hit is just one more thing that can serve the wrong answer.

More Articles

Keep reading