To retry failed Kafka messages in Spring Boot, use either a blocking retry with DefaultErrorHandler and a BackOff, or non-blocking retries with @RetryableTopic. Blocking retry re-delivers the failed record on the same partition and pauses everything behind it. @RetryableTopic publishes the failed record to delay topics and finally to a dead-letter topic, so the main topic keeps moving.
This guide targets Spring Boot 3.x with Spring for Apache Kafka 3.x and Java 21. The older SeekToCurrentErrorHandler and RetryTemplate listener setup are removed or deprecated in favour of the APIs below.
Kafka has no per-message retry or acknowledgement queue; a consumer has one offset per partition. If one record keeps failing and you keep retrying it in place, no later record on that partition is processed.
| Feature | Blocking (DefaultErrorHandler) | Non-blocking (@RetryableTopic) |
|---|---|---|
| Retries happen | In the same consumer thread | In separate retry topics |
| Partition blocked while retrying | Yes | No |
| Message ordering | Preserved | Not preserved across retries |
| Extra topics | Optional DLT only | Retry topics plus DLT |
| Good for | Short, transient errors, strict ordering | Slow dependencies, long backoffs |
Use blocking retry with a short backoff when order matters. Use non-blocking retry when the downstream system may be down for seconds or minutes and you cannot stall the whole partition.
DefaultErrorHandler retries a failed record according to a BackOff policy and then calls a recoverer. Spring Boot picks up a CommonErrorHandler bean and applies it to its auto-configured listener container factory.
What this does:
DeadLetterPublishingRecoverer sends the record to <topic>.DLT on the same partition number and the consumer moves on.addNotRetryableExceptions skip retries and go straight to recovery.The dead-letter topic must have at least as many partitions as the source topic when you route to the same partition number, or return a TopicPartition with a partition of -1 to let the producer pick one. Spring also classifies some exceptions as not retryable by default, such as deserialization and message-conversion failures.
Keep total retry time well below max.poll.interval.ms (default 5 minutes), otherwise the broker considers the consumer dead and triggers a rebalance while it is still sleeping between attempts.
@RetryableTopic creates the retry and dead-letter topics for you and routes failures through them with a delay.
With attempts = "4" the record is tried once on payments and up to three more times through retry topics. When the delay changes between attempts, topic names carry the delay by default, for example payments-retry-1000, payments-retry-2000, payments-retry-4000, and the final topic is payments-dlt. You can change naming with topicSuffixingStrategy, and the retry topics are created on startup when a KafkaAdmin bean is present, which Spring Boot provides.
The @Backoff annotation here comes from the spring-retry package, so spring-retry must be on the classpath for the annotation types to resolve in some versions; check the dependency tree if the import fails. The retry timing is applied by pausing the retry topic's partitions until the record's due time, not by sleeping a thread, so the consumer still polls.
The @DltHandler method runs once all attempts are exhausted. If it throws, the default is to retry the DLT handler in a loop, so configure dltProcessingFailureStrategy = DltStrategy.FAIL_ON_ERROR or ALWAYS_RETRY_ON_ERROR deliberately.
When you want one policy for many listeners, define a bean instead of annotating each method.
A single fixed delay results in a single retry topic, such as payments-retry, instead of one topic per attempt.
Any retry strategy delivers a message more than once. Kafka gives at-least-once delivery by default, so a retry after a partial success can repeat a side effect. Protect against that:
retry_topic-attempt header or the topic suffix, and alert when records reach the DLT.@EmbeddedKafka or Testcontainers and a listener that fails a fixed number of times, then assert on the number of invocations and the DLT message.Retry gives a message more chances to succeed. A dead-letter topic is where it goes after retries are exhausted, so it can be inspected and replayed without blocking healthy traffic.
No. A failed record moves to another topic and later messages on the main topic are processed first. Use blocking retry when strict per-key order is required.
Permanent failures such as validation errors, bad payloads and deserialization problems. Retrying them wastes time. Mark them with addNotRetryableExceptions or exclude.
Always set a finite maxAttempts or attempts, and route to a DLT afterwards. A BackOff with no limit retries forever and keeps the partition stuck.
For ordered processing with brief failures, configure DefaultErrorHandler with an exponential BackOff and a DeadLetterPublishingRecoverer. For slow or unreliable dependencies, use @RetryableTopic so retries run on separate topics without blocking the partition. In both cases skip non-retryable exceptions, keep handlers idempotent and monitor the dead-letter topic.