Video in progress

Strong Junior 📨 Kafka and messaging

How can a "poison pill" message that can't be deserialized stop a consumer?

If the deserializer fails, the message never reaches the listener and the offset doesn't move forward; the consumer hits the same message on every poll and gets stuck in an endless loop. In Spring the fix is ErrorHandlingDeserializer and a DLT.

📖 Answer

The classic case: someone manually writes hello into the orders topic for a test, while the consumer expects JSON. The logs fill up with the same stack trace, lag grows, and other messages aren't processed either.

In Spring Kafka you wrap the deserializer:

spring:
  kafka:
    consumer:
      value-deserializer: org.springframework.kafka.support.serializer.ErrorHandlingDeserializer
      properties:
        spring.deserializer.value.delegate.class: org.springframework.kafka.support.serializer.JsonDeserializer
        spring.json.trusted.packages: "com.example.events"

Now the deserialization failure reaches the error handler as an exception. DefaultErrorHandler treats DeserializationException as non-retryable by default and (if a DeadLetterPublishingRecoverer is configured) sends the message straight to orders.DLT. The consumer moves on to the next message.

What matters is setting this up before the first incident.