Backpressure: when consumers can’t keep up
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.
- 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.
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.
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
| Layer | How the consumer pushes back |
|---|---|
| TCP | The 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 channels | A send on a full buffered channel blocks until a receiver takes something. |
| Kafka | Consumers pull at their own pace. The cost of being slow shows up as consumer lag, kept on disk, not in memory. |
| HTTP APIs | Return `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.
$ node nopipe.mjsbuffer peaked at 20
$ node pipe.mjs20 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
503instead 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.503 is a request to back off. Clients that retry at once, with no backoff or jitter, turn backpressure into extra load.drain, await queue.put()) instead of a blocking wait.