Want the details?
What is backpressure?
In many systems one part produces work and another part uses it: a web server receives uploads and a worker resizes them; a service reads events and another stores them. Between them there is usually a buffer: a queue, a channel, a socket. If the producer is faster than the consumer for a long time, the buffer fills up.
Then there are only three choices: let the buffer grow without limit (until memory runs out), throw the extra work away, or tell the producer to slow down. The third choice is backpressure: the full buffer pushes back on the one who fills it.
How to read the bench
| Part on the bench | What it means in a real system |
|---|---|
| Pump | The producer. Its lamp is lit while it sends work. |
| Drops in the pipe | Work on its way: requests, messages, bytes. |
| Tank and sight glass | The buffer, with room for 40 litres. |
| Tap and basin | The consumer, using work at its own speed. |
| Handle on the inlet | The float valve: the backpressure signal. Along the pipe: open. Across it: shut. |
| Overflow pipe and drain | Work that is thrown away because there was no room, and nobody was told. |
The numbers
- The pump brings 3 litres a second. The house uses 1, 2 or 4.
- The tank holds 40 litres. The float valve shuts at 40 and opens again at 32, so it does not switch on and off every moment.
- Normal use, working valve: the tank fills at 3 − 2 = 1 litre a second, so from 20 litres it is full after 20 seconds.
- Low use, broken valve: 3 − 1 = 2 litres a second are lost, so 120 litres a minute.
- High use: the tank empties at 4 − 3 = 1 litre a second. Nothing is lost, but the house is short of 1 litre a second once the tank is empty.
All numbers on the page come from the same small simulation that moves the water, and its test checks that every litre is counted: pumped = used + lost + what is still in the tank.
In software
- A bounded queue that blocks. In Java,
ArrayBlockingQueue.put()waits when the queue is full. The producer thread stops until there is room: the float valve. - TCP flow control. The receiver tells the sender how much room it has left (the window). A full receiver says 0, and the sender waits.
- Reactive Streams. The consumer asks for
request(n)items; the producer may not send more than it was asked for. - Kafka consumer lag. Kafka stores messages on disk, so the "tank" is very large; lag tells you how far the consumer is behind.
- HTTP 503 and 429. A server that is too busy can answer "try again later": backpressure across the network, which the next lessons on retries build on.
The worst case is the broken valve: an unbounded buffer, or one that drops work quietly. Both look fine in tests and fail under real load.
Words to know
- Producer
- The part that creates work.
- Consumer
- The part that does the work.
- Buffer
- A place where work waits between them, with a limited size.
- Backpressure
- A signal from a full buffer back to the producer: slow down or stop.
- Load shedding
- Throwing work away on purpose when there is too much. Better than a crash, if you count what you threw away.