Skip to main content

Posts

Showing posts with the label Redis

Redis Cache Stampede Prevention: Implementing Probabilistic Early Expiration

  The "3:00 AM" Spike Your monitoring dashboard is all green. CPU usage is sitting at a comfortable 15%. Suddenly, without a corresponding increase in user traffic, your primary database CPU spikes to 100%, connections time out, and the API latency graph goes vertical. By the time you SSH in, the system has recovered. You just fell victim to the  Cache Stampede  (also known as the Thundering Herd). This isn't a traffic problem; it's a synchronization problem. A highly accessed cache key (e.g., a global configuration object or a trending leaderboard) expired. At that exact millisecond, 5,000 concurrent requests hit your backend. They all missed the cache simultaneously. They all decided to rebuild the expensive query simultaneously. Your database, unable to process 5,000 heavy aggregation queries in parallel, effectively DDoS'd itself. The Root Cause: The Gap of Doom The standard caching pattern is "Check Cache -> Miss -> Read DB -> Write Cache"....

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...