Video in progress

Staff 🌱 Spring

What do you do with messages that fail in a Spring Kafka consumer: how do you pick between retry, retry topics, a DLT and message ordering?

Blocking retry stops the partition but keeps ordering; non-blocking retry topics don't stop the consumer but break ordering. Which one you take depends on how much ordering matters for the business. At the end you always need a DLT and someone watching it.

📖 Answer

First I split errors into two kinds:

  • Transient: the database is busy, an external service returns 503. A retry helps.
  • Permanent: broken JSON, a message that fails validation, a bug in the code. No matter how many times you retry, the result is the same, so it goes straight to the DLT.

Blocking retry (DefaultErrorHandler + ExponentialBackOff): the consumer stays on that offset and retries again and again. Ordering within the partition is preserved, but a single "poison" message stops the whole partition and consumer lag grows. So the number of attempts and the total time must be bounded, and permanent errors must be skipped right away with addNotRetryableExceptions(...).

Non-blocking retry (@RetryableTopic): the message is moved to topics like orders-retry-0, orders-retry-1..., and the main flow keeps going. Throughput is good, but a later message with the same key can be processed before it.

@RetryableTopic(attempts = "4",
    backoff = @Backoff(delay = 1000, multiplier = 4),
    exclude = {DeserializationException.class, ValidationException.class})
@KafkaListener(topics = "payments")
void on(PaymentEvent e) { ... }

@DltHandler
void dlt(PaymentEvent e) { alerts.notify(e); }

How I choose:

Situation Choice
Account balances, state machines: ordering matters Blocking retry, short; then DLT, plus logic to "hold" later messages for the same key
Notifications, indexing: ordering doesn't matter Retry topics
An external service has been down for a long time No retry: pause the consumer (circuit breaker + pause())

A DLT is not a dumping ground. It needs an alert, headers with the failure reason (Spring adds them automatically), a tool to inspect and replay, and a team that owns it. A very common situation on prod: a DLT exists, nobody looks at it, and it turns into a "data graveyard".

And everywhere the consumer must be idempotent: because of retries and rebalances, the same message arriving twice is normal.