Video in progress
How do you configure retries in Spring Kafka when a listener throws an error?
Spring Kafka 2.8+ uses DefaultErrorHandler: you give it a BackOff (for example FixedBackOff or ExponentialBackOff), and a recoverer is called when the attempts run out. Errors where retrying is pointless can be excluded with addNotRetryableExceptions.
📖 Answer
@Bean
public DefaultErrorHandler errorHandler(KafkaTemplate<Object, Object> template) {
var recoverer = new DeadLetterPublishingRecoverer(template);
var backOff = new ExponentialBackOffWithMaxRetries(4);
backOff.setInitialInterval(500);
backOff.setMultiplier(2.0);
backOff.setMaxInterval(5_000);
var handler = new DefaultErrorHandler(recoverer, backOff);
// no point retrying a validation error
handler.addNotRetryableExceptions(IllegalArgumentException.class);
return handler;
}
If Spring Boot sees this bean, it wires it into the container factory automatically.
What happens inside: when an error occurs, the handler seeks back over the remaining records, waits, and hands the failed record over again. This is "blocking retry": the next messages in that partition wait until the retries finish. Order is preserved, but a long backoff stalls the partition.
If you see SeekToCurrentErrorHandler or setRetryTemplate(...) on the container factory in old code, they were deprecated in 2.8 and removed in 3.0.
Make sure the total backoff time doesn't exceed max.poll.interval.ms.
Sign up to mark studied questions and save your progress.