Redis & Caching (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Cache Stampede (Thundering Herd) Mitigation: Probabilistic Early Expiration.
senior LevelMost AskedSystem Design
In Simple Words: A Cache Stampede occurs when a hot cache key expires (e.g. homepage top products), causing thousands of concurrent requests to hit the backend database simultaneously:
- Solution 1: Distributed Mutex Lock: The first thread acquires a lock to recompute the cache while others wait or return stale data.
- Solution 2: Probabilistic Early Expiration (XFetch Algorithm): As the TTL approaches expiration, background worker threads probabilistically recalculate the value before it expires: $\Delta t - \beta \times \ln(\text{random}()) > \text{expiry}$