Cache Invalidation Explained: How Systems Keep Cached Data Fresh

Cache invalidation explained in plain terms means understanding how a system knows when a cached copy has gone stale. The central question is how a cache can stay fast without ever serving an answer that is no longer true, and the answer is invalidation: a rule for deciding when a cached entry must be thrown away or refreshed.

Cache invalidation is the process of removing or updating cached data when the underlying data changes, so that later requests see the new value instead of the old one. It is famously one of the two hard problems in computer science, because the cache and the source of truth are separate, and keeping them in step is never free.

The idea is simple: a cache is only useful if it can be trusted, and trust comes from knowing exactly when an entry stops being true. This article explains what cache invalidation is, how it works, and where it is used.

Key takeaways

  • Cache invalidation removes cached entries when the underlying data changes.
  • A stale cache serves old answers, which is worse than no cache at all.
  • Time-based expiry is simple; event-based invalidation is precise.
  • The right strategy depends on how much staleness a system can tolerate.
  • Invalidation must work across every cache in a distributed system.

What cache invalidation is

A cache holds copies of data that is expensive to fetch, such as a database row, a rendered page, or the result of a calculation. Invalidation is the act of marking a cached copy as unusable, so that the next request fetches the fresh value and stores a new copy. Without it, a cache would keep serving the first answer it ever stored, however old.

The difficulty is that the cache does not know when the source data changes. The change may happen in another service, another data centre, or a database the cache never talks to, so the system must either guess, by expiring entries after a fixed time, or be told, by an event that says what changed.

The classic formulation comes from Phil Karlton, who said there are only two hard things in computer science: cache invalidation and naming things. The joke survives because it is true: every invalidation strategy trades freshness against complexity, and none of them is free. Our guide to caching explains the cache itself, and our guide to Redis explains the in-memory store where many caches live.

How cache invalidation works

Time-to-live, or TTL, is the simplest strategy. Each cached entry is given a lifetime, such as sixty seconds, and after it expires the next request fetches fresh data. TTL is easy to reason about and needs no coordination, but it serves stale data for up to the TTL, and it refetches data that has not changed.

Event-based invalidation is precise. When data changes, the writer publishes an event naming what changed, and every cache that holds it drops the entry. This keeps caches fresh to the second, but it needs a reliable event channel, and a missed event leaves a stale entry until its TTL saves it.

Write-through caching avoids invalidation altogether for the writer’s own cache. Every write goes through the cache as well as the store, so the cached copy is always the latest value. The cost is that every write pays the price of updating the cache, even for data nobody will read again.

Versioning makes staleness impossible to serve by accident. Each cached entry carries the version of the data it came from, and a reader checks the version before using it. The check costs a round trip, but it catches a stale entry even when the invalidation event was lost.

In a distributed system, invalidation must reach every cache. A single server can drop its own entries, but a fleet of servers needs a broadcast, a shared cache, or a TTL short enough that a missed invalidation is harmless. Most real systems combine all three: events for speed, a shared cache for consistency, and a TTL as a safety net.

A dark server room with a glowing green cache shelf, one faded entry being lifted away while fresh ones glow behind it
A dark server room with a glowing green cache shelf, one faded entry being lifted away while fresh ones glow behind it, shows how cache invalidation works: throw away what is stale, keep what is still true.

Comparing cache invalidation strategies

Cache invalidation is one of several ways to keep cached data fresh, and each was built with a different emphasis. A short TTL is simple but wasteful, event-based invalidation is precise but complex, and write-through caching is exact but slow, and each approach trades freshness for simplicity in a different way.

The table below sets out the main contrasts between the leading approaches.

Aspect TTL expiry Event-based invalidation Write-through
How it works Entries expire after a fixed time Changes publish an event that drops entries Writes update the cache too
Freshness Stale for up to the TTL Fresh within milliseconds Always fresh
Complexity Very low High, needs an event channel Moderate
Cost when idle Refetches unchanged data Nothing, until a change Every write pays
Best for Data that changes slowly Data that must be exact Small, hot data sets

The practical result is that TTL suits data where a minute of staleness is harmless, event-based invalidation fits data that must be exact, and write-through works when the data set is small and hot.

Where cache invalidation is used

Content delivery networks invalidate cached pages when content changes. A news site that publishes a correction must purge the old page from edge caches worldwide, or readers will keep seeing the error, so CDNs offer purge APIs that drop a URL from every edge server at once.

Web applications invalidate rendered pages and fragments. When a user edits their profile, the application drops the cached copy of that page, so the next visit shows the new details, while the cached copies of unchanged pages stay in place.

Databases invalidate query caches when tables change. A cached result for a query is dropped as soon as any row it depends on is written, which keeps answers correct at the cost of re-running queries that might not have changed.

Microservices invalidate each other’s caches with events. When the pricing service changes a price, it publishes an event, and every service that cached that price drops it, so no service keeps selling at the old price.

Browsers invalidate their own caches with headers. A server tells the browser how long a file may be cached, and a new version of the file gets a new name, so the browser fetches it instead of serving the old one from its cache.

Strengths and limits

The greatest strength of good invalidation is trust. A cache that is known to be fresh can be used everywhere, which lets a system serve most requests from memory and keep its database idle, and that is where the real performance of a large system comes from.

It is also a strength that invalidation is a separate concern from caching itself. The same cache can use TTL for one kind of data and events for another, so each kind of data gets the freshness it needs without paying for more.

The limits are real, though. Precise invalidation adds moving parts, a missed event means stale data, and a cache that invalidates too eagerly is no better than no cache at all. The hardest failures are the silent ones, where the cache keeps serving old data and nobody notices.

Cache invalidation checklist

  • Decide how much staleness each kind of data can tolerate before choosing a strategy.
  • Use a TTL as a safety net even when you invalidate by event.
  • Name cache keys after the data they hold, so invalidation can target them.
  • Publish an event on every write that changes cached data.
  • Purge by pattern, not one key at a time, when a change affects many entries.
  • Test what happens when an invalidation event is lost.
  • Monitor cache hit rates; a sudden drop often means over-invalidation.
  • Version cached entries when a reader must never see stale data.
  • Keep invalidation code beside the write code, not in a separate system.

Frequently asked questions

What is cache invalidation in simple terms?

It is removing or refreshing cached data when the original data changes, so the cache never serves an answer that is no longer true.

Why is cache invalidation called hard?

Because the cache and the source of truth are separate, and the cache cannot see changes that happen elsewhere unless it is told or it guesses with a TTL.

What is a TTL?

It is a time-to-live: the lifetime given to a cached entry, after which it expires and the next request fetches fresh data.

What is the difference between invalidation and expiry?

Expiry happens on a timer, whatever the data does, while invalidation happens because the data changed, whenever that is.

What happens if an invalidation event is lost?

The stale entry stays until its TTL expires, which is why most systems combine events with a TTL as a safety net.

When should I not cache at all?

When the data changes constantly, when freshness must be exact and events are unreliable, or when the cost of a wrong answer exceeds the cost of fetching fresh data.

The bottom line

Cache invalidation explained in one line is this: know when a cached copy stops being true, and throw it away before it is served.

It is the discipline that makes caching trustworthy, and the reason a fast system can also be a correct one.

This article is general information about how cache invalidation works, not advice about designing caching for a particular system.

Latest articles

Scroll to Top