SpringSpring Kafka Error Handling
How do you configure retry and dead letter topics in Spring Kafka?
Reference answer
Use non-blocking retries with @RetryableTopic on the @KafkaListener method, for example @RetryableTopic(attempts = "4", backOff = @BackOff(delay = 1000, multiplier = 2), dltTopicSuffix = "-dlt"). attempts is the number of attempts before the message is sent to the DLT (default 3); four attempts with an exponential backoff of 1000 ms and multiplier 2 create the retry topics my-topic-retry-1000, my-topic-retry-2000 and my-topic-retry-4000, plus the DLT my-topic-dlt. A failed message is forwarded to the next retry topic with a backoff timestamp, and when the attempts are exhausted it goes to the DLT. A @DltHandler method in the same class can process DLT records; without one a default handler only logs them. Non-blocking retries lose ordering for the topic and are not supported for batch listeners. The alternative is blocking retries: set a DefaultErrorHandler with a BackOff and a DeadLetterPublishingRecoverer on the container factory with setCommonErrorHandler. That recoverer publishes to the original topic name plus -dlt by default, and the DLT record carries headers with the exception class, message and stack trace and the original topic, partition and offset.
Expected key concepts: @RetryableTopic, attempts, @BackOff, multiplier, dltTopicSuffix, retry topics, DLT, @DltHandler, DefaultErrorHandler, DeadLetterPublishingRecoverer, headers
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.