Video in progress
What is a dead letter topic (DLT), and what do you do with the messages that end up there?
A DLT is a separate topic where messages go when they still haven't been processed after all the retries. The main processing doesn't stop, and the problem messages aren't lost; they can be analyzed later and resent.
📖 Answer
In Spring Kafka, DeadLetterPublishingRecoverer by default sends the message to <topic>.DLT with the original partition number. So the DLT must not have fewer partitions than the original topic, or you need to provide a resolver that picks the partition.
Useful headers get added to the message: kafka_dlt-exception-message, kafka_dlt-exception-stacktrace, kafka_dlt-original-topic, kafka_dlt-original-offset. Those are exactly what you need for analysis.
Don't turn the DLT into a "black box". What you need in practice:
- an alert when a message lands in the DLT (when its size is greater than zero);
- a way to look at the messages: Kafka UI, AKHQ or a plain admin endpoint;
- a "replay" mechanism that resends messages to the original topic after the bug is fixed.
@KafkaListener(topics = "orders.DLT", groupId = "orders-dlt-audit")
public void dlt(OrderEvent event,
@Header(KafkaHeaders.DLT_EXCEPTION_MESSAGE) String error) {
dltRepository.save(new FailedEvent(event.orderId(), error));
}
In one project a DLT quietly filled up for a month and nobody looked at it. With retention at 7 days, the oldest messages were gone for good.
Sign up to mark studied questions and save your progress.