0
0
Kafkadevops~10 mins

Dead letter queue pattern in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Dead letter queue pattern
Message Produced
Message Consumed
Process Message
Success
Commit
Dead Letter Queue
Messages are produced and consumed. If processing succeeds, message is committed. If it fails, message is sent to a Dead Letter Queue (DLQ) for later inspection.
Execution Sample
Kafka
message = consumer.poll()
try:
  process(message)
  consumer.commit()
except Exception:
  producer.send('DLQ', message)
  consumer.commit()
Consume a message, try to process it, commit if success, else send to DLQ.
Process Table
StepActionMessage ContentProcessing ResultQueue DestinationOutput
1Poll messageOrder #123N/AMain TopicMessage received
2Process messageOrder #123SuccessMain TopicProcessed successfully
3Commit offsetOrder #123N/AMain TopicOffset committed
4Poll messageOrder #124N/AMain TopicMessage received
5Process messageOrder #124FailureMain TopicProcessing failed
6Send to DLQOrder #124N/ADead Letter QueueMessage sent to DLQ
7Poll messageOrder #125N/AMain TopicMessage received
8Process messageOrder #125SuccessMain TopicProcessed successfully
9Commit offsetOrder #125N/AMain TopicOffset committed
💡 No more messages to consume
Status Tracker
VariableStartAfter Step 3After Step 6After Step 9Final
messageNoneOrder #123Order #124Order #125Order #125
processing_resultNoneSuccessFailureSuccessSuccess
queue_destinationMain TopicMain TopicDead Letter QueueMain TopicMain Topic
Key Moments - 3 Insights
Why do we send the message to the Dead Letter Queue instead of just discarding it?
Because the message failed processing and we want to keep it for later inspection or reprocessing, as shown in execution_table step 6.
What happens if the message processing succeeds?
The message offset is committed to mark it as processed, so it won't be consumed again, as shown in execution_table steps 3 and 9.
Does sending to the DLQ stop the consumer from processing other messages?
No, the consumer continues polling and processing other messages normally, as seen in steps 7-9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the queue destination for the message 'Order #124' after processing fails?
AMain Topic
BDead Letter Queue
CCommitted Offsets
DDiscarded
💡 Hint
Check step 6 in the execution_table where the message is sent after failure.
At which step does the consumer commit the offset for a successfully processed message?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look for 'Commit offset' action in the execution_table.
If the processing of 'Order #125' failed, how would the queue destination change in the execution_table?
AIt would change to 'Dead Letter Queue'
BIt would be 'Committed Offsets'
CIt would remain 'Main Topic'
DIt would be 'Discarded'
💡 Hint
Refer to how failure is handled for 'Order #124' in steps 5 and 6.
Concept Snapshot
Dead Letter Queue Pattern:
- Consume messages from main topic
- Try processing each message
- On success: commit offset
- On failure: send message to DLQ
- DLQ stores failed messages for later handling
Full Transcript
This visual trace shows how the Dead Letter Queue pattern works in Kafka. Messages are consumed from the main topic. Each message is processed. If processing succeeds, the consumer commits the offset so the message won't be reprocessed. If processing fails, the message is sent to a special topic called the Dead Letter Queue (DLQ). This DLQ holds failed messages for later inspection or retry. The consumer continues processing other messages normally. This pattern helps handle errors without losing messages or blocking the consumer.