Kafka error deserializing key/value for partition: causes and fixes
Codemia Knowledge Hub ·
The consumer throws RecordDeserializationException (a subclass of SerializationException) when its configured deserializer cannot turn a record's bytes back into the expected key or value type. Fix it by making the consumer's deserializers match the producer's serializers, checking the key.deserializer and value.deserializer settings, and handling unreadable records so one bad message does not stop consumption.
Clients before Kafka 3.8 print the generic wording:
Kafka 3.8 and later name the part that failed, KEY or VALUE:
Why deserialization fails
Each Kafka message has a key and a value. The producer serializes them to bytes, and the consumer deserializes them back into objects. If the deserializer cannot interpret the bytes, it throws. Common causes:
- Mismatched data formats: the data written to the topic does not match the format the consumer's deserializer expects.
- Wrong configuration: the consumer is set up with the wrong deserializer.
- Corrupted messages: occasionally a message is damaged in transmission or storage.
Fixing the error
- Match serializer and deserializer: the producer's serializer must pair with the consumer's deserializer. A producer using
StringSerializerneeds a consumer usingStringDeserializer. - Check message integrity: look at logs or failure metrics to see whether messages were corrupted.
- Review the consumer configuration: confirm
key.deserializerandvalue.deserializerare set correctly. - Handle bad records: add error handling that logs and skips an unreadable message, or stops the consumer if the errors are systematic. As the error message says, you can seek past the record to continue.
A misconfigured consumer
Here the producer writes string values, but the consumer was set up with IntegerDeserializer for values:
IntegerDeserializer expects exactly 4 bytes, so the first string value makes poll() throw the error above. Change value.deserializer to StringDeserializer so the types line up.
A mismatch does not always throw. ByteArrayDeserializer accepts any bytes, so a consumer using it on string data silently receives raw bytes instead, and the failure shows up later in the code that processes them.
Skipping a bad record
The exception carries the partition and offset of the record that failed, so you can log it and seek past it:
Against the misconfigured consumer above this logged Skipping t1-0 offset 0: Size of data received by IntegerDeserializer is not 4 and moved on to the next record. In Spring Kafka, wrapping the real deserializer in ErrorHandlingDeserializer does the same job without a manual seek.
| Aspect | Detail |
| Error Type | Deserialization Error |
| Causes | Mismatch in serializer/deserializer, corrupted messages, misconfiguration |
| Solutions | Verify serializer compatibility, ensure proper consumer config, handle corrupted data |
| Example of a Mismatch | Using StringSerializer in producer and IntegerDeserializer in consumer |
Related reading
- Kafka Error from SyncGroup, The request timed out
- Kafka Error in I/O java.io.EOFException null
- Kafka expectantly shutting down. License topic could not be created
- kafka failed authentication due to SSL handshake failed
- Kafka failed to map 1073741824 bytes for committing reserved memory
- KAFKA Failed to update metadata after 60000 ms
- Kafka failed to change state for partition from OnlinePartition to OnlinePartition
- Kafka Fetch offset is out of range for partition. How to avoid data loss?
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

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.