Scrivio Interview library
CodingIntermediate6 min read · updated 13 November 2026

Spring Kafka can retry a failed record later through extra topics instead of holding up the partition it came from.

Configure non-blocking retries for a Spring Kafka listener on the topic payments: four attempts in total with delays of 2, 10 and 50 seconds, no retries for a ValidationException, and a handler for records that end in the dead-letter topic. Show the annotations, name the topics that get created, and explain how Spring delays a record without blocking the main topic.

We put @RetryableTopic(attempts = "4", backoff = @Backoff(delay = 2000, multiplier = 5.0, maxDelay = 60000), exclude = ValidationException.class) on the @KafkaListener(topics = "payments") method and add a @DltHandler method in the same class. Spring creates one retry topic per distinct delay, such as payments-retry-2000, payments-retry-10000 and payments-retry-50000 (or index suffixes, depending on topicSuffixingStrategy and version), plus payments-dlt. A failed record is published to the next retry topic with headers holding the attempt count and due time, the main consumer moves on at once, and the retry consumer pauses its partition until the record is due.

Why this question is asked

Blocking retries hold up a whole partition while one record waits, which is painful when a downstream payment provider is slow for a minute. Spring Kafka's non-blocking retries solve that by moving the failed record out of the way into retry topics. Interviewers ask this coding question to check that we can write the annotations correctly, know what topics appear in the cluster, and understand how a delay works in a system that has no delayed delivery. In this answer, we will write the listener and the dead-letter handler, list the topics Spring creates, follow a failed record through the retries, explain how the delay is implemented, and finish with exclusions and the ordering trade-off.

1The Listener and the Dead-Letter Handler

Four attempts in total means the first delivery plus three retries. The delays are 2, 10 and 50 seconds, which is an exponential back-off starting at 2,000 milliseconds with a multiplier of 5: 2,000, then 10,000, then 50,000. A maxDelay of 60,000 caps any further growth. ValidationException is excluded, so it is never retried.

Java
@Component
public class PaymentsListener {
@RetryableTopic(
attempts = "4", // 1 delivery + 3 retries
backoff = @Backoff(delay = 2000, multiplier = 5.0, maxDelay = 60000), // 2 s, 10 s, 50 s
exclude = ValidationException.class, // straight to the DLT
numPartitions = "12", replicationFactor = "3") // for the topics Spring creates
@KafkaListener(topics = "payments", groupId = "payment-processor")
public void onPayment(PaymentEvent event) {
gateway.capture(event); // may throw a timeout or ValidationException
}
@DltHandler
public void onDeadLetter(PaymentEvent event,
@Header(KafkaHeaders.RECEIVED_TOPIC) String topic, // payments-dlt
@Header(KafkaHeaders.EXCEPTION_MESSAGE) String error) {
log.error("Payment {} failed for good: {}", event.id(), error);
incidents.open(event, error); // a person looks at it
}
}

In Spring Boot this works with the auto-configured KafkaTemplate; in a plain Spring application the retry topic support must be enabled, for example with @EnableKafkaRetryTopic (check your version). The dead-letter handler receives the record plus headers that Spring adds, including the exception message and the original topic, partition and offset.

2The Topics That Get Created

Spring creates one retry topic per distinct delay, plus a dead-letter topic, all named from the main topic. By default the retry suffix is -retry and the dead-letter suffix is -dlt. How each retry topic is told apart depends on topicSuffixingStrategy: with SUFFIX_WITH_DELAY_VALUE the delay is appended, and with SUFFIX_WITH_INDEX_VALUE an index is appended. The defaults have changed across Spring Kafka versions, so check your version's naming, but with delay values the four topics would look like the table below.

By default Spring also creates the topics automatically at startup. In production we either set numPartitions and replicationFactor on the annotation, as above, or set autoCreateTopics = "false" and create the topics ourselves through the normal provisioning pipeline.

Topics for payments with 2, 10 and 50 second delays
Topic (delay-value suffixes)RoleDelay before retry
paymentsmain topic, attempt 1none
payments-retry-2000attempt 22 s
payments-retry-10000attempt 310 s
payments-retry-50000attempt 450 s
payments-dltdead letters, @DltHandlernone
With index suffixes the retry topics are numbered instead, for example payments-retry-0 to payments-retry-2.

3Following a Failed Record

Say the gateway times out on payment p-71 at 12:00:00. The listener throws. Instead of seeking back, Spring publishes p-71 to payments-retry-2000, adding headers that record the attempt count, the original topic and timestamp, and the time the record becomes due, 12:00:02. The main consumer commits and moves on to the next payment immediately, so the payments partition does not stall.

The consumer of payments-retry-2000 receives p-71 almost at once. If it fails again at 12:00:02, Spring publishes it to payments-retry-10000, due at 12:00:12, and then to payments-retry-50000, due at 12:01:02. If the fourth attempt fails too, the record goes to payments-dlt and the @DltHandler method runs.

Step throughA payment through the retry topics
paymentsListenerretry-2000retry-10000payments-dlt
p-71 attempt 1
timeout: publish, due +2 s
attempt 2 when due
timeout: publish, due +10 s
attempt 3 when due
attempt 4 via retry-50000 (not shown)
still failing: publish to DLT
@DltHandler runs

8 messages in order. Play them, or step through them one at a time.

4How Spring Delays Without Blocking

Kafka has no delayed delivery: a record is available to consumers as soon as it is written. So Spring implements the delay on the consumer side. When the retry consumer reads p-71 and its due time has not arrived yet, it does not process it. Instead it pauses that partition of the retry topic and seeks back to p-71, so the record will be fetched again. When the due time arrives, the container resumes the partition and the record is processed.

Pausing is safe because every record in one retry topic has the same delay. Records are appended in the order they failed, so their due times are in the same order: if p-71 is not yet due, nothing behind it is due either. This is exactly why Spring creates one topic per distinct delay; mixing delays in one topic would let a 50-second record block a 2-second one behind it.

5Exclusions and the Ordering Trade-Off

ValidationException is in exclude, so a payment that fails validation skips the retry topics and goes straight to payments-dlt. Retrying a validation error three times over a minute would only delay the inevitable. We can also use include to list the only exceptions that should be retried, which is often safer: retry timeouts and 503s, dead-letter everything else.

The big trade-off is ordering. Non-blocking retries take a failed record out of its partition's order, so a later record with the same key can be processed before the retry succeeds. For independent payments that is fine. If a later record depends on an earlier one, for example a refund that must follow its capture, non-blocking retries are wrong, and blocking retries or an ordered retry design are needed.

6Follow-up questions

What happens if the retry topic's consumer is down for an hour?

The records simply wait in the retry topic. When the consumer comes back, every record is past its due time and is processed at once. Delays are minimums, not exact times.

Can we use a fixed delay instead?

Yes, with @Backoff(delay = 5000) and no multiplier. Since all retries then share one delay, recent Spring Kafka versions can use a single retry topic for all attempts (see sameIntervalTopicReuseStrategy), instead of one topic per attempt.

7Conclusion

In this answer, we configured non-blocking retries for payments with @RetryableTopic, four attempts, an exponential back-off of 2, 10 and 50 seconds, ValidationException excluded, and a @DltHandler for the end. Spring creates one retry topic per distinct delay plus a -dlt topic, named by suffix strategy. A failed record is republished with headers holding its attempt and due time, the main consumer moves on, and the retry consumer pauses and seeks back until the record is due, which works because every record in a retry topic shares the same delay. Exclusions skip straight to the dead-letter topic, and the whole approach breaks per-key order, so it fits independent records only.

What to say in the interviewAbout 90 seconds aloud

Open with

I would annotate the @KafkaListener method for topic payments with @RetryableTopic, setting attempts to 4, which is the first delivery plus three retries, and backoff as @Backoff with delay 2000, multiplier 5.0 and maxDelay 60000, which gives 2, 10 and 50 seconds.

Then, if they want more

I would add exclude equals ValidationException.class so validation failures skip the retries, and set numPartitions and replicationFactor or create the topics myself. In the same class I would add a @DltHandler method that receives the record and its exception headers, logs it and opens an incident.

Spring creates one retry topic per distinct delay plus a dead-letter topic, named from the main topic with a -retry suffix plus the delay value or an index, depending on topicSuffixingStrategy, and -dlt, so payments-retry-2000, -10000, -50000 and payments-dlt with delay suffixes; I would check the version's defaults.

On failure, the record is published to the next retry topic with headers for the attempt count, original topic and due time, and the main consumer moves on at once. The retry consumer reads the record, and if it is not due yet, pauses that partition and seeks back, resuming at the due time.

Since all records in one retry topic share the same delay, they become due in order. The catch is that this breaks per-key ordering, so I would use it only where later records do not depend on earlier ones.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Configure non-blocking retries for a Spring Kafka listener on the topic payments: four...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/67fc74a3bb/