Video in progress

Strong Middle 🧩 Microservices

What do you do when a consumer keeps failing on one "poison" message and the whole partition stalls?

You shouldn't retry the message forever: after a few retries it's moved to a dead letter topic and the consumer goes on to the next message. The DLT is monitored, and once the bug is fixed the messages are resent.

📖 Answer

The classic case: a new version of the producer sent one event in a wrong format. The consumer fails on deserialization, the offset isn't committed, and the same message comes again and again. The thousands of healthy messages behind it in the partition are waiting in line, and the consumer lag keeps growing.

In Spring Kafka:

@Bean
DefaultErrorHandler errorHandler(KafkaTemplate<Object, Object> template) {
    var recoverer = new DeadLetterPublishingRecoverer(template);
    var handler = new DefaultErrorHandler(recoverer, new FixedBackOff(1000L, 3));
    handler.addNotRetryableExceptions(DeserializationException.class,
                                      IllegalArgumentException.class);
    return handler;
}

Here transient errors are retried 3 times, then the message goes to <topic>.DLT. Errors where retrying won't help (a format error) go straight to the DLT.

A deserialization error never reaches the listener, which is why you need an ErrorHandlingDeserializer; otherwise the consumer goes in circles without the error ever reaching the handler.

One catch: the DLT breaks ordering. If OrderPaid goes to the DLT and OrderShipped gets processed, the state may end up wrong. In domains where order matters, you need to hold the later messages with the same key too, or check order using a version.

The DLT is not a "trash can". Alert when a message shows up in it, and have a simple tool for viewing the messages and resending them to the original topic.