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. When that caller disconnects — a browser tab closes, a client times out — the shared future is dropped and the other ninety-nine callers fail for no reason of their own.

The producer has to be cancellation-independent: it must keep running after the caller that started it is gone, and its result must be published to whoever is still waiting.

In Rust that means spawning the work and handing callers a receiver rather than awaiting a future owned by one of them. I use a watch channel per in-flight key:

// one producer per key, callers only observe let (tx, rx) = watch::channel(None); tokio::spawn(async move { let result = upstream.collect_bounded(limit).await; let _ = tx.send(Some(result)); }); 

The producer's lifetime is the work's lifetime, not any caller's.

Bounding is not optional

An unbounded coalescer is a memory leak with extra steps. Every limit here is configurable and defaults are deliberately small:

Bound Default Why
Active keys 64 caps concurrent upstream work
Callers per key 128 caps waiters per flight
Deadline 30 s no flight outlives a sane request
Collected body 2 MiB caps buffered upstream output

Over the limits you get a decided answer, not a hang: 503 for admission refusal, 504 on deadline, 502 for abort, oversize, or truncation.

The subtle one: identity in warm caches

My first review pass found something worse than a leak. Cache keys were canonical request payloads, and the flight key was derived from the same thing — but the stored entry also carried the caller's authorization and tenant headers.

Two callers with the same body and different tenants could therefore share a warm cache entry. Empty, absent, and non-ASCII tenant values made it worse, because they collapsed to the same fingerprint.

The fix is a presence-tagged raw identity fingerprint in the namespace, so "absent" and "empty string" can never alias, and non-ASCII survives intact. There is a regression test that reproduces the collapse before the fix and passes after.

That is the kind of bug that no amount of throughput measurement would have caught, and it is the reason I would not ship a coalescer written by someone who had not written that test.

What the measurement actually shows

Local mock, artificial 250 ms upstream delay, 100 simultaneous callers:

coalescing off: 100 upstream calls, 100 responses coalescing on: 1 upstream call, 100 responses, 99 joined 

That is a correctness and call-count result on a mock. It is not a provider-billing saving, not a throughput claim, and not evidence of production adoption. The repository ships the raw before/after JSON so the numbers can be checked rather than trusted.

Honest limits

  • Process-local only. Two replicas still make two calls; this is not distributed exactly-once.
  • Non-streaming requests only. Streams have different lifecycle rules and are left alone.
  • Opt-in and default off, because a proxy that silently changes how many upstream calls happen is a proxy that surprises people.
  • The test suite is a seeded reference model plus real HTTP regressions, not a proof.

The checklist if you build one

  • Make the producer cancellation-independent, then test it by dropping the first caller.
  • Bound keys, waiters, time, and bytes — each with a decided error code.
  • Fingerprint identity with presence tags; never let "absent" equal "empty".
  • Collect upstream bytes before inserting into cache, not after.
  • Record before/after call counts with the exact delay and concurrency used.
  • Say "mock" when it is mock.

The design document and benchmarks are in stack-intercept.

If you have a proxy, worker queue, or CI pipeline with this class of bug and no time to find it, I run a fixed-fee review — severity-ranked findings with the exact patches, three working days, USD 99: CI and MCP security review.