Menu

Request coalescing in Rust: one upstream call for 100 callers
πŸ“°

Request coalescing in Rust: one upstream call for 100 callers

DEV Community: rustΒ·SiddharthΒ·2 days ago
#PiQSmTs7
#devtorust#devto#dev#upstream#caller
Long article (808 words) β€” no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.