Skip to the page

RabbitMQ · mid

RabbitMQ consumers: acks, prefetch, retries and dead-lettering

A consumer must acknowledge messages. Without a prefetch limit one consumer can hoard thousands of messages, and a requeue loop can burn CPU. Retries need delays and a limit.

The notes

What you must know

  • Unacked means delivered to a consumer but not yet acknowledged. Unacked messages still belong to the queue: if the channel or connection closes, they return to ready and are redelivered.
  • Without basic_qos (prefetch) the broker pushes as many unacked messages as it can to the consumer it is feeding. One worker hoards thousands while others sit idle. Set prefetch from the processing time, and measure.
  • Nack or reject with requeue=true for every failure creates a hot loop: a message that can never succeed is delivered again at once. Do not sleep for 30 minutes inside a consumer: it holds the message and hits the consumer timeout. Do not turn on auto_ack to stop a loop: you lose work on a crash.
  • Split errors. Permanent ones (bad JSON, missing file) are rejected with requeue=false, so the queue's dead-letter exchange takes them. Temporary ones are retried later with a delay and a limit.
  • Classic delayed retry: wait queues with x-message-ttl whose dead-letter exchange points back to the main exchange. One wait queue per delay, because messages expire only from the head of a queue. RabbitMQ adds an x-death header on each dead-lettering, with the reason (rejected, expired, maxlen, delivery_limit). Use it or your own header to count attempts.
  • Quorum queues count deliveries and dead-letter a message after the delivery-limit. Classic queues have no such limit.
  • Make jobs idempotent, because redelivery means the same message can run twice. Use a message id or a fixed output path. Long work on the client's I/O thread can block heartbeats, drop the connection and cause redelivery.

The red pen

Common traps: mistake → fix

  • Mistake: Auto-ack stops the loop safely

    Fix: messages are lost on a crash.

  • Mistake: Unacked messages are lost

    Fix: they return to ready when the connection closes.

  • Mistake: Purge the queue or restart RabbitMQ to fix a hoard

    Fix: fix prefetch first.

  • Mistake: Retry by sleeping in the consumer

    Fix: use TTL wait queues or delayed retries.

Easy to hard

Questions interviewers ask, easy to hard

  1. A RabbitMQ queue has 2 million unacked messages. What do you do first?
  2. Review this worker that nacks every error with requeue=true.
  3. How do you retry after 1, 5 and 30 minutes, then park the message?
  4. What is prefetch and how do you choose it?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview