Prevent the stampede with single flight: the first request to miss takes a short lock and recomputes while the others wait briefly or serve the stale value. Combine that with stale while revalidate, so an expired entry is still served while a background refresh runs, and add jitter to time to live values so keys created together do not expire together. For known hot keys, refresh proactively before expiry.
Why interviewers ask this
Caching questions separate people who have added a cache from people who have operated one. The interviewer wants the specific failure mode, a thundering herd on expiry, and the standard mitigations. It also opens up the harder discussion about invalidation, staleness tolerance and what happens when the cache itself goes down, which is where an under provisioned database usually falls over.
How to structure your answer
- Name the mechanism: many concurrent misses on one hot key.
- Give the single flight lock and what the waiters do.
- Add stale while revalidate and jittered expiry.
- Mention the cold cache scenario and capacity planning.
Example answer
The pattern is a thundering herd: one hot key expires and every concurrent request misses at the same moment, so they all run the same expensive query. The core fix is single flight, where the first miss acquires a short lived lock on the key and does the work, and everyone else either waits on the result or gets the stale value for a moment. I pair that with soft expiry: I store a logical refresh time inside the value and keep the hard time to live longer, so an entry past its refresh time is still served while one worker recomputes it in the background. Users never see the latency at all. Then jitter, because if a deploy warms a thousand keys with the same five minute time to live they expire together and I get the stampede across the whole cache at once. The bigger question that follows is what happens when the cache is empty entirely, after a restart or a failover. I want the database to survive that, so I load test with the cache disabled at least once.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- How would you implement the single flight lock safely across many instances?
- How do you invalidate a cached value when the underlying data changes?
- What happens to your service if the cache cluster fails completely?
Related backend developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions