Repetições não bloqueantes com tópicos de repetição
Aprenda a usar @RetryableTopic do Spring Kafka para realizar repetições não bloqueantes e com atraso, mantendo seu consumidor responsivo enquanto as mensagens com falha são processadas novamente.
Repetições não bloqueantes com tópicos de repetição é uma aula grátis de Advanced Spring Boot 4: Event-Driven Architecture (Kafka) no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Advanced Spring Boot 4: Event-Driven Architecture (Kafka), e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Advanced Spring Boot 4: Event-Driven Architecture (Kafka) inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
The Problem with Blocking Retries
Blocking retries (Spring Retry) pause the consumer thread between attempts. A long backoff blocks the partition, stalling all later records.
Non-blocking retries solve this by moving failed records to dedicated retry topics.
How Retry Topics Work
When processing fails, the record is forwarded to a retry topic with a delay. A separate listener consumes the retry topic after the delay and tries again.
- The main consumer keeps moving forward.
- Each retry level can have a longer delay.
Enabling RetryableTopic
Annotate your listener with @RetryableTopic. Spring auto-creates the retry and DLT topics.
@RetryableTopic
@KafkaListener(topics = "orders")
public void handle(Order order) {
paymentService.charge(order);
}Configuring Attempts and Backoff
Customize the number of attempts and the delay strategy directly in the annotation.
@RetryableTopic(
attempts = "4",
backoff = @Backoff(delay = 1000, multiplier = 2.0))
@KafkaListener(topics = "orders")
public void handle(Order order) { /* ... */ }Generated Topic Names
By default Spring creates topics like orders-retry-0, orders-retry-1, and finally orders-dlt.
Each retry topic corresponds to one backoff level.
Exponential vs Fixed Backoff
Set multiplier for exponential growth, or omit it for fixed delays.
- Fixed: 1s, 1s, 1s.
- Exponential: 1s, 2s, 4s.
Exponential is better for transient downstream outages.
Selecting Retryable Exceptions
Only retry exceptions that are transient. Use include or exclude to control which exceptions trigger retries.
@RetryableTopic(
include = { RemoteServiceException.class },
exclude = { ValidationException.class })
@KafkaListener(topics = "orders")
public void handle(Order order) { /* ... */ }Handling the DLT
After all attempts fail, the record lands on the DLT. Add a @DltHandler method to react.
@DltHandler
public void handleDlt(Order order) {
log.error("Order permanently failed: {}", order.getId());
}Reading Retry Headers
Retry records carry headers such as the attempt count and original timestamp, letting you log how far a message progressed.
@Header(KafkaHeaders.ORIGINAL_TOPIC) String originalTopicBlocking vs Non-Blocking
Use blocking retries for very short, immediate transients; use non-blocking retry topics when delays are longer and partition throughput matters.
Putting It Together
Non-blocking retries keep consumers responsive while still giving failed messages multiple chances. Combine @RetryableTopic, exponential backoff, exception filtering, and a @DltHandler.
Quick Check
Test your understanding of retry topics.
Recap
You learned non-blocking retries.
@RetryableTopiccreates retry topics automatically.- Configure attempts, backoff, and a multiplier.
- Filter retryable exceptions with include/exclude.
- Use
@DltHandlerfor permanently failed records.
Perguntas Frequentes
A aula “Repetições não bloqueantes com tópicos de repetição” é grátis?
Sim — o texto completo de “Repetições não bloqueantes com tópicos de repetição” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Advanced Spring Boot 4: Event-Driven Architecture (Kafka), atualize para CoddyKit PRO. O curso de Advanced Spring Boot 4: Event-Driven Architecture (Kafka) inclui 4 aulas no total.
O que vou aprender em “Repetições não bloqueantes com tópicos de repetição”?
Aprenda a usar @RetryableTopic do Spring Kafka para realizar repetições não bloqueantes e com atraso, mantendo seu consumidor responsivo enquanto as mensagens com falha são processadas novamente. Você pratica Advanced Spring Boot 4: Event-Driven Architecture (Kafka) com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
Nenhuma experiência prévia é necessária. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Repetições não bloqueantes com tópicos de repetição”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
Sim. Cada aula de Advanced Spring Boot 4: Event-Driven Architecture (Kafka) inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Tratamento de exceções dos consumidores
- Mecanismos de novas tentativas com o Spring Retry
- Implementação de tópicos de mensagens mortas (DLT)
- Repetições não bloqueantes com tópicos de repetição