Retry non bloccanti con i retry topic
Impari a usare @RetryableTopic di Spring Kafka per eseguire retry non bloccanti e ritardati, mantenendo reattivo il consumer durante la rielaborazione dei messaggi falliti.
Retry non bloccanti con i retry topic è una lezione Advanced Spring Boot 4: Event-Driven Architecture (Kafka) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Advanced Spring Boot 4: Event-Driven Architecture (Kafka), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Advanced Spring Boot 4: Event-Driven Architecture (Kafka) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Retry non bloccanti con i retry topic» è gratuita?
Sì — il testo completo di «Retry non bloccanti con i retry topic» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Advanced Spring Boot 4: Event-Driven Architecture (Kafka), passa a CoddyKit PRO. Il corso Advanced Spring Boot 4: Event-Driven Architecture (Kafka) include 4 lezioni in totale.
Cosa imparerò in «Retry non bloccanti con i retry topic»?
Impari a usare @RetryableTopic di Spring Kafka per eseguire retry non bloccanti e ritardati, mantenendo reattivo il consumer durante la rielaborazione dei messaggi falliti. Eserciti Advanced Spring Boot 4: Event-Driven Architecture (Kafka) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
Non è richiesta alcuna esperienza precedente. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Retry non bloccanti con i retry topic»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?
Sì. Ogni lezione Advanced Spring Boot 4: Event-Driven Architecture (Kafka) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Gestione delle eccezioni dei consumatori
- Meccanismi di retry con Spring Retry
- Implementazione dei topic di messaggi non recapitabili (DLT)
- Retry non bloccanti con i retry topic