Collapse duplicate in-flight requests
A cache miss on a hot key sends hundreds of identical queries to the database at once.
Say it like this
When a hot key misses, hundreds of identical fetches would hit the database at once, so I single-flight them: the first request fetches and the rest wait for that result, turning a miss storm into one query.
Basic solution
Let the first request fetch and make the others wait for its result (single-flight / request coalescing).
Single-flight: one fetch for many identical misses
The first miss starts a fetch and records a promise. Later requests for the same key attach to the promise and all receive the same result when the one fetch returns.
How it works
- A request misses the cache for key K and, instead of fetching directly, asks the coalescer to load K.
- The coalescer checks an in-memory map of loads that are currently in flight. If K is not in the map, it records a promise (a placeholder for the future result) under K and starts the fetch.
- Every other request for K that arrives while that promise is pending finds K in the map and attaches itself to the promise instead of starting its own fetch.
- When the fetch returns, the coalescer writes the value to the cache, resolves the promise, and every waiter receives the same value at once.
- The in-flight entry is removed from the map, so the next miss after the cache entry expires starts a fresh load rather than reusing an old one.
Where it lives
- In each app server process (Go singleflight, a promise map in Node): collapses duplicates within one server.
- At the CDN or reverse proxy (request collapsing): collapses duplicates across all clients for cacheable responses.
Worked example
Video manifest at a premiere
- 500K players request the same manifest in the first second after a premiere unlocks; the origin can serve about 2K requests/s.
- Without collapsing, the CDN edge misses once per PoP per concurrent request and forwards thousands of identical fetches; the origin falls over and every player sees a spinner.
- With request collapsing at the edge and an origin shield: each of ~200 PoPs sends one request to the shield, the shield sends one to the origin. Origin load is 1 request, not 500K.
- Inside the app, the same applies to a hot post body after its cache entry expires: 400 servers × 50 concurrent requests would be 20K database reads; single-flight per server makes it 400, and single-flight behind a shared lock in Redis makes it 1.
- Wait time for coalesced requests equals the one fetch, around 5 ms from the database, instead of a timeout under overload.
Variants and when each wins
Per-process single-flight
- An in-memory map in each server that lets one fetch per key run at a time. It wins as the default because it needs no infrastructure; cross-server duplicates still reach the source, bounded by the number of servers.
Shared refresh lock (Redis SET NX)
- The first server to take a lock in Redis fetches and fills the cache; the others wait briefly and then re-read the cache. It wins when the source is fragile and must see one request across the whole fleet; it costs a lock round trip and needs a stale-serving fallback if the holder dies.
- The expired value is served immediately and refreshed in the background under single-flight. It wins for keys that tolerate staleness, because nobody waits at all; readers see data a little past its TTL.
CDN request collapsing with origin shield
- For HTTP-cacheable responses the edge holds duplicate requests and forwards one, and a shield tier collapses across edges. It wins for anything served over HTTP because there is nothing to build, only to configure.
Pick per-process single-flight by default, add stale-while-revalidate for keys that tolerate staleness, a shared lock when the source is fragile, and CDN collapsing for anything served over HTTP.
Failure modes
Waiters inherit the failure
- One timeout turns into hundreds of errors at the same instant. It happens because every coalesced request shares the first fetch's result, including its failure. Fix it by letting waiters fall back to a stale value when the fetch fails, or retry with a small jittered delay, and never cache the error for long.
Coalescing a non-idempotent call
- Two users' different writes are merged into one. It happens because single-flight was keyed only by the resource name and applied to a write, so the second write attached to the first. Fix it by coalescing reads only, and keying loads by the full request identity.
Leaked in-flight entry
- A key never refreshes and every request for it hangs. It happens because the fetch neither resolved nor rejected (a hung connection) and the entry was never removed from the map. Fix it by giving every load a timeout and always deleting the entry in a finally block.
Lock holder dies
- With a shared lock, everyone waits until the lock's TTL expires. It happens because the server that took the lock crashed before fetching. Fix it with a short lock TTL, serving stale while waiting, and letting waiters take over the fetch when the lock expires.
Thundering herd after the fetch
- The source is hit again moments later. It happens because all keys expired at the same time, or the coalesced value was never written to the cache so the next request missed again. Fix it by writing through to the cache on success, adding jitter to TTLs, and refreshing early for hot keys.
Interviewer follow-ups
Answer out loud before you open each one.
How is this different from caching?
Caching stores a result after it is known; coalescing shares a result while it is still being computed. The cache handles the steady state, coalescing handles the moment of a miss when many requests arrive before the cache is filled. You need both for a hot key.
What happens when the single fetch fails?
By default all waiters get the error, which is a burst of failures. I would serve a stale value if one exists, otherwise return the error quickly and allow a bounded retry with jitter. Never cache the failure for more than a second or two.
Does single-flight help across many servers?
Per-process single-flight only collapses within a server, so the source still sees one request per server. To collapse across the fleet, use a shared refresh lock in Redis or serve stale while one holder refreshes; for HTTP content, let the CDN and an origin shield do it.
How does a CDN do this?
When an edge gets a request for an object not in cache, it marks it as in flight and queues further requests for the same URL instead of forwarding them. The first response fills the cache and answers all queued requests. An origin shield applies the same idea one tier up so the origin sees one request total.
Connections
- Built on
- Hash table · LRU cache: hash map + doubly linked list
- Pairs with
- Protect against a cache stampede · Fix a hot shard · Cache the hot reads · Serve static bytes from the edge · Avoid synchronised stampedes
- Pulls against
- Make retries safe
- Used in
- Distributed Key-Value Cache (Redis / Memcached-like) · Video Streaming Platform (YouTube / Netflix)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon Aurora | Database | AlloyDB for PostgreSQL, Cloud SQL | Azure Database for PostgreSQL or MySQL | PostgreSQL, MySQL, CockroachDB |
| Amazon ElastiCache | Cache | Memorystore | Azure Managed Redis | Redis, Valkey, Memcached |