RabbitMQ
Message Queue
Requeue Messages
Server Management
Tech Tutorial

How to requeue messages in RabbitMQ (with retries and delay)

Codemia Knowledge Hub ·

Consume with manual acknowledgements and, when processing fails, call basic_nack(delivery_tag=..., requeue=True). RabbitMQ then puts the message back on the queue. Requeueing is a decision the consumer makes about a delivery, not a separate queue operation, so it only works if the consumer acknowledges or rejects deliveries itself.

Turn off auto_ack

With auto_ack enabled, RabbitMQ considers a message handled as soon as it is delivered. The broker has already dropped responsibility for it, so there is nothing left to requeue after a failure. Use manual acknowledgements instead.

python
1import pika
2
3connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
4channel = connection.channel()
5channel.queue_declare(queue="jobs")

When consuming, set auto_ack=False so your code can decide whether to ack, nack or reject each message.

Requeue with basic_nack

The most common pattern is a negative acknowledgement that asks RabbitMQ to requeue:

python
1def callback(ch, method, properties, body):
2    try:
3        print("processing", body.decode())
4        raise RuntimeError("temporary failure")
5    except RuntimeError:
6        ch.basic_nack(delivery_tag=method.delivery_tag, requeue=True)
7
8channel.basic_consume(queue="jobs", on_message_callback=callback, auto_ack=False)
9channel.start_consuming()

With requeue=True, the message goes back to the queue instead of being discarded. Another consumer, or the same one later, may receive it again.

ack, nack and reject

  • basic_ack means processing succeeded.
  • basic_nack(..., requeue=True) means processing failed and the message should return to the queue.
  • basic_nack(..., requeue=False) means processing failed and the message should not return to the same queue.
  • basic_reject is similar to a negative acknowledgement for a single message.

For a single message, basic_reject is fine. For broader control, basic_nack is usually more flexible.

Requeue with a delay to avoid redelivery loops

Immediate requeue is dangerous when the failure is not transient. If every consumer keeps requeueing the same poison message, the queue churns endlessly, consumer capacity is wasted and logs fill up.

A safer design sends failed messages to a dedicated retry queue with a TTL and a dead-letter exchange. The message leaves the main queue, waits, and is routed back later:

python
1channel.exchange_declare(exchange="retry-exchange", exchange_type="direct")
2channel.queue_declare(
3    queue="jobs.retry",
4    arguments={
5        "x-message-ttl": 10000,
6        "x-dead-letter-exchange": "",
7        "x-dead-letter-routing-key": "jobs",
8    },
9)
10channel.queue_bind(queue="jobs.retry", exchange="retry-exchange", routing_key="jobs.retry")

Here messages sit in jobs.retry for 10 seconds (x-message-ttl), then dead-letter back to jobs, so an unrecoverable message does not hammer the main queue continuously.

Count retries in headers

A plain requeue gives consumers no way to tell a first attempt from a tenth. One common approach is to republish the message with a retry counter in its headers and route it through the retry queue:

python
1def republish_with_retry(ch, body, headers):
2    retries = headers.get("x-retries", 0) + 1
3    ch.basic_publish(
4        exchange="retry-exchange",
5        routing_key="jobs.retry",
6        body=body,
7        properties=pika.BasicProperties(headers={"x-retries": retries}),
8    )

That lets you enforce a rule such as "retry three times, then dead-letter permanently." Note that republishing is not the same as broker requeueing: they have different semantics and metadata behavior.

When requeueing is the right tool

Requeueing suits short-lived failures: a temporary database lock, a downstream timeout, a deployment restart window. It is the wrong default for bad input, schema mismatches or any message that can never succeed. Those should usually be dead-lettered, logged and handled separately rather than requeued on every exception.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design