Kafka Retry Mechanism with SpringBoot - sumitgupta28/interview-questions-answers GitHub Wiki
@RetryableTopic(include = {NullPointerException.class, ArrayIndexOutOfBoundsException.class},
attempts = "4",
backoff = @Backoff(delay = 1000, multiplier = 2),
topicSuffixingStrategy = TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE,
retryTopicSuffix = "-custom-try",
dltTopicSuffix = "-dead-t")
@KafkaListener(topics = "${kafka.topics.user.name}",
groupId = "${kafka.topics.user.group}",
containerFactory = "userKafkaListenerFactory")
public void consumeUser(ConsumerRecord<String, schema.avro.User> record) {
....
....
....
}
Let’s detail the parameters of annotation:
- value -> Determines which exceptions will be included in the scope of retry when thrown. You can throw any exception extended from the Throwable class here. These are all exceptions whose default value is Throwable.
- include -> It is used with the same logic as value. The only difference is that you can give all exceptions here, except Throwable, including custom exceptions you create yourself.
- exclude -> The opposite of include determines which errors will not be retried when thrown.
- attempts -> Determines how many times the retry operation will be repeated. This includes the first try. The default value is 3.
- backoff -> Determines the expected time between retries. The delay value indicates the milliseconds to wait, and the multiplier value indicates how many times it will increase in the next time.
- autoCreateTopics -> Creates its own retry and dlt topics depending on the rules in annotation. The default value is true. If false is set, you must create the topics yourself, adhering to the rules. After each error, these topics are produced in the same message order. In the next attempt, it is consumed from this topic. Annotation manages this itself. The rules are explained in the following parameters.
- topicSuffixingStrategy -> Takes two different enum values:
-
- TopicSuffixingStrategy.SUFFIX_WITH_DELAY_VALUE -> Default value. According to the example above, 3 retry topics are created as {topic_name}-retry-1000, {topic_name}-retry-2000 and {topic_name}-retry-4000.
-
- TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE -> If this value is given, 3 retry topics will be created as {topic_name}-retry-0, {topic_name}-retry-1 and {topic_name}-retry-2, according to the example above.
- retryTopicSuffix -> You can give the suffix yourself when naming the retry topic. For example, you gave this value as “-custom-try” and your strategy is index value. Then, 3 retry topics are formed: {topic_name}-custom-try-0, {topic_name}-custom-try-1 and {topic_name}-custom-try-2.
- dltTopicSuffix -> Likewise, you can give suffix to your dlt topic. If you do not give this value, your dlt topic will be created as {topic_name}-dlt, if you give the value as “-dead-t”, it will be created as {topic_name}-dead-t.