22. Message Queues
22.8 What is backpressure?
At scale
At the bank: On pension day, people arrive faster than clerks can serve them. The board fills up. That's fine for a while: nobody is turned away, they just wait longer. But if it keeps growing, the hall overflows, and the bank has to stop handing out tokens for a while or ask people to come back later.
In the system: When producers send faster than consumers can process, the queue grows. With 1,200 orders a second in and workers handling 1,000 a second, the backlog grows by 200 a second: after a one-hour sale, 720,000 messages are waiting. When orders drop back to 400 a second, it takes 20 minutes to drain.
Figure: a queue turns a spike into a delay; backpressure stops the delay from becoming a disaster.
That's the queue doing its job: turning a spike into a delay. But a queue that grows forever means trouble: messages expire, memory or disk fills, and "SMS within a minute" becomes "SMS tomorrow". Options:
- Absorb: if the delay is acceptable and the queue has room, just let it drain.
- Scale consumers on queue depth or age of the oldest message (22.9).
- Push back on producers: a bounded queue that blocks, slows or rejects new messages when full, returning 503 or 429 upstream (Chapter 25), so the system degrades gracefully instead of falling over.
- Shed or prioritise: drop low-value messages (analytics) before high-value ones (payments), or use separate queues by priority.
Backpressure is any mechanism that tells the producer "slow down". Without it, a fast producer can bury a slow consumer.
Memory code: "A queue turns a rush into a wait; backpressure stops the wait from growing forever."