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 be the capacity and the number of items in the queue.
Enqueue (add): if , store the item and add 1 to . Otherwise apply the overflow policy.
Dequeue (remove): if , take the oldest item and subtract 1 from . 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.
Reject new, also called tail drop: the arriving item is refused, and its producer is told and keeps it. Older items are preserved.
Drop oldest, also called overwrite: the oldest waiting item is discarded to make room. The producer never waits, and the queue always holds the most recent items. This suits trace buffers and sensor readings.
Block: the producer waits, possibly with a time limit, until the consumer frees a slot. Nothing is lost, but the producer is delayed for as long as the consumer is.
By priority: the item of lowest priority, new or old, is the one refused or discarded.
Recording. Every refusal or discard must leave a trace.
A counter for each cause, such as refused because full, overwritten, or timed out while blocked, incremented at the moment of loss.
A sequence number in each item, assigned by the producer, so that the consumer sees a gap when items are missing: receiving 17 right after 14 shows that 15 and 16 were lost.
A high-water mark, the largest number of items the queue has ever held, which shows how close it came to full and helps to choose its size.
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 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 ms: in total at most ms. It then needs at most 2 ms itself, so it is finished within 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 bytes. If each pointer refers to a message of up to 256 bytes, the queued messages take up to 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 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 . 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 be the capacity in items and the number of items in the queue at time . The queue maintains the invariant at every time . Counting from the start, 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 time units of service per item; and a consumer that never stays idle while an item waits. Then an item admitted when items are already waiting, where , waits at most , counting the item in service, and is finished within . The worst case gives a wait of at most and a finish within . With and 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 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.
Under reject new, the message that arrives while the queue is full is refused; its sender learns this from the return value and still holds the message.
Under drop oldest, the oldest waiting message is discarded to make room for the new one.
Under a priority policy, the message of lowest priority is refused or discarded, whether it is new or old.
Under block, nothing is lost unless the sender gives up after a time limit, and then that sender’s message is refused.
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].