Zeeshan Adil
Book a call
← All concepts

CONCEPT

What is request coalescing?

Request coalescing, also called singleflight, makes concurrent callers asking for the same key share one in-flight call instead of each making their own. The first caller runs the work and the rest wait for its result. It turns a burst of identical requests into a single backend query.

Also called: singleflight, request collapsing. By Zeeshan Adil··Distributed Systems·Performance

Bar chart of database reads for one cold post: 100 callers with no coalescing make 100 reads, 100 callers with coalescing make 1 read, and 20 processes with 100 callers each make 20 reads, one per process.

The problem it solves

When a cache entry is missing, every request that notices starts the same slow query. For a popular key that is hundreds of copies of one query. Coalescing fixes this at the source: the process keeps a map from cache key to the call already in progress.

How it works

Code
caller A asks for p123  -> no call in progress: start one
caller B asks for p123  -> call in progress: wait for it
caller C asks for p123  -> call in progress: wait for it
the call finishes       -> A, B and C all get the same result

The first caller is the leader. It runs the query and stores the result. Everyone else waits on that result. Go ships this as the singleflight package, and the same idea exists in most languages.

Three details decide whether it is safe:

  • Key it per cache key. One global lock would make a slow refresh of one key block every other key.
  • Give each waiter its own deadline. If the leader is slow, a waiter should give up at its own time limit, not wait as long as the leader does.
  • Scope matters. It is per process. Twenty processes can still make twenty queries. To get one query across the fleet you need a distributed lock, which adds its own failure modes.

What it does not guarantee

Coalescing does nothing when the keys are all different. If 10,000 different posts expire together, you still get 10,000 queries. For that you need a separate budget on refresh work. It also shares failures: if the leader's query fails, every waiter sees that failure unless you handle it.

Measured

These numbers come from my cache stampede challenge. In a local run with real threads, 100 callers asking for one cold post read the database 100 times with coalescing off and once with it on. With 20 real OS processes, 100 callers each, the database saw 20 reads: one per process, as expected. In a simulation of 20 API processes at 20,000 reads a second, sharing one refresh per key cut the queries to 40 in 50 seconds, with 100% of reads served and database CPU at most 7.8%. A 2-second query with a 500 ms waiter deadline made 99 of 100 callers give up, the slowest after 517 ms, while the database still ran only one read.

RUN IT YOURSELF

Challenge · Distributed Systems ChallengesA popular post takes down the database
What is a cache stampede?What is stale-while-revalidate?What is a hot key?

SOURCES

Get the next deep dive by email

New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.

You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.

Written by Zeeshan Adil

I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.