Learn Labs
Reliability

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

auto_ack=True"fire and forget": removed on delivery; a crash loses itbasic_ack(tag)done: remove itbasic_ack(tag, multiple=True)done with this and every earlier unacked delivery on the channelbasic_nack(tag, requeue=True)failed: put it back (to the front, for classic queues)basic_nack(tag, requeue=False)failed: drop it, or dead-letter it if the queue has a DLXbasic_reject(tag, requeue=...)same as nack, for one message

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.

lab.jobs queueworker Aworker Bdeliver 0-4 (prefetch 5)A crashes, no ackconnection closed0-4 redelivered=True5-9ack each

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
prefetch=1: finished in 0.9 s60 messages
  • fast worker (10 ms): 56
  • slow worker (200 ms): 4
prefetch=50: finished in 2.1 s60 messages
  • 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.

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_consume(queue, callback)push: RabbitMQ delivers as messages arrive (use this)basic_get(queue)pull: one message per round trip (fine for labs and scripts)

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

  1. Run work_queue.py and explain both prefetch results.
  2. Change the slow worker to 1,000 ms and rerun both. How much worse does prefetch=50 get?
  3. In the UI, watch lab.jobs during the run: Ready vs Unacked.
  4. 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.

On this page