Skip to content
Kafka Is Fun

SpringSpring Kafka Error Handling

What is the difference between DefaultErrorHandler and CommonErrorHandler in Spring Kafka?

Reference answer

CommonErrorHandler is the interface. Since Spring Kafka 2.8 it supersedes the legacy ErrorHandler and BatchErrorHandler interfaces and can handle errors for both record and batch listeners, so one listener container factory can create containers for both kinds. DefaultErrorHandler (also since 2.8) implements CommonErrorHandler and replaces SeekToCurrentErrorHandler and RecoveringBatchErrorHandler. You set it on the container factory with factory.setCommonErrorHandler(new DefaultErrorHandler(recoverer, backOff)), or in Spring Boot by declaring it as a @Bean. It redelivers a failed record according to a BackOff (the default is 9 retries with no delay, so ten attempts) and then passes the record to a recoverer (by default the record is only logged; a DeadLetterPublishingRecoverer can publish it to a dead-letter topic). Some exceptions, such as DeserializationException, are treated as not retryable by default, and you can change the classification with addRetryableExceptions and addNotRetryableExceptions. Non-blocking retries with @RetryableTopic are a separate mechanism and are not supported with batch listeners.

Expected key concepts: CommonErrorHandler, DefaultErrorHandler, Spring Kafka 2.8, SeekToCurrentErrorHandler, setCommonErrorHandler, BackOff, recoverer, DeadLetterPublishingRecoverer, addRetryableExceptions, addNotRetryableExceptions, batch listeners

How did that feel?

Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.