All jobs / Backend developer / Performance
Backend developer interview question · 3–5 years
The interviewer asks: “The cache expires and 2,000 requests hit the database at once. How do you prevent that?” Strongest answer?
The strongest answer
Stampede control: single-flight / lock around the rebuild, jittered TTLs, and a stale-while-revalidate window so only one request (or a small set) refills. Don't let every waiter run the same query.
Why it works
Exactly. The thundering herd is a known pattern. Jitter plus a rebuild lock is the standard fix; serving stale briefly is allowed if the product can live with it.
What a good answer covers
- One rebuild, many waiters.
- Jitter TTLs; consider SWR.
- Don't drop the cache as the fix.
- Don't cache the entire database.
Answers that fall short
- Close, but not the strongest: Set TTL to 24 hours so expiry is rare.
Long TTL reduces frequency and makes invalidation a rumour. You'll still herd at 24:00:00. - Weak: Disable the cache if it stampedes — the DB can take it.
The DB cannot take an unbounded simultaneous replay. That's the outage you were caching to avoid. - Red flag: Preload the entire database into cache at boot.
Boot-time full preload is slow, huge, and still needs invalidation. Cache the hot keys, not the warehouse.
Who gets asked this
More Performance questions
- Under load, someone raises the connection pool from 20 to 200 per instance. Throughput drops and p95 gets…
- The interviewer asks: “How do you know we can take 3× traffic on Monday?” Strongest answer?
- The interviewer asks: “The bill doubled but QPS didn't. Where do you look?” Strongest answer?
- A teammate opens a pull request that rewrites a service “for performance”. What do you ask for before you…
- The interviewer asks: “The API is idle but requests hang on 'opening a connection.' What's going on?”…
- The interviewer asks: “The API returns 429. What should a client do?” Strongest answer?
- The interviewer asks: “You add Cache-Control to a JSON API. What do you watch for?” Strongest answer?
- The interviewer asks: “Why is OFFSET 100000 slow even with a LIMIT 20?” Strongest answer?