Skip to content
Kafka Is Fun

SpringSpring Kafka Configuration

What happens if a @KafkaListener throws an uncaught exception?

Reference answer

The exception is thrown to the container. If no common error handler is configured and no transaction manager is used, the container uses a DefaultErrorHandler with its default BackOff, FixedBackOff(0L, 9): the failed record is seeked back and redelivered immediately up to 9 more times (10 attempts), and then the default recoverer logs the record at ERROR level and the container moves on. It does not retry forever. With a custom DefaultErrorHandler you choose the BackOff and a recoverer, for example DeadLetterPublishingRecoverer to publish to a DLT. Exceptions classified as not retryable (DeserializationException, MessageConversionException, ConversionException, MethodArgumentResolutionException, NoSuchMethodException, ClassCastException by default, plus any added with addNotRetryableExceptions) skip retries and go straight to the recoverer. With a transaction manager, no error handler is configured by default: the transaction rolls back and the AfterRollbackProcessor handles redelivery. Only RuntimeException is handled; a subclass of Error bypasses the handler and the consumer terminates. Risk: with an unbounded BackOff the same record blocks its partition and lag builds up, and a skipped (logged) record is lost unless you route it to a DLT.

Expected key concepts: DefaultErrorHandler, FixedBackOff, seek, retry, recoverer, DeadLetterPublishingRecoverer, DLT, addNotRetryableExceptions, AfterRollbackProcessor

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.