What is prefetch (QoS) in RabbitMQ and how does it affect throughput?
Learn what prefetch (QoS) means in RabbitMQ, how it limits unacknowledged messages, and how to tune it to balance throughput and fair dispatch across consumers.
Expected Interview Answer
Prefetch (set via basic.qos) is the limit on how many unacknowledged messages RabbitMQ will deliver to a consumer at once, controlling how much work a consumer buffers before it must acknowledge.
With a prefetch count of 1, the broker sends one message and waits for its ack before sending the next, giving fair distribution but adding a network round-trip per message. A higher prefetch lets consumers pipeline work and hides latency, raising throughput, but too high a value hoards messages on fast-arriving-but-slow-processing consumers, causing uneven load and higher memory use. The right value balances round-trip latency against fairness, and is tuned per consumer speed and message processing time.
- Prevents a single consumer from being overwhelmed
- Improves throughput by pipelining deliveries
- Enables fair dispatch across competing consumers
- Bounds consumer memory usage
- Tunable per channel or per consumer
AI Mentor Explanation
Prefetch is like a batting coach deciding how many throw-downs to feed a player before they must return balls to the basket. Feed one at a time and the net session drags with constant waiting; feed a bucketful and a slower batter drowns while a quicker one stands idle. Setting the right count keeps every net running smoothly without any player hoarding deliveries or starving.
Step-by-Step Explanation
Step 1
Open a channel
Prefetch (QoS) is configured per channel, so establish the consumer channel first.
Step 2
Call basic.qos
Set prefetch_count to the number of unacknowledged messages the consumer may hold at once.
Step 3
Consume with manual ack
Prefetch only takes effect when acknowledgements are manual, so disable auto-ack.
Step 4
Process and acknowledge
Ack each message after processing; the broker delivers a new one to stay within the prefetch window.
Step 5
Measure and tune
Benchmark throughput and fairness, then raise or lower the count to match consumer speed.
What Interviewer Expects
- Definition of prefetch as unacknowledged-message limit
- Link between prefetch and manual acknowledgements
- Trade-off of latency versus fairness
- Awareness that too high a value causes uneven load
- How to tune the value for real workloads
Common Mistakes
- Thinking prefetch works with auto-ack enabled
- Assuming a higher prefetch is always faster
- Confusing prefetch count with queue length
- Ignoring fairness when setting a large value
- Setting a global default without per-consumer tuning
Best Answer (HR Friendly)
“Prefetch controls how many messages RabbitMQ hands a worker before that worker confirms it finished the earlier ones. A low number spreads work evenly, while a higher number lets each worker stay busy, so you tune it to balance speed and fairness.”
Code Example
const channel = await connection.createChannel()
await channel.prefetch(10) // hold at most 10 unacked messages
await channel.consume('tasks', async (msg) => {
await doWork(msg.content)
channel.ack(msg) // frees a slot so the broker sends the next
}, { noAck: false })Follow-up Questions
- What is the difference between global and per-consumer prefetch?
- How does prefetch interact with fair dispatch?
- What happens if you never acknowledge a message?
- How would you tune prefetch for slow versus fast consumers?
- Does prefetch apply when auto-ack is enabled?
MCQ Practice
1. What does basic.qos prefetch_count limit?
Prefetch caps how many delivered-but-unacknowledged messages a consumer may hold at once.
2. For prefetch to have any effect, the consumer must use:
Prefetch limits unacknowledged messages, so it only matters when acks are manual.
3. A prefetch that is set too high can cause:
A large prefetch lets one consumer hoard messages, creating uneven distribution and more buffered memory.
Flash Cards
What is prefetch in RabbitMQ? — A QoS limit on the number of unacknowledged messages a consumer may hold at once.
Which ack mode does prefetch require? — Manual acknowledgement — it has no effect with auto-ack.
Effect of prefetch = 1? — Fair, one-at-a-time dispatch with a round-trip per message, lowering throughput.
Risk of a very high prefetch? — Uneven load and higher consumer memory as one worker hoards messages.