To handle errors in Kafka consumers in Spring Boot, define a DefaultErrorHandler bean with a back-off policy and a DeadLetterPublishingRecoverer. Spring Kafka then retries a failing record, and once retries are exhausted it publishes the record to a dead-letter topic and moves on. Add deserialization protection with ErrorHandlingDeserializer, and use @RetryableTopic when you need non-blocking retries.
SeekToCurrentErrorHandler and the other legacy error handlers were deprecated and then removed, so DefaultErrorHandler is the current default.
DefaultErrorHandler with back-off@RetryableTopicWhen a @KafkaListener method throws, the listener container passes the exception to its CommonErrorHandler. If you define none, the container uses a DefaultErrorHandler with a FixedBackOff that retries a failing record up to nine more times with no delay, then logs the failure and skips the record. Skipping means the offset advances, so the record is lost unless you recover it elsewhere.
That default is rarely what you want in production. You usually want a short, bounded retry, then a dead-letter topic so that nothing is lost and one bad record does not block the partition forever.
Kafka consumers also differ from queue consumers in one way that shapes error handling: records in a partition are processed in order. Blocking retries therefore delay every record behind the failing one.
DefaultErrorHandler with back-offRegister a DefaultErrorHandler bean. Spring Boot's auto-configured listener container factory picks up a single CommonErrorHandler bean automatically.
This retries with delays of 1, 2, 4, 8 and 16 seconds until 30 seconds have elapsed. addNotRetryableExceptions marks permanent errors, which are sent to the recoverer immediately without retrying. Spring already treats some exceptions as not retryable, such as DeserializationException and MessageConversionException.
For a simple fixed policy, pass new FixedBackOff(1_000L, 3), which retries three times one second apart.
Give the handler a DeadLetterPublishingRecoverer so exhausted records are published to another topic:
By default the recoverer publishes to a topic named after the original with a .DLT suffix, to the same partition number, so the destination topic must have at least as many partitions. The code above makes that mapping explicit. The recoverer adds headers describing the failure, including the original topic, partition, offset and the exception message and stack trace, which makes later diagnosis much easier.
Monitor the dead-letter topic. A topic that nobody reads is only a slower way of losing data. Create an alert on its message rate, and a consumer or admin tool to replay records after you fix the bug.
A record that cannot be deserialized fails before your listener runs, and without protection the consumer can get stuck on it forever. Wrap the real deserializers with ErrorHandlingDeserializer:
The wrapper catches the failure and hands the container a DeserializationException. Because the default handler treats it as not retryable, the record goes straight to the dead-letter topic. Limit trusted.packages to your own packages rather than *.
@RetryableTopicBlocking retries hold up the partition. If failures are common, or retries take minutes, use retry topics so that other records keep flowing:
Spring creates retry topics such as orders-retry-2000 and a dead-letter topic, and forwards failed records between them after the delay. The trade-off is that ordering is no longer guaranteed across a retried record and the records after it.
Spring Kafka commits offsets after a record or batch is processed successfully, and the error handler decides what happens on failure. Leave enable-auto-commit as false, which is the Spring Kafka default, and choose an ack mode only when you need manual control (spring.kafka.listener.ack-mode=manual).
Delivery is at-least-once, so a record may be processed more than once after a rebalance or retry. Make handlers idempotent, for example with a unique constraint on a business id or a processed-event table.
DefaultErrorHandler do by default?It retries a failed record up to nine more times without delay, then logs it and skips it. Supply your own back-off and a recoverer to change that.
DefaultErrorHandler and SeekToCurrentErrorHandler?DefaultErrorHandler replaced SeekToCurrentErrorHandler and the other legacy handlers. It does the same seek-and-retry work and supports both record and batch listeners.
Create a DeadLetterPublishingRecoverer with a KafkaTemplate and pass it to DefaultErrorHandler, or use @RetryableTopic, which creates the dead-letter topic for you.
With DefaultErrorHandler, yes: the partition waits during back-off. With @RetryableTopic, retries run through separate topics, so other records continue.
Define a DefaultErrorHandler with a bounded back-off and a DeadLetterPublishingRecoverer, mark permanent errors as not retryable, and wrap deserializers in ErrorHandlingDeserializer. Use @RetryableTopic for non-blocking retries, monitor the dead-letter topic, and write idempotent listeners because Kafka delivery is at-least-once.