Cache Invalidation Strategies Explained
On this page
A profile picture that won't change
A user uploads a new profile picture. Their own page shows it. Their friends see the old one for an hour, some see it on one page and not another, and the support team can't reproduce it. Four separate caches held the old picture, and only some of them were told it changed. That's cache invalidation, one of the hard problems in computing because it's about keeping copies honest.
Cache invalidation strategies: TTL, delete, events, versioned keys
TTL expiry. The entry disappears after a fixed time. No coordination needed, and staleness is bounded by the TTL. Good enough for a lot of data, and a useful safety net under the others.
Delete on write. After updating the database, the code deletes the matching cache key, so the next read reloads it. Precise and immediate, but every code path that writes the data must remember to do it, including admin tools and batch jobs.
Event-based invalidation. The write publishes a "user 42 changed" event to a message bus. Every service, region or server with a cache of that data subscribes and deletes its copy. Scales to many caches and many writers; adds a messaging system and a small delay. A sturdier version uses change data capture: invalidation events come from the database's own change log, so no writer can forget.
Versioned keys. Put a version in the key: user:42:v7. Updating the user bumps the version to 8, and readers ask for v8. The old entry is never read again and ages out. No deletes needed, at the cost of keeping a current version number somewhere. Versioned asset filenames (Day 38) are the same idea.
The dual-write race that leaves stale data
Updating the database and the cache are two separate operations. They can interleave badly. A known race with cache-aside:
t1 Reader A: cache miss for user 42
t2 Reader A: reads the OLD row from the database
t3 Writer B: updates the row in the database
t4 Writer B: deletes cache key user:42
t5 Reader A: writes the OLD value into the cache <- stale until TTLWays to reduce it:
- Always keep a TTL, so the stale entry can't live forever.
- Delete again shortly after the write ("delayed double delete"), catching a reader that loaded old data in the gap.
- Leases or versions: the cache only accepts a fill if no invalidation happened since the miss. Facebook's memcache paper describes leases for exactly this.
- Invalidate from the change log, so invalidation follows the committed data, not application code paths.
And never update the cache before the database commits: if the transaction rolls back, the cache holds data that never existed.
How read replicas reopen the race
There's a second way to re-cache old data, and it catches teams that use replicas (Day 25). The writer updates the primary and deletes the key. The next reader misses, loads from a replica that hasn't received the update yet, and caches the old value. The delete worked; the reload undid it.
Fixes: load cache misses for recently changed keys from the primary, or delay the delete until replicas have caught up, which is what change data capture from a replica's log does naturally.
The same applies to in-process caches on many servers. Deleting user:42 from Redis doesn't touch the copies in 20 servers' memory. Broadcast invalidations over Redis Pub/Sub or your message bus so each server drops its own copy, and keep local TTLs short, seconds rather than minutes.
Worked example: invalidating a profile picture
- Store the new image at a new URL, such as
/avatars/42-v8.jpg. Old cached image URLs never need purging; the new one has never been cached. - Update the user row in the database.
- Delete
user:42from Redis, and publishuser.updatedso other services' local caches drop their copies. - Keep a 10-minute TTL on cached profiles as the safety net.
Check yourself
Invalidations can be missed through bugs, failed deletes or the dual-write race. The TTL bounds how long a stale entry can survive.
A reader misses, reads the old row, a writer updates the row and deletes the key, then the reader writes its old value into the cache, where it stays until the TTL expires.
Each update changes the key readers ask for, so the old entry is simply never read again and ages out on its own.
After the delete, a miss reloads from a replica that hasn't applied the update yet, re-caching the old price. Load misses for recently changed keys from the primary, or invalidate after replicas catch up.
An event, such as "product 42 changed", removes the stale entry immediately when the data changes, so users don't wait for a TTL. TTLs remain a safety net for missed events, which is why the two are combined.