05
Deep dive — the stale-read race in cache-aside
Consider two clients hitting the same key:
- Client A: read cache → miss
- Client A: read DB (gets value
v1)
- Client B: write DB (new value
v2)
- Client B: delete cache key
- Client A: write cache key ← writes the now-stale
v1
Cache now holds v1; DB holds v2. Cache is stale forever (until TTL). This race exists in any cache-aside system. Mitigations:
- Short TTLs — stale window bounded (e.g., 60s).
- Double-delete — B deletes the cache, sleeps 500ms, deletes again. Catches A's late write.
- Versioned values — cache entry stores
{value, version}. On write, A's version is older, gets rejected.
- Write-through instead — eliminates the race entirely but costs latency on every write.
The interview answer: "cache-aside is correct for most workloads if you pair it with short TTLs. If you need read-after-write, switch that specific key's strategy to write-through — don't redesign the whole cache."