Caching Strategies and Invalidation

2026-04-21

Phil Karlton famously said there are only two hard things in computer science: cache invalidation and naming things. He wasn't wrong. Caching is deceptively simple to add and surprisingly difficult to get right.

The three core strategies you need to know:

Real-world example: Imagine a product catalog page. You use cache-aside with a 10-minute TTL on product details. Traffic spikes during a sale — 50,000 requests/second hit one product. The cache entry expires, and suddenly hundreds of concurrent requests all miss the cache and slam your database simultaneously. This is a cache stampede. The fix: use a lock so only one request fetches from the database while others wait for the cache to repopulate, or use probabilistic early expiration where items randomly refresh before their TTL actually expires.

Invalidation approaches, ranked by complexity:

Rule of thumb for TTL sizing: Start with TTL = acceptable staleness × 0.5. If your business can tolerate 10 minutes of stale data, set TTL to 5 minutes. This gives you a safety margin for clock drift, delayed invalidation, and the reality that stakeholders always underestimate how stale "acceptable" really feels.

Common mistakes:

See it in action: Check out Caching Pitfalls Every Developer Should Know by ByteByteGo to see this theory applied.
Key Takeaway: Choose your caching strategy based on your read/write ratio, pick the simplest invalidation approach your consistency requirements allow, and always monitor your hit rate to prove the cache is actually helping.