Spring Kafka Consumer Retry

Punter Vicky picture Punter Vicky · Oct 2, 2017 · Viewed 13.9k times · Source

I am using Spring Kafka consumer which fetches messages from a topic and persist them into a db. If a failure condition is met , say for instance the db is unavailable , does kafka consumer library provide mechanism to retry ? If it does , is there a way to set different retry intervals such as 1st retry should be done after 5 mins , 2nd after 30 mins , 3rd after 1 hr etc.

Answer

Artem Bilan picture Artem Bilan · Oct 2, 2017

Spring Kafka is shipped with the RetryingMessageListenerAdapter and RetryingAcknowledgingMessageListenerAdapter. If you use @KafkaListener, you can supply AbstractKafkaListenerContainerFactory with the RetryTemplate. And the last one can be injected with any custom RetryPolicy and BackOffPolicy from the Spring Retry project:

https://docs.spring.io/spring-kafka/reference/html/#retrying-deliveries

Also bear in mind that since version 2.0, there is transaction support in Spring Kafka, based on such one in the Apache Kafka 0.11.x.x:

https://docs.spring.io/spring-kafka/reference/html/#transactions