Aller au contenu principal

Head-of-Line Blocking: When One Slow Request Stalls Everyone Behind It

· 10 minutes de lecture
Clustron Team
Ingénierie des systèmes distribués

Picture a single line at a counter. Most people have a quick errand — a stamp, a question — that takes seconds. Then one person steps up with a complicated transaction that takes ten minutes, and everyone behind them, no matter how trivial their errand, waits ten minutes. The counter is not overwhelmed; it is serving someone the entire time. The queue is simply ordered, and the slow item is at the front. That is head-of-line blocking, and almost every layer of a distributed system has a version of this line hidden in it.

The reason it is such a reliable source of confusing latency is that it decouples how busy a resource is from how long requests wait. A saturated server is easy to diagnose — CPU pinned, queues deep, everything slow in proportion. Head-of-line blocking produces the opposite signature: the server is nearly idle, most requests are instant, and a subset of requests — the ones that happened to arrive behind a straggler — are wildly slow, with no correlation to what they themselves were doing. You go looking for the expensive query that must be causing the spike, and the slow request is a one-key GET that should have been free.

What makes it worse is that the two reflexes you reach for both miss. Adding capacity assumes the resource is the bottleneck, but the resource was idle — it was blocked, not busy. Optimizing average latency assumes the slow requests are intrinsically slow, but they were fast; they inherited the latency of the item in front of them. The problem is not throughput and it is not per-request speed. It is ordering, and you fix it by changing the order, not the speed.

It hides at every layer​

The first place it lives is beneath everything else, in TCP. A TCP connection is an ordered byte stream: bytes are delivered to the application in exactly the order sent, with no gaps. That guarantee is implemented by holding back any bytes that arrive after a lost packet until the lost packet is retransmitted and fills the gap — so a single dropped packet stalls delivery of everything behind it, even data that already arrived intact. Run many independent logical requests over one TCP connection and one lost packet blocks all of them. This is precisely why HTTP/2, which multiplexes many streams over a single TCP connection, still suffered head-of-line blocking at the transport layer, and why QUIC rebuilt streams on UDP so that a loss on one stream no longer stalls the others.

The second place is a single pipelined connection. Pipelining is a throughput win — fire many commands without waiting for each response, and the pipe stays full — but it serializes completion: responses come back in request order. Put a slow command at the head of the pipeline and every response queued behind it waits for the slow one to finish first, even though those later commands were done long ago. Pipelining fills the pipe; it does not let a fast response overtake a slow one ahead of it. That is the tax the throughput win quietly carries.

The third place is the server's own work queue. One worker pulling from a shared FIFO can only process the item at the head, so a single slow operation — a scan across many keys, a bulk read, a command waiting on a slow dependency — holds the worker while fast operations pile up behind it. The same shape appears on any shared, ordered resource: a single thread, an event loop where one blocking call stalls every pending callback, a per-connection read loop. Everywhere one server serves a shared queue strictly in order, the slowest item at the head sets the wait for everything behind it.

One ordered channel — fast requests wait behind the slow headR1 slowR2R3R4R2–R4 only start here — after R1 clears the headserver idle the whole time; the fast ops inherited R1's latencyIndependent lanes + out-of-order completion — fast requests overtakelane AR1 slowlane BR2R3R4R2–R4 done here — long before R1 finishesunrelated work no longer shares a line with the straggler

Four ways past the straggler​

The cleanest fix is independent lanes: give unrelated work its own channel so a stall in one cannot block another. This is what QUIC's independent streams do at the transport layer, what using a connection per partition does at the client layer, and what a per-shard worker does at the server layer. Partition-aware routing already pushes in this direction — because each shard's traffic rides its own connection to its own owner, a slow operation on one shard's channel cannot park in front of a fast operation bound for a different shard. The more the lanes line up with genuinely independent work, the less any one straggler can touch.

The second fix is to separate slow traffic from fast. Even within one destination, not all operations are alike: a range scan or a bulk read is categorically slower than an interactive GET, and letting the two share a queue guarantees the scan will sometimes sit at the head of the interactive path. Route the known-slow classes — scans, large multi-key operations, background jobs — to their own queue or worker pool, and the interactive path keeps a short, fast-draining head no matter how much bulk work is in flight. It is admission control by traffic class rather than by volume.

The third fix is out-of-order completion: stop requiring responses to come back in request order. Tag each request with an id, let the server complete them in whatever order they finish, and match responses to requests by id on the way back. A slow command then delays only its own response, not the responses of everything queued behind it, which is the single feature that turns a pipelined connection from a head-of-line hazard back into a pure throughput win.

// One ordered channel: the slow op at the head blocks the fast ops behind it.
channel.Send(bigScan); // ~50 ms of work
channel.Send(getUser); // ~0.2 ms of work — but waits ~50 ms behind the scan

// Fix: route to an independent lane, and complete out of order by id.
var lane = Route(req.PartitionOrClass); // unrelated / slow work gets its own lane
var id = lane.SendAsync(req); // tagged; does not block other lanes
// responses are matched to requests by id and delivered as each one finishes —
// a slow request delays only its own reply, never the fast replies behind it.

The fourth is to bound the head. The techniques above route around a straggler; this one caps how bad a straggler can be. Put a ceiling on how much work a single queued item represents — chunk a large value or a huge multi-key operation into smaller units, attach a deadline and cancel past it — so the worst-case item at the head of any lane is bounded. A bounded head is the same discipline as backpressure and large-object chunking, seen from the latency side: if nothing can sit at the head for long, head-of-line blocking has nothing long to block behind.

Wrap-up​

Head-of-line blocking is latency caused by order, not load: a slow item at the front of a shared ordered channel makes every fast item behind it wait, while the resource itself sits idle. It hides at every layer — TCP's ordered byte stream, where one lost packet stalls all the data behind it; a single pipelined connection, where responses return in request order so a slow command gates the fast ones; and one worker draining a shared FIFO, where the slowest head sets everyone's wait. Because the resource is not saturated, adding capacity and shaving average latency both miss, since neither changes the order. What changes the order is structural: give unrelated work independent lanes so a stall stays local, separate known-slow traffic from the interactive path so it never sits at the head, complete requests out of order by id so a slow one delays only itself, and bound how large any single queued item can be so the worst-case head is short. A fast request should never have to wait on a slow one it has nothing to do with — and making sure it does not is about the shape of the line, not the speed of the counter.