System design lab · Episode 8

Backpressure: the water tank on the roof

A pump fills a tank on the roof, and the house takes water from it. When the tank is full, a float valve shuts the inlet: the full tank tells the pump to stop. Software needs the same signal. Without it, the extra work is simply lost. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
Water moving through a pipePump at the left, tank on the roof, tap on the front wallOverflow pipe at the right, down to the drain

Controls

Water use in the house
Float valve
Time

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 benchWhat it means in a real system
PumpThe producer. Its lamp is lit while it sends work.
Drops in the pipeWork on its way: requests, messages, bytes.
Tank and sight glassThe buffer, with room for 40 litres.
Tap and basinThe consumer, using work at its own speed.
Handle on the inletThe float valve: the backpressure signal. Along the pipe: open. Across it: shut.
Overflow pipe and drainWork 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.