Sub topic 12
Routing failed records to a dead-letter topic
This chapter assumes you have a working @KafkaListener and have read the previous chapter on retries and back-off. You should know that DefaultErrorHandler retries a failing record before giving up.
Why a dead-letter topic
A listener can fail on a record again and again, for example when the payload is invalid or a downstream rule rejects it. If the container retried forever, that one record would block its partition. If it dropped the record, you would lose data silently.
A dead-letter topic (DLT) is a separate topic that holds records the listener could not process. Once retries run out, the container publishes the failed record to the DLT and moves on. The partition keeps flowing, and you can inspect or replay the bad records later.
Configuring the recoverer
DeadLetterPublishingRecoverer is the component that publishes the failed record. You hand it to DefaultErrorHandler as the action to take when retries are exhausted.
package com.example.orders;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.DeadLetterPublishingRecoverer;
import org.springframework.kafka.listener.DefaultErrorHandler;
import org.springframework.util.backoff.FixedBackOff;
@Configuration
public class KafkaErrorHandlingConfig {
@Bean
public DefaultErrorHandler errorHandler(KafkaTemplate<Object, Object> template) {
DeadLetterPublishingRecoverer recoverer = new DeadLetterPublishingRecoverer(template);
return new DefaultErrorHandler(recoverer, new FixedBackOff(1000L, 3L));
}
}FixedBackOff(1000L, 3L) waits one second between attempts and allows three retries. The record is therefore delivered four times in total: the first attempt plus three retries. Spring Boot applies this bean to its auto-configured listener containers, so no further wiring is needed.
What ends up in the DLT
By default the recoverer publishes to a topic named after the original one plus .DLT, so orders becomes orders.DLT. It keeps the original key and value and adds headers that describe the failure:
| Header | Content |
|---|---|
| `kafka_dlt-original-topic` | Topic the record came from |
| `kafka_dlt-original-partition` | Source partition |
| `kafka_dlt-original-offset` | Source offset |
| `kafka_dlt-exception-fqcn` | Exception class name |
| `kafka_dlt-exception-message` | Exception message |
| `kafka_dlt-exception-stacktrace` | Stack trace |
Reading the dead letters
A DLT is an ordinary topic, so you consume it with another listener. This one only logs the record, which is a reasonable starting point before you build an alert or a replay tool.
package com.example.orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
@Component
public class DeadLetterListener {
private static final Logger log = LoggerFactory.getLogger(DeadLetterListener.class);
@KafkaListener(topics = "orders.DLT", groupId = "order-service-dlt")
public void onDeadLetter(OrderEvent event,
@Header(KafkaHeaders.DLT_EXCEPTION_MESSAGE) String reason) {
log.warn("Dead letter for order {}: {}", event.orderId(), reason);
}
}Gotchas
Also check that the DLT publish itself can fail. If the broker rejects it, the record is not committed and the container redelivers it, so monitor the DLT and its producer errors like any other topic.
Exercise
A listener on orders is configured with new FixedBackOff(1000L, 3L) and a DeadLetterPublishingRecoverer. A record always throws an exception. How many times does the listener see the record, and where does it go afterwards?
Show answer
Four times: the first attempt plus three retries, one second apart. After that the recoverer publishes it to orders.DLT (same partition number as the original), and the container commits the offset and continues.