Lesson 3 of 9 · 26m
Consumers, Acknowledgements, Prefetch, and the Work-Ownership Boundary
Control when work is considered done using manual acknowledgements, delivery tags, redelivery behavior, and prefetch tuning.
The most dangerous line in a RabbitMQ consumer is the one that acknowledges too early. I have seen payment workers ack before writing the database row because "the job was basically done." Then the process died, the row never existed, and the message was gone. Acknowledgement is not a performance detail. It is the exact boundary where ownership moves from broker to application.
Manual acknowledgements are the production default
With manual acknowledgements, RabbitMQ keeps an unacknowledged delivery until the consumer says what happened:
def callback(ch, method, properties, body):
try:
process_message(body)
except RetryableError:
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
except Exception:
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
raise
else:
ch.basic_ack(delivery_tag=method.delivery_tag)Use requeue=True only when immediate retry is genuinely useful. A poison message requeued in a tight loop can consume all capacity while looking like a very busy worker. Usually the safer pattern is requeue=False combined with a dead-letter retry design, which the next module covers.
Delivery tags are channel-scoped facts
A delivery tag identifies a delivery on that channel. It is not a global message ID. Do not ack on a different channel, reconstruct tags after reconnect, or assume a redelivered copy has the same tag. If you use multiple=True, you acknowledge every delivery up to that tag on the channel — convenient, but dangerous if concurrency is sloppy.
safe rule: one worker channel, controlled prefetch, ack exactly onceClient libraries often hide channel behavior behind convenient abstractions. Read what they actually do. Async consumers that process thousands of messages concurrently can easily ack out of order or crash with uncommitted state.
Prefetch controls how much damage a worker can hold
basic.qos limits unacknowledged messages a consumer may hold:
channel.basic_qos(prefetch_count=20)A prefetch of 1 favors strict fairness and slow, careful processing. A higher prefetch improves throughput when work is fast and uniform. There is no universal correct number. The question is: how much work can this consumer responsibly hold hostage if it dies?
A useful starting model:
fast idempotent task (<50 ms) prefetch 50-200
moderate CPU or DB work prefetch 10-30
slow external API work prefetch 1-10
large memory batch processing measure memory per message firstAck after the commit boundary, not after the function starts
The order should be:
receive message
perform work
commit durable side effect
ack deliveryIf the commit succeeds and the ack fails because the connection drops, RabbitMQ redelivers. That is not a bug; it is the cost of safety. Design consumers to tolerate duplicates. If you ack first and then fail before commit, the work is lost. That is the worse trade for almost every business workflow.
Poison loops look like progress
Metrics can lie here. High consumer utilization, constant redeliveries, and low backlog can mean one malformed message is cycling through workers. Watch redelivery counts and consumer error rates together, not just total throughput. A healthy system does not repeatedly process the same bytes at high speed.
Field notes
auto_ack=Trueis appropriate only when losing messages is acceptable; it removes the safety boundary before processing begins.- Consumer cancellation, deployment, and connection loss can all create redelivery even when application code behaved correctly.
- Do not put ack logic in a finally block unless you genuinely want to acknowledge failure.
- RabbitMQ ordering is per queue under normal conditions, but redelivery and competing consumers mean you should not design a business process that requires strict global ordering.
Was this lesson helpful?