Replay only some RabbitMQ dead letters: by exception, header or routing key
Originally published at warrenops.io . The tax service was down for twenty minutes and is back. orders.dlq holds 230 messages: 41 of them failed with TaxServiceUnavailableException , the rest are missing orders and declined payments that a replay would only send round again. RabbitMQ has no "move the messages where header X equals Y": a queue hands out messages from the head, one after the other,…
On a single day, the tax service went down for twenty minutes. Upon its return, the orders.dlq queue contained 230 messages. Out of these messages, 41 were due to TaxServiceUnavailableException, while the rest were missing orders and declined payments that a replay would reprocess. Unfortunately, RabbitMQ lacks the ability to move messages based on message headers. The queue hands out messages sequentially from its head, and the management UI provides move messages and shovel functionalities that process all messages.
Here's how to selectively replay only the 41 messages with delivery tags 1 through 41, using RabbitMQ 4.3.6 with both classic and quorum queues. The provided script demonstrates the process.
The first attempt using basic_get with auto_ack set to False would lead to an endless loop. The script attempts to process each message, checks if it meets the criteria, and then either requeues the message with nack (not acknowledged) or publishes it to the work queue. To avoid requeuing messages, the script must keep the skipped messages unacknowledged while iterating through the queue.
Once the iteration is complete, a single basic_nack call with multiple set to true releases all the unacknowledged messages back to the dead-letter queue. The script prints the number of messages moved from the dead-letter queue to the work queue.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.