Kafka - Deserializing the object in Consumer
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, a robust event streaming platform, enables developers to build real-time data pipelines and streaming applications. Consumers, a core component in Kafka, are used to read data from Kafka topics. Efficient deserialization of data is crucial for consumer applications to interpret messages correctly.
What is Deserialization?
Deserialization is the process of converting byte stream data back into its original data structure or object. Kafka stores and transmits data in byte arrays for both keys and values of messages. When consuming messages, Kafka needs to transform these byte arrays back into usable data formats (objects, strings, etc.), which is where deserializers come into play.
Kafka Consumer and Deserialization
When setting up a Kafka Consumer, you must specify how to deserialize the keys and values of incoming messages. This specification is necessary because Kafka itself is agnostic of the content type of your messages—it only deals with raw bytes.
Configuring Kafka Consumer for Deserialization
Here is an example of how you might set up a Kafka consumer with specific deserializers in Java:
In this setup, both the key and the value are deserialized using StringDeserializer. This means the consumer expects both the key and value of the messages to be strings.
Common Deserializers
Apache Kafka provides several built-in deserializers, including:
- StringDeserializer: Converts bytes into a string.
- IntegerDeserializer: Converts bytes into an integer.
- LongDeserializer: Converts bytes into a long.
- ByteArrayDeserializer: Returns the raw byte array, which is useful if you want to handle the bytes manually.
Custom Deserializers
Sometimes, built-in deserializers do not suffice because you might be working with complex data types or you need a specific deserialization logic. Kafka allows you to implement your own deserializer by implementing the Deserializer interface.
Here is a simple example of a custom deserializer:
This custom deserializer can then be configured in your consumer:
Key Points Summary
| Key Aspect | Description |
| Purpose of Deserialization | Converts byte stream from Kafka topics back into objects. |
| Configuration | Set properties for key.deserializer and value.deserializer. |
| Built-in Deserializers | Kafka provides deserializers like StringDeserializer, IntegerDeserializer, etc. |
| Custom Deserializers | Implement the Deserializer interface for custom deserialization needs. |
Handling Schema Evolution
Deserializers must handle data encoded by older or different versions of serializers, especially in production environments where schemas can evolve. Techniques such as versioning, backward compatibility checks, and using serialization frameworks like Avro, which include schema information during serialization, can help manage this complexity.
Conclusion
Proper deserialization in Kafka consumers is essential for accurately interpreting the data transmitted across a Kafka cluster. By understanding and implementing the right deserialization methods, developers can ensure that their Kafka-based applications are robust, scalable, and maintainable. Whether using built-in deserializers or creating custom ones, the flexibility and performance of Kafka's deserialization allow for the efficient processing of high volumes of real-time data in diverse application scenarios.
Related reading
- Kafka - difference between Log end offset(LEO) vs High Watermark(HW)
- Kafka - Docker - Error when sending message from Host to Container (Batch Expired)
- KAFKA - ERROR Failed to write meta.properties due to (kafka.server.BrokerMetadataCheckpoint)
- Kafka - fetch.max.wait.ms - how does it behave with multiple partitions?
- Kafka -> Flink DataStream -> MongoDB
- Kafka - How to commit offset after every message using High-Level consumer?
- Kafka - How to use filter and filternot at the same time?
- Kafka - how to use @KafkaListener(topicPattern=${kafka.topics}) where property kafka.topics is ''sss.*''?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.