Consumer Acks & Prefetch
Manual acknowledgements, redelivery after a crash, and how prefetch shares work
RabbitMQ removes a message from a queue only when a consumer acknowledges it. Until then the message is unacked: delivered, held by that consumer, and redelivered to someone else if the consumer's channel or connection goes away. That one rule is the basis of at-least-once delivery.
Acknowledgement modes
Always ack manually for anything that matters, and ack only after the work is done (the same rule as committing Kafka offsets after processing).
A crash, redelivered
From labs/work_queue.py: a worker takes 5 of 10 messages and dies
without acking:
crashing worker received ['0', '1', '2', '3', '4'] and dies without acking
surviving worker got: ['0(redelivered)', '1(redelivered)', '2(redelivered)', '3(redelivered)',
'4(redelivered)', '5', '6', '7', '8', '9']When the connection closed, all five unacked messages went back to the
queue and were redelivered with redelivered=True. Nothing was lost, and
messages 0-4 may have been half-processed by the dead worker, so the
second worker must handle them idempotently. The redelivered flag is a
hint that a duplicate is possible, not a guarantee that one happened.
There's no ack timeout by default for "slow" consumers. A consumer that
holds a message without acking and without dying keeps it forever. In
RabbitMQ 3.8+ a delivery acknowledgement timeout (consumer_timeout,
30 minutes by default) closes the channel of a consumer that holds a
delivery too long, which redelivers the message. Long-running jobs (hours)
need it raised for their queue, or they're killed mid-work and retried
forever.
Prefetch
Prefetch (basic_qos(prefetch_count=N)) caps how many unacked messages
a consumer may hold at once. It decides how work is distributed. The lab
runs a fast worker (10 ms per message) and a slow one (200 ms) on 60
messages:
prefetch=1 {'fast (10ms)': 56, 'slow (200ms)': 4} finished in 0.9s
prefetch=50 {'fast (10ms)': 50, 'slow (200ms)': 10} finished in 2.1s- fast worker (10 ms): 56
- slow worker (200 ms): 4
- fast worker (10 ms): 50
- slow worker (200 ms): 10, pushed up front
- With prefetch 1, a worker gets a new message only after acking the last one. The fast worker naturally takes most of the work, and the job finishes as soon as possible.
- With prefetch 50, RabbitMQ pushes a big batch to each consumer immediately. The slow worker takes 10 messages up front and works through them at 200 ms each while the fast worker sits idle: more than twice as long overall.
Never leave prefetch unlimited (0, the default in many clients).
RabbitMQ will push the entire queue into one consumer's memory. Typical
values: 1 for slow, uneven jobs; 10-300 for fast, uniform messages where
round-trip latency matters more. Measure, because the right number
depends on processing time versus network latency.
Prefetch also bounds how much work is "in flight" per consumer, which is how much gets redelivered on a crash, and how much memory each consumer needs.
Consumers: push vs pull
basic_get polls, costs a round trip per message, and isn't bounded by
prefetch. Production consumers use basic_consume.
Ordering
A queue is FIFO, but with several consumers, or with redeliveries,
processing order isn't. A requeued message (with classic queues) goes back
to the head; with quorum queues, returned messages are redelivered first,
but other messages may already be in progress elsewhere. If per-key order
matters, use one consumer per queue, or single active consumer
(x-single-active-consumer: true), where only one consumer receives at a
time and another takes over if it dies.
Lab
- Run
work_queue.pyand explain both prefetch results. - Change the slow worker to 1,000 ms and rerun both. How much worse does prefetch=50 get?
- In the UI, watch
lab.jobsduring the run: Ready vs Unacked. - Declare a queue with
x-single-active-consumer: true, attach three consumers, and see in the UI that only one is active. Kill it and watch another take over.