Section 9.19

Head-of-line blocking

Definition

Head-of-line blocking, often shortened to HOL blocking, is the delay that items in a first-in, first-out §14.14 (FIFO) queue suffer when the item at the front, the head of the line, is stuck or slow: every item behind it waits, even one that a free resource could serve at once. The term comes from network switches, where a packet waiting for a busy output holds back packets bound for idle outputs. It also describes work queues §6.36, event loops §14.23 and network connections that deliver data strictly in order.

The idea

A supermarket with one checkout line shows the problem. The customer at the front has a full trolley and a payment problem. Behind them stands someone with one bottle of water. The cashier could scan the bottle in two seconds, but the line is first-in, first-out, so the bottle waits for the trolley. The second customer’s delay comes not from their own purchase but from their place in line.

In software the same shape appears whenever unrelated jobs share one ordered path. The common embedded case is a single worker thread that runs jobs one after another. A job that erases pages of flash memory §6.6 or computes a long cryptographic signature occupies the worker, and a short job that only acknowledges a message waits behind it. Nothing is broken, and by the queue’s own rule nothing is unfair; the short job is simply in the wrong line.

How it works

Head-of-line blocking needs three things:

  1. One ordered path: items enter a single queue and leave it in arrival order.

  2. A server that takes only the head: the worker, link or output serves the front item and cannot skip it.

  3. A slow or stuck head: the front item takes long, or waits for something, such as a busy device, a lost packet or a full output, that the items behind it do not need.

Remove any one of the three and the blocking goes away.

The classic cases:

The remedies attack one of the three conditions:

Worked example

A worker serves one FIFO queue and runs each job to completion. At time 0 it starts a 20 ms job that erases four flash pages at 5 ms each. Jobs A and B, which need 1 ms each, arrive immediately afterwards; treat their arrival time as 0.

The average response time of the two short jobs is (21+22)/2=21.5(21 + 22) / 2 = 21.5 ms, against 1 ms of work each.

Separate queue: if A and B go to a second worker that shares nothing with the erase, A runs from 0 to 1 ms and B from 1 to 2 ms. Their response times are 1 ms and 2 ms, an average of (1+2)/2=1.5(1 + 2) / 2 = 1.5 ms. If A and B also need the flash controller, which performs one operation at a time, the second worker waits for the erase just the same and the gain disappears.

Splitting: if the erase is instead split into four steps of one page each, and each unfinished erase is put back at the tail, step 1 runs from 0 to 5 ms and the queue then holds A, B and the erase. A runs from 5 to 6 ms and B from 6 to 7 ms; erase steps 2, 3 and 4 run from 7 to 12, 12 to 17 and 17 to 22 ms. A and B now have response times of 6 ms and 7 ms, and the erase finishes at 22 ms instead of 20 ms. It is 2 ms later, exactly the work of A and B that now runs before it.

Stated precisely

Consider one server that serves a FIFO queue without preemption. Item 0 starts service at time 0 and needs service time S0S_0. Items 1,…,n1, \dots, n arrive at times a1≤a2≤…≤ana_1 \le a_2 \le \dots \le a_n, all before time S0S_0, and need service times S1,…,SnS_1, \dots, S_n. The server is then busy without a gap, and item kk finishes at Fk=S0+∑i=1kSi,F_k = S_0 + \sum_{i=1}^{k} S_i, so its response time is Rk=Fk−akR_k = F_k - a_k. The head contributes S0−akS_0 - a_k to RkR_k, the part of its service that remains when item kk arrives; every item between the head and item kk contributes its full service time. In the worked example, S0=20S_0 = 20, S1=S2=1S_1 = S_2 = 1 and a1=a2=0a_1 = a_2 = 0, so F1=21F_1 = 21 and F2=22F_2 = 22 ms.

The switch result holds under these assumptions: an NN-by-NN switch with NN growing without limit; fixed-size packets and time divided into slots; at most one packet sent to each output per slot; every input queue always non-empty; and the output of each packet chosen uniformly at random and independently of all others. Under them, the throughput of each output tends to 2−2≈0.5862 - \sqrt{2} \approx 0.586 of one packet per slot.

Limits and common mistakes

Head-of-line blocking is a delay, not a failure: every item is eventually served, provided the head eventually finishes. If the head can wait forever, for example for a reply that never comes, the whole queue stalls forever. When that reply would come from an item behind it in the same queue, the stall is a deadlock §9.14.

Moving work to another thread moves the queue, not the bottleneck. If the work still needs a resource that performs one operation at a time, such as a flash controller or a bus, the line forms there.

On some microcontrollers §6.1 the processor stops while its internal flash memory is being erased or written, whenever it fetches instructions from that same flash. Every thread and every interrupt handler §6.12 that runs from that flash then waits, and no change to the queues helps.

Priority order trades head-of-line blocking for a risk of starvation §9.18: a low-priority item can wait forever if urgent items keep arriving.

Reordering is allowed only where order does not matter. Items that depend on each other, such as a write followed by a read of the same record, must stay in order, so splitting work into separate queues must keep such items in one queue.

The durations in the worked example are hypothetical; real flash and cryptographic operation times depend on the device.

Questions answered

Which unrelated work waits behind a long cryptographic or flash operation?

All work that sits behind the long operation on the same ordered path waits, whether or not it needs cryptography or flash memory. That includes every job submitted to the same worker thread or work queue after the long job, every event handled by the same single-threaded event loop, and every message queued behind it on the same in-order connection: for example acknowledgments of other messages, timer handlers and logging. Work further along also waits if it needs a lock that the long operation holds, or the same device, such as the flash controller. On some microcontrollers a flash erase or write also stops the processor whenever it fetches instructions from that flash, and then all code running from that flash waits.

To find the exact set, list the queue, the thread, the locks and the devices that the long operation occupies, and then every other piece of work that uses any of them. The cure is to move work that needs none of them to its own queue or thread, to split the long operation or make it asynchronous where that is valid, and to keep in one queue the work that must stay in order.

Related entries: Bounded queue (§9.31), Response time versus execution time (§9.32), Backpressure (§9.30), Queue and FIFO (§14.14), Queueing delay (§14.38), Zephyr workqueue (§6.36). Further reading: [299].