Kafka
Deserialization Error
Key/Value Partition
Data Processing
Debugging Errors

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:

plaintext
org.apache.kafka.common.errors.RecordDeserializationException: Error deserializing key/value for partition t1-0 at offset 0. If needed, please seek past the record to continue consumption.

Kafka 3.8 and later name the part that failed, KEY or VALUE:

plaintext
org.apache.kafka.common.errors.RecordDeserializationException: Error deserializing VALUE for partition t1-0 at offset 0. If needed, please seek past the record to continue consumption.

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

  1. Match serializer and deserializer: the producer's serializer must pair with the consumer's deserializer. A producer using StringSerializer needs a consumer using StringDeserializer.
  2. Check message integrity: look at logs or failure metrics to see whether messages were corrupted.
  3. Review the consumer configuration: confirm key.deserializer and value.deserializer are set correctly.
  4. 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:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test-group");
4props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
5// Incorrect configuration below
6props.put("value.deserializer", "org.apache.kafka.common.serialization.IntegerDeserializer");
7
8KafkaConsumer<String, Integer> consumer = new KafkaConsumer<>(props);

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:

java
1while (true) {
2    try {
3        for (ConsumerRecord<String, Integer> record : consumer.poll(Duration.ofSeconds(1))) {
4            process(record);
5        }
6    } catch (RecordDeserializationException e) {
7        log.warn("Skipping {} offset {}: {}", e.topicPartition(), e.offset(), e.getCause().getMessage());
8        consumer.seek(e.topicPartition(), e.offset() + 1);
9    }
10}

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.

AspectDetail
Error TypeDeserialization Error
CausesMismatch in serializer/deserializer, corrupted messages, misconfiguration
SolutionsVerify serializer compatibility, ensure proper consumer config, handle corrupted data
Example of a MismatchUsing StringSerializer in producer and IntegerDeserializer in consumer

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design