SpringSpring Kafka Error Handling
How do you handle deserialization errors in Spring Kafka?
Reference answer
Wrap the real deserializer in ErrorHandlingDeserializer (org.springframework.kafka.support.serializer.ErrorHandlingDeserializer). In Spring Boot set spring.kafka.consumer.value-deserializer to ErrorHandlingDeserializer and pass the real one as the delegate through the ErrorHandlingDeserializer.VALUE_DESERIALIZER_CLASS consumer property (KEY_DESERIALIZER_CLASS for keys). If the delegate fails, ErrorHandlingDeserializer returns a null value and puts a DeserializationException, which contains the cause and the raw bytes, in a record header. With a record-level listener, when the ConsumerRecord has such a header the container's error handler is called with the failed record and the record is not passed to the listener, so the error handler decides what happens next, for example whether the record is retried or recovered. Alternatively set a failedDeserializationFunction (ErrorHandlingDeserializer.VALUE_FUNCTION) that creates a replacement value to pass to the listener. Since Spring Kafka 3.1 a validator can be configured so validation failures are handled like deserialization failures.
Expected key concepts: ErrorHandlingDeserializer, DeserializationException, VALUE_DESERIALIZER_CLASS, delegate, error handler, header, null value, raw bytes, failedDeserializationFunction
How did that feel?
Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.