Skip to content

Backpressure: when consumers can’t keep up

Deep diveConcept

One part of your system makes work faster than the next part can handle it. The difference piles up somewhere until something breaks. Here is how buffers, dropping and slowing the producer down each deal with it, and where each one fits.

01 A queue that only grows

A service writes 1,000 events a second into a worker. The worker can handle 800. Nothing fails at first. The extra 200 a second wait in a queue, the queue grows by 12,000 a minute, latency climbs as each new event waits behind all the old ones, and an hour later the process runs out of memory and loses everything it was holding.

Whenever one part of a system is faster than the next, the difference has to go somewhere. Backpressure is the next part telling the one before it to slow down. Without it, the only thing that eventually stops the producer is a crash.

A queue that is usually empty is doing its job: it smooths out bursts. A queue that is growing steadily means the consumer is too slow on average, and no buffer size fixes that. Watch the trend, not the size.

02 Bounded buffers and the signal back

Here is a producer that runs twice as fast as its consumer, with a buffer of 4 in between.

A bounded buffer pushes back on a fast producer
Step 1 of 12
Producer running
Buffer between them (capacity 4)
Empty
Handled by the consumer
Empty
Producer told to wait
Empty
  • just added or taken
  • no room: must wait

A producer makes a message every 1 ms (example rates). The consumer handles one every 2 ms. Between them is a buffer that holds at most 4 messages.

0 of 4 buffer

The buffer still matters: it absorbs short bursts so the producer is not stopped for every small hiccup. But only a bounded buffer gives backpressure. An unbounded one never says no, so the producer never learns it is going too fast.

03 Three answers when the buffer is full

Once the buffer is full, the system has to do one of three things with the next item. Each is right somewhere.

Buffer moreDelays the problemFine for bursts that end. For a steady overload it trades a crash now for a crash later, plus high latency in between.
DropLose some itemsRight when stale data is worthless: video frames, metrics samples, live like counts. Wrong for payments or orders.
Slow the producerNothing lostBlock, refuse or ask it to wait. Right when every item matters, as long as the producer can wait. A user's phone can; a sensor may not.

There is a fourth answer that is not about the buffer: make the consumer faster, often by adding more consumers. It fixes the cause, but scaling takes time, so you still need one of the three while it happens.

04 Where backpressure already lives

The same idea at different layers
LayerHow the consumer pushes back
TCPThe receiver advertises a receive window: how many more bytes it can take. A full window stops the sender.
Node.js streams`write()` returns `false` when the buffer passes `highWaterMark`; the writer waits for `drain`. `pipe()` does this for you.
Reactive Streams (RxJava, Reactor, Akka)The subscriber calls `request(n)`; the publisher may send at most `n` more items.
Go channelsA send on a full buffered channel blocks until a receiver takes something.
KafkaConsumers pull at their own pace. The cost of being slow shows up as consumer lag, kept on disk, not in memory.
HTTP APIsReturn `429` or `503` with `Retry-After`; a well-behaved client waits.

Notice that pull designs get backpressure for free: a consumer that asks for work when it is ready can never be flooded. Push designs need an explicit signal, and that signal is what tends to get lost when a system crosses a network or a queue boundary.

05 Seeing it in Node.js streams

Node's writable streams tell you when to stop. The code below writes 20 items into a slow consumer and waits for drain whenever write() says the buffer is full.

pipe.mjs
import { Writable } from 'node:stream';
// A consumer that takes 10 ms per item and buffers at most 4.
const slow = new Writable({
objectMode: true,
highWaterMark: 4,
write(item, _enc, done) {
setTimeout(done, 10);
},
});
let paused = 0;
let peak = 0;
for (let i = 1; i <= 20; i++) {
const ok = slow.write(i);
peak = Math.max(peak, slow.writableLength);
Ignore the return value
$ node nopipe.mjs
buffer peaked at 20
Wait for drain
$ node pipe.mjs
20 items written, paused 5 times, buffer peaked at 4

Both outputs are real runs on Node 24. The left one is the same loop calling slow.write(i) without checking what it returns. Node never refuses a write: highWaterMark is advice, and if you ignore false the buffer grows with everything you write. With 20 items it does not matter; with a file of a million lines it is the out-of-memory crash from the start of this page. stream.pipeline() handles the waiting for you.

06 Dropping on purpose

When slowing the producer is not possible, because it is a million phones or a live feed, choose what to lose instead of letting the crash choose.

  • Drop the oldest when only the latest value matters, such as a stock price or a cursor position. A ring buffer does this naturally.
  • Drop the newest when what is already queued is already promised to someone. New requests get a fast 503 instead of a slow timeout.
  • Sample when you need the shape and not every point: keep 1 in 10 metrics during a spike.
  • Coalesce when items can be combined: 500 likes in one second become one message saying +500.
  • Drop work nobody is waiting for. A request that has sat in the queue longer than the client's timeout will be thrown away by the client anyway. Check its age before doing the work.

07 Where backpressure leaks away

asyncio.Queue()Unbounded by defaultPython's asyncio.Queue() with no maxsize never fills. Java's Executors.newFixedThreadPool queues tasks in an unbounded LinkedBlockingQueue. Give every queue a limit and decide what happens at it.
A fire-and-forget hopService A pushes to service B over HTTP and does not wait for the reply. B can push back all it likes; A never hears. Backpressure only works along a chain where every link passes the signal on.
Retries that ignore the signalA 503 is a request to back off. Clients that retry at once, with no backoff or jitter, turn backpressure into extra load.
Buffers so big they hide the problemA huge buffer turns overload into latency: everything still works, just seconds late. On networks this is called bufferbloat. Size buffers for the bursts you expect, not for the worst day.
Blocking the wrong thingBlocking inside an event loop (Node, asyncio) stops every connection, not just the slow one. Use the asynchronous signal (drain, await queue.put()) instead of a blocking wait.

08 Check yourself

Question

A producer makes 500 items a second; the consumer handles 400. You raise the buffer from 10,000 to 100,000 items. What changes?

Question

Which design gives backpressure without any extra signal?

Question

A live-likes counter is overloaded. Which response fits best?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?