0
0
RabbitMQdevops~10 mins

Dead letter exchanges and queues in RabbitMQ - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Dead letter exchanges and queues
Message sent to normal queue
Message processed?
Ack message
Message sent to dead letter exchange
Dead letter exchange routes to dead letter queue
Message stored in dead letter queue for inspection
Messages go to a normal queue first. If they fail or expire, they move to a special dead letter exchange, which sends them to a dead letter queue for later review.
Execution Sample
RabbitMQ
rabbitmqadmin declare exchange name=dlx type=direct
rabbitmqadmin declare queue name=normal_queue arguments={"x-dead-letter-exchange":"dlx","x-dead-letter-routing-key":"dlx_key"}
rabbitmqadmin declare queue name=dead_letter_queue
rabbitmqadmin declare binding source=dlx destination=dead_letter_queue routing_key=dlx_key

# Publish message with routing_key normal_key
# Reject message from normal_queue
This setup creates a normal queue with a dead letter exchange. Messages rejected from the normal queue go to the dead letter queue.
Process Table
StepActionQueueMessage StateRouting
1Publish messagenormal_queueMessage enqueuednormal_key
2Consume messagenormal_queueMessage delivered to consumernormal_key
3Reject messagenormal_queueMessage rejecteddead letter exchange dlx
4Route messagedlxMessage routed to dead_letter_queuedlx_key
5Message storeddead_letter_queueMessage stored for inspectionnone
💡 Message rejected from normal_queue triggers routing to dead letter exchange and then to dead_letter_queue
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
normal_queueempty1 message0 messages (delivered)0 messages (rejected)0 messages0 messages
dead_letter_queueemptyemptyemptyempty1 message1 message
message_statenoneenqueueddeliveredrejectedroutedstored
Key Moments - 3 Insights
Why does the message go to the dead letter exchange after rejection?
Because the normal queue is configured with the x-dead-letter-exchange argument pointing to the dead letter exchange, rejected messages are automatically routed there as shown in execution_table step 3 and 4.
What happens if the message is acknowledged instead of rejected?
If the message is acknowledged, it is removed from the normal queue and does not go to the dead letter exchange. This is why only rejected messages appear in the dead letter queue (see execution_table step 2 vs step 3).
Can messages expire and still go to the dead letter queue?
Yes, if messages have a TTL (time to live) and expire in the normal queue, they are also routed to the dead letter exchange and then to the dead letter queue, similar to rejection routing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what happens to the message in the normal_queue?
AIt is rejected and sent to dead letter exchange
BIt stays in the normal_queue
CIt is acknowledged and removed
DIt is duplicated
💡 Hint
Check the 'Message State' and 'Routing' columns at step 3 in execution_table
According to variable_tracker, how many messages are in dead_letter_queue after step 4?
A0 messages
B1 message
C2 messages
DNone
💡 Hint
Look at the 'dead_letter_queue' row and 'After Step 4' column in variable_tracker
If the message was acknowledged at step 2 instead of rejected, what would happen?
AMessage would be routed to dead letter queue
BMessage would remain in normal_queue
CMessage would be removed from normal_queue and not routed
DMessage would be duplicated
💡 Hint
Refer to key_moments answer about acknowledgment vs rejection and execution_table steps 2 and 3
Concept Snapshot
Dead letter exchanges (DLX) catch messages rejected or expired from normal queues.
Configure normal queue with x-dead-letter-exchange to specify DLX.
Rejected or expired messages route to DLX, then to dead letter queue.
Dead letter queue stores messages for later inspection or reprocessing.
Useful for debugging message failures or delays.
Full Transcript
Dead letter exchanges and queues help handle messages that cannot be processed normally. When a message is sent to a normal queue, it waits for a consumer. If the consumer rejects the message or if the message expires, the queue sends it to a special exchange called the dead letter exchange. This exchange then routes the message to a dead letter queue. The dead letter queue holds these messages so you can check why they failed or expired. This setup helps keep your system clean and lets you fix problems with messages later. The execution table shows each step: publishing, consuming, rejecting, routing, and storing. The variable tracker shows how message counts change in each queue. Remember, only rejected or expired messages go to the dead letter queue, not acknowledged ones.