BACKPRESSURE
An unbounded queue is a slow crash
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.
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 minutesconst queue = new BoundedQueue(50);
producer.on('item', (item) => {
if (queue.offer(item)) return;
producer.pause(); // tell the upstream: slow down (HTTP 429, TCP window, pause())
producer.unshift(item); // and keep the item: hand it back, or reject it with a 429, never drop it silently
});
queue.on('drain', () => producer.resume());
// delay is at most 50 / 100 = 0.5 s, whatever the arrival rate 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Delivery and planningEngineering Manager
- Architecture governance and platform investmentHead of Engineering
- Capacity and costSRE, Platform and DevOps