Lesson 8 / 25
Prefetch, Fair Dispatch and Competing Consumers
Tune prefetch to balance throughput and fairness between workers.
How many messages a consumer may hold
By default RabbitMQ pushes messages to consumers as fast as it can, round-robin. Without a limit, one consumer may receive thousands of messages it cannot process quickly while another sits idle, and a crash returns a huge batch to the queue. basic_qos(prefetch_count=N) sets how many unacknowledged messages a consumer may hold at once; once it has N unacked messages, the broker sends it nothing more until it acks some. A small prefetch (1 to 10) gives fair dispatch for slow, uneven jobs; a larger prefetch (tens to hundreds) improves throughput for fast, uniform messages by keeping the network pipe full. Scale throughput by adding consumers (competing consumers), within the same process or across machines. Keep in mind that more consumers on one queue means messages can be processed out of order relative to each other, even though the queue delivers them in order.
Choosing prefetch
Start from processing time and tune with measurements.
workload suggested prefetch why
------------------------------------------ ------------------- -------------------------------
slow jobs (seconds each, uneven) 1-5 fair dispatch, small redelivery on crash
medium jobs (tens of ms) 10-50 keeps workers busy
fast, uniform messages (sub-ms processing) 100-300 hides network round trips
never leave it unlimited for production consumers
rule of thumb: prefetch ~ (round-trip time / processing time per message) + a littlePlates at a buffet counter
Prefetch is how many plates a server carries at once. Carrying one plate at a time is fair but slow; carrying fifty means some guests wait while one server juggles. A sensible stack keeps everyone served.
Quick check: What does basic_qos(prefetch_count=10) control?
- The maximum queue length
- The number of retries
- Message TTL in seconds
- The maximum number of unacknowledged messages delivered to the consumer at once
Answer
The maximum number of unacknowledged messages delivered to the consumer at once — Prefetch limits in-flight unacked messages per consumer (or channel).