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.
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:
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_ackmeans 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_rejectis 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:
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:
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.
Related reading
- How to reset offsets to arbitrary value in Kafka Consumer Group?
- How to reset user for rabbitmq management
- How to resolve a zookeeper authentication failure when using Kafka with Kerberos
- How to resolve Kafka error Connection to node 0 could not be established?
- How to restart a failed pod in kubernetes deployment
- How to restart apache2 without terminating docker container?
- How to resolve IndexError too many indices for array
- How to restart RabbitMQ service
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.