Why TTL Alone Fails: Cache Stampedes and Smarter Invalidation Strategies
Stale data occurs when cached content falls out of sync with the source of truth, an inevitable gap created by any synchronization mechanism including TTL, event-based invalidation, or versioning. A more dangerous consequence is the cache stampede, where thousands of simultaneous requests miss an expired hot key and hammer the origin database at once, potentially taking it offline within seconds. Engineers typically combine four techniques to address this: TTL for bounded staleness, event-based invalidation for near-realtime freshness on writes, versioned cache keys to silently retire old entries, and single-flight request coalescing to ensure only one request rebuilds a missed key while others wait. The single-flight pattern can be implemented in-process using Go's singleflight package or distributed across nodes via a Redis SETNX lock. A robust production setup layers all three invalidation methods together, using TTL as a safety net, events to cut staleness early on writes, and single-flight to absorb the thundering herd at expiry time.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in