Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Kafka Consumer Retry

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.

like image 417
Punter Vicky Avatar asked Oct 02 '17 19:10

Punter Vicky


People also ask

How do you retry in Kafka?

Retries happen within the consumer poll for the batch. Consumer poll must complete before poll timeout, containing all retries, and total processing time (including REST calls & DB calls), retry delay and backoff, for all records in the batch. Default poll time is 5 minutes for 500 records in the batch.

Does Kafka have Retry?

The retries setting determines how many times the producer will attempt to send a message before marking it as failed. The default values are: 0 for Kafka <= 2.0.

How do you handle failed messages in Kafka?

Processing failed messages can be achieved by cloning the message and republishing it to one of retry topics with updated information about attempt number and next retry timestamp. Consumers of retry topics should block the thread unless it is time to process the message.

How do I turn off Kafka Retry?

In order to disable retries in the STCEH, use a FixedBackOff with 0 retries. The default BackOff is 9 retries (10 attempts) with no back off.


1 Answers

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

like image 89
Artem Bilan Avatar answered Oct 22 '22 22:10

Artem Bilan