Section 9.31

Bounded queue

Definition

A bounded queue is a queue, a container that releases items in a defined order, usually first in, first out §14.14 (FIFO), whose number of items can never exceed a fixed capacity. It comes with a rule for an item that arrives when the queue is full: refuse the new item, discard an older one, or make the producer wait. The bound limits the memory that the queue’s slots occupy and turns overload into visible refusals or drops. It does not by itself limit how long an admitted item waits.

The idea

A device with 64 KiB of RAM queues log messages for a slow serial port. During a burst of errors, messages arrive faster than the port can print them. With an unbounded queue, a linked list §14.6 that allocates memory for every message, the queue grows until memory runs out, and the device crashes at an unpredictable moment, often while handling the very error being logged. With a bounded queue of 32 messages, the 33rd message is refused and counted, the device keeps running, and the log states how many messages were lost.

The picture is a car park with a fixed number of spaces and a barrier at the entrance. When the car park is full, the barrier stays down and arriving cars are turned away or wait outside. The number of spaces is fixed in advance, and what happens at the barrier is a rule that someone chose. The picture stops at one point: cars leave a car park in no particular order, while a queue releases items in a defined order, so an item’s position in the queue decides how long it waits.

How it works

Storage. A bounded queue is usually a ring buffer §14.15: a fixed array §14.5 of slots with a read position and a write position that each advance by one and wrap around to the start after the last slot. Each slot holds either the item itself, copied in, or a pointer to an item stored elsewhere. In the second case the queue bounds only the number of pointers, and the memory of the items must be bounded separately, for example by taking them from a fixed pool.

Operations. Let KK be the capacity and qq the number of items in the queue.

  1. Enqueue (add): if q<Kq < K, store the item and add 1 to qq. Otherwise apply the overflow policy.

  2. Dequeue (remove): if q>0q > 0, take the oldest item and subtract 1 from qq. Otherwise wait, or report that the queue is empty.

With one producer and one consumer, each position is written by only one side, and atomic loads and stores of the two positions suffice, with no lock. With several producers, adding an item needs a lock or an atomic read-modify-write instruction such as compare-and-swap §9.9.

Overflow policies.

Recording. Every refusal or discard must leave a trace.

A loss must not be reported through the same full queue, or the report is lost too. The counters are read and reported later.

Waiting time. A bound on the number of items ahead of a new item gives a bound on its waiting time only if each item’s service time is bounded and the consumer works whenever items are waiting. If the consumer stalls, every admitted item waits as long as the stall lasts.

Worked example

Waiting time. A first-in, first-out queue of capacity K=4K = 4 feeds one consumer. The consumer serves one item at a time, is never interrupted, and takes at most 2 milliseconds (ms) per item. A new item arrives while 3 items are waiting and another is being served, so the new item becomes the fourth waiting item. It waits at most for the rest of the item in service, at most 2 ms, plus the 3 items ahead of it, at most 3×2=63 \times 2 = 6 ms: in total at most 2+6=82 + 6 = 8 ms. It then needs at most 2 ms itself, so it is finished within 8+2=108 + 2 = 10 ms of arriving. If other work can pause the consumer, these numbers no longer hold.

Memory. The queue runs on a 32-bit processor, where a pointer takes 4 bytes, and each of its 8 slots holds a pointer. The slots take 8×4=328 \times 4 = 32 bytes. If each pointer refers to a message of up to 256 bytes, the queued messages take up to 8×256=20488 \times 256 = 2048 bytes, which the 32-byte figure does not include. If the messages come from a pool of 8 buffers of 256 bytes each, queue and messages together take at most 32+2048=208032 + 2048 = 2080 bytes. If instead the producer allocates each message from general memory and an enqueue is refused, the producer must free that message; otherwise every refusal causes a memory leak §4.8 of up to 256 bytes.

Recording. In a test, 1000 messages are offered, 940 are accepted and 60 are refused, a loss of 60/1000=6%60 / 1000 = 6\%. The refused messages are exactly those that arrived while the queue was full, when waiting times were longest. A latency figure computed over the 940 delivered messages alone therefore looks better than the system is, an instance of survivorship bias §11.30. The report must state the offered, delivered and lost counts beside the latency figures.

Stated precisely

Let KK be the capacity in items and q(t)q(t) the number of items in the queue at time tt. The queue maintains the invariant 0≤q(t)≤K0 \le q(t) \le K at every time tt. Counting from the start, offered=refused+overwritten+delivered+q(t),\text{offered} = \text{refused} + \text{overwritten} + \text{delivered} + q(t), where refused counts items turned away on arrival, overwritten counts items discarded after admission under drop oldest, and delivered counts items handed to the consumer. An item whose producer is still blocked counts as not yet offered.

For waiting time, assume four things: first-in, first-out order; one consumer that serves one item at a time without interruption; at most CC time units of service per item; and a consumer that never stays idle while an item waits. Then an item admitted when nn items are already waiting, where 0≤n≤K−10 \le n \le K - 1, waits at most (n+1)C(n + 1)C, counting the item in service, and is finished within (n+2)C(n + 2)C. The worst case n=K−1n = K - 1 gives a wait of at most KCKC and a finish within (K+1)C(K + 1)C. With K=4K = 4 and C=2C = 2 ms these are 8 ms and 10 ms, as in the worked example. Under drop oldest, an admitted item may be discarded instead of served. Without the four assumptions there is no such bound.

Limits and common mistakes

A bound on slots is not a bound on memory when the slots hold pointers.

A bound on memory is not a bound on time. A stalled consumer makes every admitted item wait without limit, and a blocked producer waits as long.

A larger capacity does not raise throughput; it only lets more items wait. Little’s law §14.39 says that the average number of items in a stable queue equals the arrival rate times the average time an item spends there [88]. A full first-in, first-out queue of 100 items served at 3 items per second therefore makes a new item wait about 100/3≈33100 / 3 \approx 33 seconds. An oversized queue hides overload behind long delays, a problem that networking calls bufferbloat.

Too small a capacity refuses ordinary bursts. Size the queue for the largest burst that must be absorbed, and compare the size with the high-water mark.

Drop oldest delivers items with gaps: the consumer may receive item 3 and then item 7. The consumer must tolerate gaps.

With several producers, one fast producer can fill every slot and cause starvation §9.18 of the others. A quota of slots for each producer prevents it.

In a single first-in, first-out queue, one slow item delays every item behind it, which is head-of-line blocking §9.19.

Loss that is not recorded makes results look better than they are, as the recording example shows.

Questions answered

Which messages are dropped or rejected, and how is that recorded?

The messages lost are the ones that the queue’s overflow policy selects, so the policy must be chosen and written down, and every loss must be counted when it happens.

For recording, keep a counter for each cause of loss, put a sequence number in every message so that the receiver can see gaps, and keep a high-water mark of the largest number of messages ever queued. Do not report a loss through the full queue itself. Any analysis must report how many messages were offered, delivered and lost, because statistics over delivered messages alone leave out the messages that met the fullest queue.

Related entries: Backpressure (§9.30), Head-of-line blocking (§9.19), Response time versus execution time (§9.32), Ring buffer (§14.15), Queue and FIFO (§14.14), Little’s law (§14.39), Survivorship bias (§11.30). Further reading: [88].