Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

In Spring Kafka, what does DefaultErrorHandler do when a listener throws an exception, with default settings, and how do you send failed records to a dead-letter topic?

With default settings, DefaultErrorHandler seeks the consumer back so the failed record is delivered again, up to 9 retries (10 attempts in total) with no delay between them. After the last attempt it logs the failure and skips the record. To send failed records to a dead-letter topic, we give the handler a DeadLetterPublishingRecoverer, which publishes the record to <topic>.DLT, to the same partition number, with headers describing the exception.

Why this question is asked

Every Kafka consumer eventually meets a record it cannot process. What happens next decides whether the service gets stuck, loses the record, or parks it somewhere safe. Interviewers ask this question because Spring Kafka's default behaviour surprises people: it retries very fast, then quietly moves on. In this answer, we will see what DefaultErrorHandler does step by step, which exceptions skip retries, how to add a dead-letter topic, what the published record looks like, and how to configure exponential back-off. We will also see why these retries hold up the whole partition.

1What Happens When a Listener Throws

A listener container is the Spring Kafka component that polls records from Kafka and calls our @KafkaListener method for each one. When our method throws an exception, the container hands the failed record, plus every record that came after it in the same poll, to its error handler. Since Spring Kafka 2.8 the default error handler is DefaultErrorHandler.

DefaultErrorHandler does not retry in a loop inside our method. Instead it performs seeks: for each partition with unprocessed records, it tells the consumer to go back to the offset of the first unprocessed record. On the next poll, Kafka returns the failed record again, and our method gets another attempt. Records that came after it in the batch are fetched again too, so nothing is lost and order is kept.

The handler counts attempts per record. Its default back-off is FixedBackOff with an interval of 0 and 9 retries, which means 10 delivery attempts in total with no pause between them. In practice that means ten attempts within a fraction of a second, which rarely helps with a downstream outage.

Step throughDefaultErrorHandler with default settings
  1. Listener throws for offset 42
  2. Is the exception not retryable?
    YesGo straight to the recovererNoCount the attempt
  3. Fewer than 10 attempts so far?
    YesSeek back to 42 and poll againNoGive up on the record
  4. Call the recovererdefault: log the error; with a DLT: publish it
  5. Commit offset 43 and move onthe partition continues

5 steps in order. Play them, or step through them one at a time.

2After Retries: Logged and Skipped

When the tenth attempt fails, the handler calls its recoverer, the component that decides what to do with a record we have given up on. The default recoverer just logs the record and the exception at error level. Then the handler commits the offset after the failed record, so the partition moves on. The failed record is not stored anywhere; if nobody reads that log line, it is effectively lost.

That is often surprising, and it is why most production services configure a dead-letter topic. A dead-letter topic, or DLT, is an ordinary Kafka topic where we park records that could not be processed, so someone can inspect them and replay them later.

3Adding a Dead-Letter Topic

Spring Kafka provides DeadLetterPublishingRecoverer for this. We give it a KafkaTemplate to publish with and pass it to DefaultErrorHandler. With Spring Boot, a DefaultErrorHandler bean (or more generally a CommonErrorHandler bean) is picked up by the auto-configured listener container factory.

Java
@Configuration
class KafkaErrorConfig {
@Bean
DefaultErrorHandler errorHandler(KafkaTemplate<Object, Object> template) {
var recoverer = new DeadLetterPublishingRecoverer(template);
// orders.DLT, same partition number as the failed record
return new DefaultErrorHandler(recoverer, new FixedBackOff(1000L, 2L)); // 3 attempts, 1 s apart
}
}
// A record that fails three times on orders, partition 4, offset 1207:
// published to topic orders.DLT, partition 4
// header kafka_dlt-original-topic = orders
// header kafka_dlt-original-partition = 4
// header kafka_dlt-original-offset = 1207
// header kafka_dlt-exception-fqcn = java.lang.IllegalStateException
// header kafka_dlt-exception-message = Unknown product SKU-77

Notice two defaults. The topic name is the original topic plus .DLT, and the partition is the same partition number as the original record. That means the DLT must have at least as many partitions as the source topic, or publishing fails; we can pass our own destination resolver function to choose a different topic or partition. The headers carry the original coordinates and the exception class, message and stack trace, which is what makes triage possible later.

Notice also that the key and value are published as they were received, so the template's serializers must be able to write them. A common trick is a template that uses ByteArraySerializer for records whose value failed to deserialize.

4Exceptions That Skip the Retries

Some failures will never succeed on a retry. A record that cannot be deserialized, or whose payload cannot be converted to the method's parameter type, will fail the same way all ten times. So DefaultErrorHandler classifies certain exceptions as not retryable by default and sends those records to the recoverer immediately.

The default list includes DeserializationException, MessageConversionException, ConversionException, MethodArgumentResolutionException, NoSuchMethodException and ClassCastException; check your version for the exact set. We can add our own with addNotRetryableExceptions, for example a ValidationException for invalid business data, so that bad input goes straight to the DLT while a timeout gets retried.

Java
handler.addNotRetryableExceptions(InvalidOrderException.class); // straight to the DLT
handler.addRetryableExceptions(SocketTimeoutException.class); // keep retrying these

5Why These Retries Block the Partition

Because the handler seeks back and redelivers the same record, nothing after it in that partition is processed until the record succeeds or is recovered. These are called blocking retries. With the default of no delay that hardly matters, but once we add a back-off of, say, 10 seconds and 5 retries, one bad record holds up its partition for about 50 seconds, and every key in that partition waits.

The upside is that order is preserved: record 43 is never processed before record 42. The downside is lag on that partition during the retries. There is also a limit to keep in mind: the consumer thread waits during the back-off, so each individual delay should stay well below max.poll.interval.ms, which is 5 minutes by default, or the consumer can be considered dead and the group rebalances. When retries need minutes or hours, Spring Kafka offers non-blocking retries with @RetryableTopic, which move the record to separate retry topics instead.

6Follow-up questions

How do you configure exponential back-off?

Pass an ExponentialBackOffWithMaxRetries to the DefaultErrorHandler instead of a FixedBackOff. For example, new ExponentialBackOffWithMaxRetries(5) with setInitialInterval(1000), setMultiplier(2.0) and setMaxInterval(10000) gives retries after 1, 2, 4, 8 and 10 seconds, and then the recoverer runs. The max retries variant is useful because a plain ExponentialBackOff limits total elapsed time rather than the number of attempts. Keep the total within what the partition can afford to stall, and each delay below max.poll.interval.ms.

Does the DLT record keep its original key?

Yes. DeadLetterPublishingRecoverer publishes the original key and value, plus headers with the original topic, partition, offset, timestamp and exception details. Keeping the key matters because a later replay to the original topic will then land on the same partition as before.

What changes for batch listeners?

For a batch listener, the handler does not know which record failed unless we tell it. Throwing BatchListenerFailedException with the index of the failed record lets DefaultErrorHandler commit the records before it, retry from the failed one and eventually recover just that record. Throwing any other exception makes it retry the whole batch.

7Conclusion

In this answer, we saw that DefaultErrorHandler reacts to a listener exception by seeking back and redelivering the record, 10 attempts with no delay by default, and then logs and skips it. Adding a DeadLetterPublishingRecoverer sends exhausted records to <topic>.DLT on the same partition number with exception and origin headers. Deserialization and conversion errors skip retries and go straight to the recoverer. Because the retries are blocking, they keep order but stall the partition, which is why the back-off must be chosen with care.

What to say in the interviewAbout 80 seconds aloud

Open with

When a listener throws, the container passes the failed record and the rest of the batch to DefaultErrorHandler. It doesn't loop inside my method; it seeks the consumer back to the failed offset so the next poll delivers the record again.

Then, if they want more

The default back-off is a FixedBackOff with zero interval and nine retries, so ten attempts with no delay. When they are exhausted, the default recoverer just logs the error and the handler commits past the record, so it is effectively skipped.

To keep failed records, I configure a DeadLetterPublishingRecoverer with a KafkaTemplate and give it to the DefaultErrorHandler, which Spring Boot wires into the listener container factory.

By default it publishes to the topic name plus .DLT, using the same partition number, so the DLT needs at least as many partitions, and it adds headers with the original topic, partition, offset and the exception class, message and stack trace.

Some exceptions, like deserialization or message conversion failures, are classified as not retryable and go straight to the recoverer, and I can add my own validation exceptions to that list.

One thing I always point out is that these retries are blocking: the partition doesn't progress while one record is retried, so with exponential back-off I keep the total delay bounded.

About this answer

Last updated
12 November 2026
Versions it names
Kafka 2.8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In Spring Kafka, what does DefaultErrorHandler do when a listener throws an exception...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/567575c2e3/