advanced
RabbitMQ prefetch
Tune prefetch to bound unacknowledged work, balance fairness and throughput, and avoid one worker hoarding slow messages.
Prefetch (`basic.qos`) limits how many unacknowledged messages a channel delivers to a consumer. Low prefetch improves fairness across competing workers; higher prefetch raises throughput when handlers are fast and homogeneous. Prefetch of one is common for long-running tasks to avoid one worker hoarding a backlog of slow jobs.
Prefetch applies per channel, not per connection with multiple consumers. Combine prefetch with queue depth alerts and consumer autoscaling. Without prefetch limits, a single slow consumer can buffer many messages unacked and starve peers.
On interviews: explain why prefetch 1 helps fair work distribution, when to raise it, and how unacked messages relate to broker memory pressure.
Common pitfalls: high prefetch with slow handlers blocking the queue for others; forgetting prefetch on auto-scaled consumers; tuning prefetch without measuring handler latency variance.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Set prefetch based on handler duration variance.
- Monitor unacked message counts per consumer.
- Align prefetch with autoscaling signals.
- Re-tune after workload shape changes.