Quick revision
The short answer
The waiters polled until the value appeared, but the only pod allowed to build it had been killed, so every request for that key waited until the 30-second lock expired and held a thread the whole time, until the pool ran out. We redesign it so a dead holder costs little: a short lease close to the rebuild time that the holder extends while working, a short wait budget after which waiters serve a stale copy or load themselves, a logical expiry inside the value with a longer physical TTL so a stale copy exists, token-checked release, and graceful shutdown.
What to remember
In this answer, we saw that the lock turned into an outage because waiters had no limit except the 30-second lock, so a killed holder made every request for the key wait 30 seconds while holding threads, and Little's law shows how quickly that drains a pool. The redesign uses a short lease the holder extends, a wait budget after which waiters serve stale data, load themselves or degrade, a logical expiry inside the value so a stale copy always exists, token-checked release in Lua, and graceful shutdown so rebuilds finish before a pod exits.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The waiters polled until the value appeared, but the only pod allowed to build it had been killed, so every request for that key waited until the 30-second lock expired and held a thread the whole time, until the pool ran out. We redesign it so a dead holder costs little: a short lease close to the rebuild time that the holder extends while working, a short wait budget after which waiters serve a stale copy or load themselves, a logical expiry inside the value with a longer physical TTL so a stale copy exists, token-checked release, and graceful shutdown.
Why this question is asked
Stampede protection with a lock is a good idea with a dangerous default: it makes many requests depend on one process. Interviewers like this incident because it shows how a mechanism meant to protect the database can take down the service instead, and because the fix needs several small decisions rather than one. In this answer, we will recap how the protection worked, explain step by step why it became an outage, and then rebuild it with short leases, wait budgets, stale copies, safe release and graceful shutdown.
1How the Protection Worked
On a cache miss, a request tries to take a lock with SET lock:<key> <token> NX PX 30000. NX means set only if the key does not exist, so exactly one request wins; PX 30000 makes the lock disappear after 30 seconds in case the winner never releases it. The winner rebuilds the value and stores it. Every other request polls the cache, say every 50 ms, until the value appears.
On a normal day this works well. The rebuild takes about a second, the waiters see the value after a few polls, and the database receives one rebuild instead of hundreds.
2Why It Became an Outage
During the deploy, the pod holding the lock for a hot key was killed in the middle of the rebuild. The lock stayed in Redis, because nothing deleted it, and nothing else was allowed to rebuild, because the lock said someone was already doing it. So every request for that key polled for the value that was never coming, until the lock expired 30 seconds later. That is exactly the 30-second p99.
The threads are the second half of the story. Each waiting request holds a request thread. Little's law says the number of requests in the system is the arrival rate times the time each one spends there: at 500 requests per second for this key and 30 seconds of waiting, that is 15,000 waiting requests, far more than a pool of 200 threads. Within half a second the pool was full, and requests for every other endpoint queued behind them. One dead pod and one hot key took down the whole service.
6 messages in order. Play them, or step through them one at a time.
3A Short Lease the Holder Extends
The lock's lifetime is really a guess at how long a holder may take before we assume it is dead. 30 seconds was a very pessimistic guess for a one-second rebuild. A lease close to the expected rebuild time, for example 3 seconds, means a dead holder blocks others for at most 3 seconds. A holder that legitimately needs longer extends its own lease every second while it works, so a slow rebuild is not interrupted and a dead one is detected quickly.
Both scripts compare the token first. The reason is that a holder whose lease ran out may still be running; if it deleted the lock by name, it could delete a lock that another pod took in the meantime. Running the check and the action in one Lua script makes them atomic.
4Waiters Need a Budget
The deeper flaw was that waiters had no limit except the lock. A waiter should wait only as long as the caller can afford, for example 300 ms, and then do something useful. If a stale copy of the value exists, it serves that. If not, it loads the value itself, accepting some duplicate work, because a few extra rebuilds are far cheaper than an outage. If the value is optional on the page, it returns a degraded response without it.
Notice that the thread cost is now bounded by the budget, not by the lock. At 500 requests per second and at most 0.3 seconds of waiting, Little's law gives about 150 waiting requests in the worst case, and only when there is no stale copy at all.
5Keep a Stale Copy to Serve
The best waiter is one that does not wait. If the value stored in Redis carries its own logical expiry, while its physical TTL is much longer, readers can tell when a value should be refreshed but still have it to serve. For example the value includes softExpiresAt ten minutes after it was written, while the key's Redis TTL is one hour.
- Value in cache?GET returns data and
softExpiresAtYesCheck its ageNoTake the lock or wait up to 300 ms - Past softExpiresAt?Logically stale, physically presentYesTry the 3-second leaseNoReturn it: fresh hit
- Lease acquired?SET lock NX PX 3000YesRefresh in background, return staleNoReturn stale
3 steps in order. Play them, or step through them one at a time.
Once a stale copy exists, no request ever waits for a rebuild.
6Release Safely and Shut Down Gracefully
Release through the token-checking script so a late holder never removes someone else's lock, and always release in a finally block so an exception does not leave the lease to expire on its own.
Finally, the deploy should not kill work in progress. On SIGTERM, a pod should first stop receiving new traffic, for example through a failing readiness probe or a preStop delay, then let in-flight requests and rebuilds finish within a grace period longer than the slowest rebuild, and release any leases it holds before exiting. With short leases and stale copies this is no longer critical, but it removes the most common way a lock holder dies.
7Follow-up questions
How would you choose the waiters' budget?
From two numbers: the latency the caller can afford and the threads we can spare. If the endpoint's p99 target is 500 ms and a normal rebuild takes 200 to 300 ms, a budget of about 300 ms lets waiters see a normal rebuild finish. Then check with Little's law that request rate times budget fits comfortably in the thread pool, for example 500 per second times 0.3 seconds is 150 threads out of 200, and lower the budget or add a stale copy if it does not.
Should we use Redlock or a consensus-based lock instead?
Not for this. The lock only prevents duplicate work, so if it occasionally fails and two pods rebuild, nothing breaks. A stronger lock adds latency and failure modes without fixing the real problem, which was waiting without a budget on a holder that could die.
Why does the lock need a token at all?
Without a token, any pod could release or extend the lock by name, including a holder whose lease already expired and was taken by another pod. The token proves ownership, and the compare-and-act scripts make that check atomic.
8Conclusion
In this answer, we saw that the lock turned into an outage because waiters had no limit except the 30-second lock, so a killed holder made every request for the key wait 30 seconds while holding threads, and Little's law shows how quickly that drains a pool. The redesign uses a short lease the holder extends, a wait budget after which waiters serve stale data, load themselves or degrade, a logical expiry inside the value so a stale copy always exists, token-checked release in Lua, and graceful shutdown so rebuilds finish before a pod exits.
Open with
The lock itself was fine; the waiting was not. The pod holding SET NX PX 30000 was killed mid-rebuild, the lock stayed for 30 seconds, and since only the lock holder may rebuild, every request for that hot key polled for a value that was never coming.
Then, if they want more
Each one held a request thread, and with hundreds of requests a second and 30 seconds of waiting, the pool was exhausted in under a second, so every endpoint suffered. I would change four things.
First, a short lease close to the real rebuild time, say 3 seconds, extended by the holder every second through a Lua script that checks its token, so a dead holder blocks others for 3 seconds at most.
Second, a wait budget of a few hundred milliseconds; after that a waiter serves a stale value, or loads it itself accepting some duplicate work, or returns a degraded response.
Third, I keep stale copies by storing a logical expiry inside the value and a much longer Redis TTL, so readers serve the old value while one caller refreshes, and normally nobody waits at all. Fourth, release only with a token check, and shut pods down gracefully so in-flight rebuilds finish before the process exits.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain why the protection turned into an outage, and redesign it so a dead lock holder...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/820d3310af/