BACKPRESSURE

An unbounded queue is a slow crash

L214 MINRELIABILITY
PREDICT

A producer feeds a consumer through a queue. You bound the queue at 50 items and reject new items while it is full. The consumer handles 100 items/s and the producer sends 150 items/s for an hour. What is the worst queueing delay any accepted item sees?

A producer pushes items into a queue. A consumer drains them at 100 items/s. If the producer sends 150 items/s, the queue grows by 50 items/s and never stops. Each item waits behind every item before it, so delay grows with the queue, and memory grows with it, until the process dies and takes the queue with it. Nothing errors on the way down. The queue absorbs the overload, and that is the problem.

Backpressure is the slow side telling the fast side to slow down. Bound the queue. When it is full, either block the producer or reject the item. Delay is then at most capacity divided by the consumer’s rate, whatever the arrival rate.

▶ SIMULATION
PRODUCER
wants to send: 0 items/s
accepted last second: 0
never told to slow down
push and forget
QUEUE
t = 0 s
length: 0 (max 0, capacity 50)
queueing delay: 0 s (bound 0.5 s)
memory: 0 MB of 500 MB heap
unbounded (ArrayList / channel with no limit)
CONSUMER
capacity: 100 items/s
served last second: 0
served total: 0 of 0 arrived
idle: queue empty
worst queueing delay0 s (queue peaked at 0)
✓ queue length <= capacity (delay <= capacity / consumer rate)✓ process memory below the heap limit
SCHEDULE
01[PRODUCER]producer starts at ARRIVAL RATE items/s; consumer drains 100 items/s
02[PRODUCER]burst: 300 items/s for 2 s
03[CLOCK]+5 s
04[CLOCK]+5 s
05[CLOCK]+5 s
06[CLOCK]+10 s
07[CLOCK]+10 s
08[CLOCK]+20 s
09[CLOCK]end of window: report
Start of unbounded queue scenario. 9 steps.
const queue = [];                       // grows forever
producer.on('item', (item) => queue.push(item));
setInterval(() => { const item = queue.shift(); if (item) process(item); }, 10);
// arrival 150/s, service 100/s → +50 items/s → OOM in minutes

Watch the invariant strip in UNBOUNDED QUEUE: it turns red during the burst and never recovers, and the heap check goes red at t = 35 s. In both FIX scenarios the same burst arrives and the queue never passes 50. BLOCK PRODUCER keeps the strip green but its verdict ends red: the producer keeps wanting 150 items/s, so blocking only moves a growing pile from the queue into the producer. Blocking is right for a burst. For overload that does not stop, something has to reject or shed.

TUNE IT NOT YET

The consumer drains 100 items/s. After the 2-second burst the unbounded queue holds a backlog. Find the lowest ARRIVAL RATE at which that backlog never drains.

GOAL: the queue is still non-empty at the end and ARRIVAL RATE <= 100

FIX IT NOT YET

Rewrite offer so the queue is bounded: queue the item and return true when there is room, otherwise return false so the producer slows down. The grader runs a producer at 150 items/s against a consumer at 100 items/s, then a calm phase at 50 items/s.

Mental model:

A queue does not add capacity; it only moves time around. If arrivals exceed the consumer’s rate for long enough, an unbounded queue turns that excess into unbounded delay and then into a crash. So bound every queue, and decide at design time what happens at the bound: block the producer (the backlog moves upstream, where it must also be bounded) or reject (the work is dropped, visibly). Either choice is a decision; the unbounded queue is the same decision made for you, later, by the OOM killer.

IN PRODUCTION

Bounded channels in Go and Rust, BlockingQueue and offer() in Java, Reactive Streams request(n), Kafka consumer lag, TCP receive windows, HTTP 429 and Retry-After. The symptom without it: latency that climbs for minutes and a restart that loses everything in flight.

IN INTERVIEWS

“Your consumer is slower than your producer. What happens, and what do you do?” Say the queue grows without bound and delay with it, then name the bound and what you do when it is hit: block, reject, or shed. Mention that blocking moves the problem upstream, and that at 100% utilisation a backlog never drains.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: LOAD SHEDDING →