Sub topic 9
Backpressure and Prefetch Limits
This chapter assumes you know how producers, brokers, queues, and acknowledgements work, and that you have run competing consumers on one queue. It covers what happens when messages arrive faster than consumers can handle them, and how the prefetch limit controls the flow.
What backpressure means
Backpressure is a signal that tells a faster part of a system to slow down because a slower part is full. In a message queue, producers often outpace consumers. Without a limit, the queue grows, memory or disk fills, and latency climbs.
A broker can apply backpressure in several ways. It can block publishers, refuse new messages, or drop old ones. The consumer side has its own control: the prefetch limit.
What prefetch does
By default, a broker may push many messages to a consumer at once. Those messages are unacknowledged: the consumer holds them, but the broker has not yet removed them from the queue. Prefetch is a cap on how many unacknowledged messages the broker will send to one consumer. In RabbitMQ it is set with basic_qos.
Here is the consumer from the acknowledgements chapter, with the relevant line marked:
import pika
def process(body):
print("handling", body)
def handle(ch, method, properties, body):
try:
process(body)
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception:
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()
ch.queue_declare(queue="tasks", durable=True)
ch.basic_qos(prefetch_count=10) # at most 10 unacked messages in flight
ch.basic_consume(queue="tasks", on_message_callback=handle, auto_ack=False)
ch.start_consuming()With prefetch_count=10, the broker sends 10 messages. It sends the eleventh only after the consumer acknowledges one. The ack acts as the "ready for more" signal.
Why an unbounded prefetch causes trouble
Suppose two consumers share a queue holding 1,000 messages, and prefetch is unlimited. The broker may hand most of the messages to whichever consumer connected first. That consumer works through them slowly while the other sits idle. If it crashes, all of its unacknowledged messages return to the queue at once.
Choosing a value
The right number depends on how long each message takes to process and how long a network round trip takes.
| Prefetch | Effect | Typical use |
|---|---|---|
| 1 | Fairest sharing. The consumer idles during each round trip, so throughput is lowest. | Slow, uneven jobs such as video encoding |
| 10 to 50 | Balances fairness and throughput. | Most workloads |
| Very high or unlimited | Highest raw throughput. Risks uneven load and memory pressure. | Fast, uniform messages with careful memory limits |
Start small and raise the value while watching throughput. Stop raising it when throughput no longer improves.
Backpressure on the producer side
Prefetch protects consumers. To protect the broker, bound the queue itself. RabbitMQ supports a maximum queue length, and when it is reached it either drops the oldest messages or rejects new publishes. Rejecting is usually the safer choice because it pushes the problem back to the producer, which can retry later or return an error to its caller.
Exercise
A queue has two consumers, each with unlimited prefetch. One consumer is much slower than the other, but the queue's messages are all delivered to it before the faster one connects. What setting fixes the uneven load, and why does it work?
Show answer
Set a small prefetch_count, such as 10, with manual acknowledgements. The broker then sends each consumer only a few unacknowledged messages and delivers more when the consumer acks. The faster consumer acks sooner, so it receives more messages, and the slow one no longer holds a large backlog.