Design a Cache That Avoids Concurrent Duplicate Fetches
Problem
Design a caching layer where the critical requirement is to avoid issuing concurrent fetches for the same resource (request coalescing / thundering-herd protection).
Requirements
Functional:
- Cache responses with TTL
- On a miss, only ONE fetch per key; other callers wait for it
- Serve stale-while-revalidate
- Purge support
Non-functional:
- Very high concurrent request rate
- Protect the origin from stampedes
- Low added latency
Discussion points
- In-flight request map (single-flight) keyed by resource
- Locking vs futures/promises for waiters
- Stale-while-revalidate and negative caching
- TTL and purge propagation across edge nodes
- Failure handling when the single fetch fails
added …