How to skip corrupt (non-serializable) messages in Spring Kafka Consumer?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When consuming messages from a Kafka topic using Spring Kafka, you may encounter corrupt or non-serializable messages that can cause your application to throw serialization exceptions and stop processing further messages. Handling these exceptions effectively ensures that your application remains robust and resilient.
Understanding Serialization in Spring Kafka
Serialization in Kafka involves converting the data in a Kafka message's key and value into bytes to be sent over the network. Deserialization is the reverse process—converting these bytes back into objects when reading messages from a topic.
In Spring Kafka, the KafkaListener uses a ConsumerFactory which in turn configures a Deserializer for the keys and values of the messages. The serialization process needs to match the structure and data type expected by the consumer. If a message does not match the expected format, the deserializer can throw a SerializationException.
Strategies to Handle Corrupt Messages
1. Custom Error Handling with ContainerProperties
Spring Kafka provides ways to handle exceptions using the ContainerProperties configuration on the ConcurrentKafkaListenerContainerFactory. By setting an ErrorHandler or a RecoveryCallback, you can decide how to handle errors such as serialization failures.
Here's how you might configure this in your Spring Kafka consumer:
2. Custom Deserializer
A more proactive approach is to implement your own deserializer which can safely handle corrupt messages and skip them. Here is an example of a custom deserializer:
You then need to configure your Spring Kafka consumer to use this deserializer:
Summary Table
| Strategy | Implementation Level | Pros | Cons |
Error Handling with ContainerProperties | Consumer Configuration | Simple; Uses built-in recovery features | Less control over individual messages |
| Custom Deserializer | Message Deserialization | High control; Can handle message-by-message | More complex to implement |
Conclusion
By implementing one of these strategies, you can enhance the robustness of your Spring Kafka application, ensuring it can handle corrupt or non-serializable messages gracefully without impacting overall message processing. Whether you choose to handle errors after they occur or preemptively filter out problematic messages, depends largely on your specific application needs and constraints.
Related reading
- How to skip record which produces runtime exception in Kafka and keep stream running?
- How to slow down or set given speed on the Kafka stream consumer?
- How to specify a byte-array serializer/deserializer in Kafka-Python
- how to specify consumer group in Kafka Spark Streaming using direct stream
- How to skip weekends while adding days to LocalDate in Java 8?
- How to Solve 403 Error in Spring Boot Post Request
- How to solve -Cannot use import statement outside a module in AWS lambda console
- How to solve ' CUDA out of memory. Tried to allocate xxx MiB' in pytorch?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.