A cache miss under load produces a burst of identical upstream calls. The usual fix is a lock plus a cache write, and the usual bug is what happens when the first caller walks away.
I added opt-in request coalescing to a Rust reverse proxy and wrote up the design. Here is the short version, including the part I got wrong first.
The shape of the problem
One request arrives, the cache is cold, the handler starts an upstream call. Ninety-nine more arrive before the first response is inserted. If your only guard is "check cache, then fetch", all hundred go upstream.
Coalescing means: the first caller becomes the producer, the rest wait on its result, everyone gets the same bytes, and exactly one upstream call is made.
The bug you hit on the second day
The obvious implementation ties the producer's future to the first caller's request.β¦