Cache Stampede: The 30 Seconds That Brought Redis to Its Knees During a Campaign

On the evening of April 14, at exactly 20:00, our spring campaign went live. A push notification went out for the home page, traffic jumped sixfold, and at 20:00:07 the p99 latency of our checkout service shot up from 80 milliseconds to 4.2 seconds. What I saw when I looked at the Redis panel in Grafana defied logic: our cache hit rate had dropped from 99.7% to 91%, yet Redis CPU was perfectly calm. The real fire was on the MySQL side: 38 thousand queries per second, connection pools full, queries piling up in the queue. Thirty seconds later, everything went back to normal on its own. In those thirty seconds our add-to-cart rate fell 72%, and by a rough estimate seven figures of revenue evaporated on the spot. This post is the autopsy of those thirty seconds.

The Three Suspects We Thought Were Guilty

Our first reflex was the classic one: a bot attack. Campaign launches are a holiday for scalper bots, so we opened the WAF logs. The geographic and behavioral distribution of the traffic was organic; real people had tapped the push notification on real phones. The second suspect was our connection pool settings. We tried in staging to see whether enlarging the pool would solve the problem; it didn't — we were just sending a bigger mob at MySQL. The third suspect was Redis itself: "Maybe one of the nodes went through a failover," we said. There wasn't a single anomalous line in the Redis logs. Redis was working; the problem was in the way we were using it.

The real breakthrough in the war room came when we reconstructed the incident second by second. We lined up the load balancer logs, Redis keyspace miss statistics and the MySQL slow query log on the same timeline. The picture was clear: at 20:04:59.2 the miss count for a single key jumped from zero to tens of thousands, between 20:05:00 and 20:05:29 the same SQL statement was executed 11,800 times, and 96% of those queries died with a timeout. In other words, for 30 seconds the database had tried to produce the same answer 11,800 times. We wrote that sentence word for word into the postmortem document, because nothing sums up the problem better: searching for the answer to the same question thousands of times is the surest way never to find it.

Twelve Thousand Requests Racing to the Same Key

We saw the real cause once we grouped the access logs by the second. A single key fed the campaign storefront: the campaign product list, cached with a 300-second TTL. Because the campaign started at 20:00, this key had been warmed at 19:59 — and at 20:04:59 it expired, all at once. In the very millisecond the key died, the storefront endpoint was receiving more than 12 thousand requests per second. Every one of them got a cache miss, every one of them said "I'll generate the data," and all of them at once sent the same 900-millisecond aggregate query to MySQL. The literature calls this a cache stampede, or dog-piling: the resource the cache protects is left unprotected at the moment the cache refreshes, and it takes hundreds of times the load it would normally absorb, all in one go.

The insidious part is this: a cache with a 99.7% hit rate tells you you're safe. But nobody asks whether that 0.3% of misses is spread out over time. Ours weren't spread out; every five minutes, they all arrived in the same millisecond. MySQL was on its knees for 30 seconds, queries timed out, the application pods that got timeouts retried, and the retries poured gasoline on the fire. The system could only breathe again once the first successful query refilled the cache.

The moment a single key expired, thousands of requests sent the same query to the database; the system recovered once the first successful query refilled the cache.
The hit rate was high, but the misses arrived in the same millisecond.

The retry layer wasn't an innocent extra in this story; it was an accomplice. Our storefront service's HTTP client had a retry policy with two attempts and a fixed wait time; every request that timed out tried the same thing again 200 milliseconds later. So from the database's point of view, a wave of 12 thousand requests had grown into a wave of 30 thousand. After the incident we switched retries in every service to exponential backoff plus jitter and put a circuit breaker in front of the storefront query: when the error rate crosses a threshold the circuit opens, and requests fall back to a pre-built static campaign list without ever reaching the database. While the circuit is open, customers see a non-personalized but fully stocked storefront; never an empty page.

Locks, Dice and Stale Data

We built the fix in three layers. The first layer is locked refresh: when a key misses, not every pod rushes to the database; each one first tries to acquire a three-second lock in Redis with SET lock:kampanya NX PX 3000. The single pod that gets the lock generates the data and writes it to the cache; those that don't get it poll the cache again at 50-millisecond intervals. From the database's point of view, twelve thousand concurrent requests dropped to a single request.

The second layer is about removing the need for a lock altogether: probabilistic early expiration. We implemented the approach known as XFetch; when writing a value to the cache, we also store its generation cost (delta, 900 ms in our case) and the time it was written. Every reading client rolls a small die with the formula now - delta * beta * log(rand()), and shortly before the TTL runs out, one of the clients probabilistically says "I'll refresh this value." The key never actually expires; the refresh moment is statistically pulled forward and deduplicated. For hot keys, this is an even more elegant solution than a lock, because the very concept of waiting disappears.

The third layer is tolerance for stale data: stale-while-revalidate. We embedded a logical TTL inside the value and set the physical Redis TTL to twice that. When the logical time is up, the client returns the stale data immediately, and a single background job does the refresh. For a campaign storefront, showing a list that is 20 seconds stale is measurably better than showing a blank page for 4 seconds; we confirmed that with numbers in an A/B test.

We also put an in-process L1 cache in front of Redis: each pod keeps the 500 hottest keys in its own memory with a 5-second micro TTL. This layer isn't a direct cure for stampedes, but by cutting read traffic to Redis by 64% it creates a buffer against all kinds of turbulence on the network. For consistency, when keys are updated an invalidation message is published over Redis pub/sub and the pods drop their own copies. We didn't accept, without measuring it, that five seconds of storefront staleness touched no metric on the product side; that is the habit that became part of our culture.

Locking, probabilistic early refresh, stale-while-revalidate and an in-pod L1 cache together close off the stampede risk.
From the database's point of view, 12 thousand concurrent requests dropped to a single request.

Jitter for TTLs, a Dress Rehearsal for Campaigns

On top of these, we added two operational habits. The first is giving a random ±15% jitter to families of keys written at the same time; such a simple touch keeps hundreds of keys warmed in bulk after a deploy or at a campaign launch from dying in sync five minutes later. The second is tying the pre-campaign cache warmup to a job, and deliberately shortening TTLs in load tests so the stampede scenario becomes part of the rehearsal. In the May campaign, with the same traffic profile, our p99 latency never exceeded 110 milliseconds; the storefront query on MySQL stayed below 2 per second. From twelve thousand down to two.

Thanks to jitter, pre-campaign warmup and a stampede rehearsal, p99 stayed below 110 ms with the same traffic profile.
The storefront query went from twelve thousand down to two.

On the monitoring side, we added a metric specific to this incident: the number of concurrent misses per key. When the application starts generating a key, it emits as a histogram how many requests are waiting on that same generation; when the p99 of this histogram exceeds 5 in Grafana, a low-priority alert fires. This is the early cough of a stampede: it shows which key is risky weeks before it turns into a disaster. Indeed, two months later this alert caught a shipping-fee configuration key that nobody thought of as hot; we moved it to probabilistic refresh before the campaign, and we never heard a peep from that front. The best incident is the one whose graph nobody has to zoom into.

Hit Rate Is an Average, Disaster Is a Moment

The lesson that stuck with me from this case: nearly all cache metrics are averages, but what kills systems isn't averages, it's concurrency. The sentence "our hit rate is high" says nothing about the temporal distribution of misses. In design meetings we now ask the question this way: "In the millisecond this key dies, how many requests will be waiting on it, and how many of those will touch the database?" If the answer is greater than one, that cache design is incomplete. Whether you use a lock, probabilistic early refresh or stale data depends on the data and the product; but "none of them" is no longer a valid answer for us.

If you've lived through a similar campaign-moment crash, or if your storefront quietly flickers every five minutes, write to us via the contact page; let's read those thirty seconds in your logs together.