Skip to content

The cache that went stale right after invalidation

Failure story 19, in concurrency and ownership.

The writer commits the new price, then deletes the cache entry, exactly as the design says. Why is the cache serving the old price a day later?

Assumption Value
Pattern Cache-aside: on a miss, a reader loads from the database and puts the value in the cache
Writes Commit to the database, then delete the cache key
Cache entries Expire after 24 hours
Value The price of product P-7: $40 (version 1), changing to $32 (version 2)
Step Reader Writer Database Cache
1 Cache miss for P-7; reads the database v1, $40 (empty)
2 Pauses before writing to the cache v1 (empty)
3 Commits $32 v2, $32 (empty)
4 Deletes P-7 from the cache: nothing to delete v2 (empty)
5 Resumes; puts $40 in the cache v2 v1, $40
6 Every request for the next 24 hours gets $40 v2 v1

The invalidation completed before the stale value arrived, so there was nothing left to invalidate it.

Deleting on write assumes that any later fill reads the new value. A fill that started before the write and finishes after it carries the old value past the invalidation. The window is the time between a reader’s database read and its cache write: usually microseconds, sometimes seconds when a process pauses. At high read volume, a small window is still hit often, and one hit lasts until the entry expires.

Aspect Design A: delete on write Design B: version-checked fills
What a fill carries The value The value and its version from the database
What a delete leaves Nothing A marker: P-7 invalidated at version 2
A fill with an older version Accepted Rejected, because version 1 is older than the marker
Alternative Leases: on a miss, the cache hands the reader a token; a delete cancels outstanding tokens; a fill with a canceled token is refused

Both variants make the cache the place where the race is decided, because only the cache sees the order in which the fill and the invalidation arrive. Memcached deployments at Facebook used the lease approach for this race and to limit stampedes.

Reproduce the interleaving with a test hook that pauses the reader between its database read and its cache fill.

  1. Expected: after the invalidation completes, the delayed fill can’t republish version 1: the cache rejects it, or the entry stays empty.
  2. Expected: the next reader misses, loads version 2 and fills successfully.
  3. Run reads and writes concurrently for 10 minutes. Expected: no cache entry ever holds a version older than the database’s for longer than the time it takes to complete one read.

This check is in the checks catalog as test templates in Java and Python.

  • Staleness is acceptable for up to the entry’s lifetime, and the lifetime is short. A 60-second TTL bounds the damage to 60 seconds; it doesn’t prevent it. That’s a fine trade for a product description and a poor one for a price.
  • Writes are rare and reads never pause for long, and you measure that rather than assume it.

Discussion question. A colleague proposes deleting the cache entry twice, once before and once after the write, with a short delay in between. What does that fix, and what doesn’t it?

Exercise. Under Design A, the catalog has 10,000 popular products. Each product’s price changes about once a minute, and a cache fill (the time from a reader’s database read to its cache write) takes about 2 milliseconds. Entries live for 24 hours. For one product, how often does the race happen? And for the catalog?

Worked answer

The race needs a fill in flight at the moment a write commits. A product’s entry is empty just after each write, so the next reader fills it, and that fill is in flight for about 2 ms. The chance that the following write lands inside that 2 ms is about 2 ms ÷ 60,000 ms, or 1 in 30,000. With 1,440 writes a day, that’s about 0.05 a day per product: rare enough that nobody will reproduce it on purpose. Across 10,000 products it’s about 480 stale prices a day, each of which can last up to 24 hours. A process pause makes the window seconds instead of milliseconds, and the numbers grow accordingly. On the delete-twice idea: the second delete catches fills that finish before it, but a fill that finishes after it still wins. It shrinks the window; Design B closes it.

Written and edited by Abhinav Taduka, enterprise software architect.