Request coalescing — also called single-flight, dogpile prevention, or “request collapsing” depending on which cache vendor’s docs you read — is the technique that turns “N concurrent miss requests” into “one origin fetch and N-1 waiters.” It is the foundation under any cache that intends to protect origin under load. This article is the operational view: what coalescing does, where it does it, and the failure modes to know.
How to verify
Confirm the cache layer is coalescing rather than passing-through. The signature is “one origin request even when N clients miss simultaneously.”
# Issue 50 concurrent requests for an uncached URL, then look at origin logs
seq 50 | xargs -P 50 -I{} curl -s -o /dev/null http://cache:6081/test-page
tail -n 20 /var/log/origin/access.log | grep test-page | wc -l
# Expect 1, not 50.
# In Varnish, the coalescing event shows in varnishlog
varnishlog -g request -q 'ReqUrl eq "/test-page"' -i ReqUrl,VCL_call,Waitinglist,HitMiss
If Waitinglist events appear and origin saw a single request, coalescing is working. If origin saw 50, your cache is not coalescing — it is passing through.
What’s happening
When a request misses the cache, the cache layer has a choice: send it to origin immediately, or wait to see if another request for the same key arrives within a small window and group them together. Coalescing chooses the latter — the first miss kicks off the origin fetch, subsequent identical-key misses register themselves as waiters on the same in-flight fetch, and when origin responds, every waiter receives the same response.
Varnish does this by default with no configuration: a request that misses creates a busyobj (a busy object), and any other request that hashes to the same key waits on the vcl_busy_* path until the first one completes. Nginx implements it with proxy_cache_lock on — when a request misses, nginx acquires a lock for that cache key and other waiters wait proxy_cache_lock_timeout for it to complete.
At the application layer, the same pattern lives under names like “singleflight” — Go’s golang.org/x/sync/singleflight is the canonical example. A function call keyed on a string is deduplicated across goroutines so only one runs at a time per key.
The catch: coalescing is per-cache-instance. Two Varnish nodes both miss simultaneously — each will issue its own origin fetch, and they will not see each other’s coalescing. Across a fleet, coalescing only protects per-node. For full deduplication, you need either origin shielding (one POP funnels everyone), an application-layer mutex on origin, or both.
The procedure
-
Varnish — verify it is on (it is, by default).
# No directive needed for basic coalescing; happens automatically per backend. # Tune the waiting list timeout if needed: sub vcl_backend_fetch { set bereq.first_byte_timeout = 5s; set bereq.between_bytes_timeout = 2s; } -
Nginx — enable explicitly. The default is
off; turn it on.proxy_cache_path /var/cache/nginx/main levels=1:2 keys_zone=main:10m max_size=2g; server { location / { proxy_pass http://origin; proxy_cache main; proxy_cache_lock on; proxy_cache_lock_timeout 5s; proxy_cache_lock_age 5s; proxy_cache_use_stale updating; } } -
Application — Go singleflight. A drop-in to deduplicate expensive calls across goroutines.
import "golang.org/x/sync/singleflight" var sf singleflight.Group func loadUser(ctx context.Context, id string) (*User, error) { v, err, _ := sf.Do("user:"+id, func() (interface{}, error) { return db.Query(ctx, "SELECT ... WHERE id=$1", id) }) if err != nil { return nil, err } return v.(*User), nil } -
Application — Python. Use an asyncio Future per key.
import asyncio _inflight = {} async def get_user(uid): fut = _inflight.get(uid) if fut: return await fut fut = asyncio.get_event_loop().create_future() _inflight[uid] = fut try: result = await db_query(uid) fut.set_result(result) return result finally: _inflight.pop(uid, None) -
Cross-node coalescing — origin shield + application lock. When per-node coalescing is not enough (popular object, fleet of caches), pair an origin shield (so one POP becomes the funnel) with an application-level Redis lock so even if the shield expires its entry mid-rebuild, only one origin worker rebuilds.
Common pitfalls
- Setting
proxy_cache_lock_timeouttoo short — when origin is slow, waiters time out and start their own fetches; you have re-created the stampede you wanted to prevent. - Coalescing on
req.urlalone when the response varies by header (e.g.Accept-Language) — N waiters receive the same response, but it might be the wrong language for some of them. - Coalescing POST or PUT requests at the cache layer — these are not idempotent; never coalesce non-cacheable methods.
- Application singleflight without a context with timeout — if the function never returns, every caller waits forever.
- Per-key locks with the key being the user ID instead of the resource ID — N users requesting their own profiles do not coalesce; you want the lock on the resource being fetched.
In the engagements we run, coalescing is a first-class part of the cache topology, not an afterthought. Stack Harbor’s managed operations practice is to verify it under load, document the per-layer behaviour, and pair it with the cache stampede mitigations that cover the failure modes coalescing alone cannot.