All Posts
Backend 9 min read

Cache stampedes, dogpiling, and eviction: Redis mistakes we won't make again

#redis#caching#architecture

Redis is blisteringly fast, but treating it like a magic key-value bucket will eventually bite you in production. During Black Friday, a client's primary database CPU spiked to 100% and crashed. The cause was not a lack of caching; it was how the cache expired.

The cache stampede (Dogpiling) catastrophe

The homepage featured products query was cached for exactly 10 minutes. At minute 10:00, the cache key expired. In the 200 milliseconds before a new worker could query the database and re-populate the cache, 4,000 concurrent user requests arrived.

All 4,000 requests saw a cache miss and executed the exact same heavy SQL query simultaneously. This phenomenon is called a cache stampede.

The solution: Distributed mutexes and XFetch

We prevented stampedes using two distinct strategies:

1. Mutex Locking: On a cache miss, only the first request acquires a short-lived Redis lock to regenerate the data. The other 3,999 requests wait 50ms and read the freshly cached value. 2. Probabilistic Early Expiration (XFetch algorithm): Instead of waiting for a hard TTL to expire, workers calculate a probability of refreshing the cache early based on request frequency and computation time. The cache is refreshed in the background before it ever goes cold.

Memory fragmentation and eviction policies

Another silent killer is improper eviction settings. If Redis runs out of memory without an eviction policy configured, it rejects all write operations with an `OOM command not allowed` error.

- Always configure `maxmemory-policy volatile-lru` or `allkeys-lru` to discard least-recently-used keys when memory pressure peaks - Avoid storing massive unbounded Redis sets or hashes that can cause latency spikes during serialization

Have a similar challenge?

We build production-grade software for companies that need it done right.

Let's Talk