Black Friday: 30% off every plan through November 30. See the prices
SysDesignPrep.com
Study guide

Updated 24 October 2026

Cache invalidation strategies

Keeping caches correct when data changes: TTLs, delete on write, write-through and write-behind, the race conditions that leave stale data, versioned keys, invalidation from change data capture, leases against stampedes, and multi-layer caches.

Reading is half of it. Now try it in an interview.An AI interviewer asks you to design a Distributed Cache, out loud or typed, and scores the answer for your level. Or watch a sample interview first, free with no sign-up to start.
Start an AI mock

Try it yourself, in a playground:LRU, LFU and FIFO cache evictionCache stampedeCDN caching and hit ratioCache write policies

"There are only two hard things in computer science: cache invalidation and naming things." Adding a cache is easy; keeping it from serving stale or wrong data after updates is the hard part. Interviewers who hear "we add a Redis cache" often follow with "what happens when the data changes?" This guide gives you the patterns, the races that break them, and the fixes.

Start with how stale is acceptable

DataTolerable stalenessApproach
Static assets, immutable mediaforeverversioned URLs, no invalidation
Popular lists, recommendations, countsseconds to minutesTTL only
Profiles, product pagessecondsinvalidate on write plus TTL as safety net
Prices, inventory, permissionsnone at decision timeread source of truth for decisions; cache only for display
Balances, paymentsnonedo not cache, or cache with versioned reads

Answering this first often makes invalidation trivial.

TTL only

Every entry expires after a fixed time. Simple, self-healing, and bounded staleness. Add jitter to TTLs so many keys do not expire at the same moment. Always set a TTL, even when you also invalidate explicitly, as a safety net for missed invalidations.

Delete on write (cache-aside)

The application updates the database, then deletes the cache key; the next read misses and reloads fresh data. Deleting is safer than updating the cache with the new value, because concurrent writers updating the cache can leave it with an older value.

Even delete-on-write has a race:

  1. Reader A misses the cache and reads the old value from the database.
  2. Writer B updates the database and deletes the cache key.
  3. Reader A writes the old value into the cache.

The stale value now lives until its TTL. Fixes:

  • Leases: on a miss, the cache gives the reader a lease token; a delete invalidates outstanding leases, so A's late write is rejected. (Facebook's Memcache paper describes this.)
  • Versioned values: store the row's version with the cached value and refuse to overwrite a newer version with an older one.
  • Delayed second delete: delete again shortly after the write. Crude, but narrows the window.
  • Short TTLs to bound the damage.

Write-through and write-behind

  • Write-through: writes go to the cache and the database together (often through a cache layer that writes to the database). Reads after writes are fresh; every write pays both costs.
  • Write-behind (write-back): writes go to the cache, which flushes to the database asynchronously. Very fast writes, but data can be lost if the cache fails before flushing; use only where that is acceptable (counters, analytics).

See caching.

Invalidation from the database log

Instead of every code path remembering to invalidate, subscribe to the database's change stream: change data capture emits each committed change, and an invalidator deletes or refreshes the affected keys. Benefits: no missed invalidations from scripts or other services, ordering per row, and invalidation only after commit. The cost is a short delay (typically well under a second).

Versioned keys

Embed a version in the key (user:42:v17) and store the current version somewhere cheap (or in the parent object). Updating means incrementing the version; old keys are simply never read again and expire. Works well for derived data with many keys (all of a user's rendered fragments), since one version bump invalidates them all. The same idea underlies fingerprinted asset URLs. See HTTP caching.

Stampedes on invalidation

Invalidating a hot key sends every request to the database at once. Protect with:

  • Request coalescing: one request recomputes; others wait for its result.
  • Stale-while-revalidate: keep serving the old value while one background refresh runs.
  • Early probabilistic refresh: refresh before expiry with increasing probability as expiry approaches.

See hot keys and skew.

Multi-layer caches

Browser, CDN, application in-process cache, distributed cache, database buffer cache: each layer needs an invalidation story. In-process caches across hundreds of servers are hardest; use short TTLs or broadcast invalidations via pub/sub, and accept a few seconds of inconsistency between servers. CDN layers use purges or versioned URLs. See CDN and edge.

Derived and aggregated data

Caches of computed results (feeds, search results, counts) depend on many rows. Rather than invalidating precisely, recompute on a schedule or on events, accept short staleness, and patch in the user's own changes for read-your-writes. See fan-out on write vs read and consistency models.

In the interview

"Cache-aside with delete on write, invalidations driven by CDC so no path is missed, a TTL of five minutes as a safety net, leases to prevent stale backfills, and request coalescing for hot keys. Prices are read from the database at checkout regardless of the cache." See Design a Distributed Cache.

Checklist

  • Staleness tolerance decided per data type.
  • TTLs with jitter on everything.
  • Delete rather than update on write; leases or versions against races.
  • CDC-driven invalidation for completeness.
  • Versioned keys for groups of derived entries.
  • Coalescing and stale-while-revalidate against stampedes.
  • An invalidation plan for every cache layer; decisions made on the source of truth.

Questions people ask

What is cache invalidation?

Cache invalidation is keeping a cache from serving stale or wrong data after the underlying data changes. Adding a cache is easy, but keeping it correct is the hard part, which is why interviewers who hear "we add a Redis cache" often ask what happens when the data changes. The first step is deciding how stale each type of data may be: immutable assets can be cached forever, profiles for seconds, and balances or payments not at all.

Should you update or delete the cache on write?

Delete the cache key after updating the database, rather than writing the new value into the cache. Concurrent writers updating the cache can leave it holding an older value, whereas after a delete the next read misses and reloads fresh data. Delete on write still has a race, so always keep a TTL as a safety net and use leases or versioned values against stale backfills.

How can a cache hold stale data even with delete on write?

A slow reader can put the old value back after a writer deletes the key. Reader A misses and reads the old value from the database, writer B updates the database and deletes the key, then A writes the old value into the cache, where it stays until its TTL. Leases fix this: the delete invalidates the lease token A got on its miss, so the late write is rejected; versioned values and a delayed second delete also help.

What is the difference between write-through and write-behind caching?

Write-through writes to the cache and the database together, while write-behind writes to the cache and flushes to the database asynchronously. With write-through, reads after writes are fresh but every write pays both costs. Write-behind gives very fast writes, but data can be lost if the cache fails before flushing, so it suits only data where that loss is acceptable, such as counters and analytics.

How do you make sure no cache invalidation is missed?

Drive invalidation from the database's change stream: change data capture emits each committed change, and an invalidator deletes or refreshes the affected keys. Because invalidation no longer depends on every code path remembering it, scripts and other services cannot skip it, changes stay ordered per row, and keys are invalidated only after commit. The cost is a short delay, typically well under a second.

How do you prevent a stampede when a hot cache key is invalidated?

Use request coalescing, stale-while-revalidate or early probabilistic refresh, because invalidating a hot key otherwise sends every request to the database at once. With coalescing, one request recomputes the value while the others wait for its result. Stale-while-revalidate keeps serving the old value while one background refresh runs, and early probabilistic refresh renews the entry before expiry with rising probability as expiry approaches.