Redis Caching Patterns: Implementing Probabilistic Early Expiration (Jitter) to Stop Thundering Herds
The Hook It starts with a single alerting pixel. Your database CPU spikes from 15% to 100% in less than a second. Latency alarms scream, and the connection pool exhausts immediately. The application isn't just slow; it’s down. The culprit? A single, highly accessed cache key (like a configuration blob or a homepage feed) expired. At $t=0$, the key existed. At $t+1ms$, it didn't. In that millisecond window, 5,000 incoming requests checked Redis, found nothing, and simultaneously hammered your primary database to recompute the same dataset. This is the Thundering Herd problem (also known as Cache Stampede), and standard TTLs are not enough to prevent it. The Why: Anatomy of a Stampede To solve this, we must understand the race condition. In a high-throughput system, a cache miss is expensive. Synchronization Gap: When a key expires physically in Redis (TTL hits 0), it vanishes atomically. Concurrent Reads: If your throughput is 10k RPS, and your database que...