Backpressure and bounded queues
Telling the producer to slow down
Section titled “Telling the producer to slow down”The last lesson ended with a queue that grew by 10 messages a second for as long as the producers outpaced the consumers. Nothing in the queue objected. Every message was accepted, every producer was told that all was well, and the cost showed up later as a longer and longer wait.
Backpressure is the opposite. The Reactive Manifesto’s glossary describes it as a component that is under stress communicating that fact to the components upstream, so that they reduce the load. For a queue, the way to say it is to bound the queue: when it holds as many messages as it is allowed, a new message is refused, and the producer learns that it must wait, drop the message, or send fewer.
The bound turns an invisible, growing delay into a visible limit. The overload is still there. What changes is where it is felt: in the producers, which can decide what to do about it, instead of in an ever longer queue.
Mental model. An unbounded queue accepts everything and pays later in waiting time. A bounded queue pays at once by refusing. In both, the consumers can only take what they can take: the queue never makes capacity, it chooses where the excess waits.
Try it: the same overload, with a limit
Section titled “Try it: the same overload, with a limit”The lab is the same overload as before: 30 a second in, 4 consumers taking 5 each. This time Backpressure is on, and the queue accepts no more than 100 messages, waiting and being received together. Read Accepted from producers and Waiting in the queue.
The same overload, but the queue refuses what it cannot hold and producers are slowed to the rate the consumers sustain.
What is happening
Producers send 30 a second and the consumers can take 20. Backpressure keeps the queue under 100 messages by slowing the producers down.
- Waiting in the queue
- 800 in flight, invisible until their timeout
- Work done
- 200200 different messages, 0 runs repeated
- Dead-letter queue
- 0gave up after 3 attempts
- Accepted from producers
- 28020 refused for lack of room
Waiting in the queue Dead-letter queue
| State | Messages | What it means |
|---|---|---|
| Waiting in the queue | 80 | Visible in the queue, ready to be received. |
| In flight | 0 | Received and invisible until the visibility timeout, waiting for an acknowledgement. |
| Acknowledged | 200 | Done and deleted from the queue. |
| Work repeated | 0 | Times a message already processed was processed again, because its acknowledgement was lost. |
| Dead-letter queue | 0 | Gave up after the allowed number of attempts, kept apart to be looked at. |
| Refused | 20 | Not accepted because the queue was full, with backpressure on. The producer is slowed down. |
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab advances one second at a time. A consumer receives up to its rate each second and finishes each message in the same second; processing time, the network and the broker itself are not modelled.
- A message is hidden for the visibility timeout after each receive. A crash or a lost acknowledgement makes it reappear, and after the allowed number of attempts it moves to the dead-letter queue. A random draw, seeded so the same settings give the same run, decides each outcome.
Simplifications
- The work queue does not track the order of messages: retries are received first, which stands for the oldest. Order is shown in the stream, where a partition keeps its messages in the order they were appended.
- Exactly-once processing, transactions, replicated brokers, retention, message size and a consumer that is slow rather than dead are not modelled. The numbers are floors and illustrations, not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
At the start, after 10 s, the queue has accepted 280 messages, and the detail line says that 20 were refused for lack of room. The queue filled to its 100 in the first 9 s and has been full ever since. Press Skip 30 s twice, to 70 s: 1,480 accepted and 620 refused. From the moment the queue is full it accepts exactly what the consumers free: 20 a second, which is 4 × 5. The other 10 a second are refused, a third of what the producers send, and the producers are held to two thirds of their rate, 20 out of 30.
Waiting in the queue stays at 80. The limit is 100, and at the start of each second the queue is full; the consumers then take their 20, which leaves 80 waiting, and the producers refill it to 100. That is a steady state, as the status sentence says: backpressure keeps the queue under 100 messages by slowing the producers down.
Now switch Backpressure off and press Skip 30 s twice. The backlog starts to climb again, to 680, by 10 a second. Compare the waits. Messages are taken oldest first at 20 a second, so a message that arrives behind 80 others waits 80 / 20, which is 4 s, whereas without the limit the same arithmetic at 70 s gives 700 / 20, which is 35 s. The producers pay for the short wait with refused messages.
What happens if the producers ignore the refusal?
Section titled “What happens if the producers ignore the refusal?”A refusal is only a signal. A producer that drops what was refused loses messages, which may be fine for a temperature reading and is not fine for an order. A producer that retries at once sends the same messages again, so the queue sees the same pressure and refuses again. A producer that holds the message and tries later moves the queue to the producer’s memory: the backlog is still there, only somewhere else. In each case the system decided what to do with the excess. Only an unbounded queue avoided deciding, and it paid by growing.
So backpressure is worth most when it reaches the source of the work. If the producers are web requests, the refusal should become an answer to the user, such as a message to try again later, and not a thread waiting inside the service. Load shedding, which refuses work on purpose to protect a service, is the same idea applied to requests.
- Backpressure is a component under stress telling the components upstream to reduce the load.
- A bounded queue refuses messages when it is full. The overload does not disappear: it is felt by the producers, which must wait, drop or send fewer.
- In the lab the queue accepts 20 a second once it is full, which is 4 consumers × 5. Of the 30 a second sent, a third is refused, and after 70 s there are 1,480 accepted and 620 refused.
- With the limit, the newest message waits 4 s. Without it, 35 s at 70 s.
- A refusal is only a signal: what the producer does with it decides whether messages are lost, repeated or delayed.
Next: retries and dead-letter queues, for the messages that fail.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.