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

- Author: Zeeshan Adil (https://www.internalswithzee.com/about)
- Published: 2026-11-12
- Source: https://www.internalswithzee.com/concepts/request-coalescing

Also called: singleflight, request collapsing.

## 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

```
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](/challenges/distributed-systems/a-popular-post-takes-down-the-database). 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.

## Related

- Challenge to run: https://www.internalswithzee.com/challenges/distributed-systems/a-popular-post-takes-down-the-database
- Related concept: https://www.internalswithzee.com/concepts/cache-stampede
- Related concept: https://www.internalswithzee.com/concepts/stale-while-revalidate
- Related concept: https://www.internalswithzee.com/concepts/hot-key

## Sources

- [Go singleflight package](https://pkg.go.dev/golang.org/x/sync/singleflight)
- [AWS Builders' Library: Caching challenges and strategies](https://aws.amazon.com/builders-library/caching-challenges-and-strategies/)

