Kafka
Custom Serializer
Data Serialization
Coding
Programming

Writing Custom Kafka Serializer

System Design practice on Codemia

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

Practice system design

Apache Kafka is a popular framework for handling high-throughput, low-latency processing of real-time data feeds. Kafka provides built-in serializers and deserializers for simple data types like strings and integers. However, when dealing with complex types or custom objects, you'll need to create custom serializers and deserializers.

Understanding Kafka Serializers

Kafka uses serializers to convert objects into bytes so that they can be sent over the network efficiently. Correspondingly, deserializers convert these byte arrays back into objects.

Why Write Custom Kafka Serializers?

Custom Kafka serializers are necessary when:

  • The objects to be serialized are not supported by Kafka’s default serializers.
  • Customized serialization logic is needed, perhaps for performance optimizations.
  • Additional processing like compression or encryption is required during serialization.

How to Create a Custom Serializer in Kafka

To create a custom serializer in Kafka, you need to implement the Serializer interface provided by Kafka. Here's a simple example of a custom serializer for a hypothetical User object.

Step 1: Define the User class

java
1public class User {
2    private String name;
3    private int age;
4
5    // Constructors, getters and setters
6    public User(String name, int age) {
7        this.name = name;
8        this.age = age;
9    }
10    // Getters and setters omitted for brevity
11}

Step 2: Implement the Kafka Serializer Interface

java
1import org.apache.kafka.common.serialization.Serializer;
2import com.fasterxml.jackson.databind.ObjectMapper;
3
4import java.util.Map;
5
6public class UserSerializer implements Serializer<User> {
7    private final ObjectMapper objectMapper = new ObjectMapper();
8
9    @Override
10    public void configure(Map<String, ?> configs, boolean isKey) {
11        // Configuration code can be placed here
12    }
13
14    @Override
15    public byte[] serialize(String topic, User data) {
16        try {
17            return objectMapper.writeValueAsBytes(data);
18        } catch (Exception e) {
19            throw new IllegalStateException("Error serializing value", e);
20        }
21    }
22
23    @Override
24    public void close() {
25        // Cleanup resources if necessary
26    }
27}

Integration with Kafka Producer

To use your custom serializer in a Kafka producer, configure it as follows:

java
1import org.apache.kafka.clients.producer.ProducerConfig;
2import org.apache.kafka.clients.producer.KafkaProducer;
3
4import java.util.Properties;
5
6public class App {
7    public static void main(String[] args) {
8        Properties props = new Properties();
9        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
10        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
11        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "com.example.UserSerializer");
12
13        KafkaProducer<String, User> producer = new KafkaProducer<>(props);
14
15        User user = new User("John Doe", 30);
16        producer.send(new ProducerRecord<String, User>("users", user));
17        producer.close();
18    }
19}

Summary Table

ElementDescription
User classRepresents the data model to be serialized.
UserSerializer classImplements the Serializer interface for the User class.
Configuration in Kafka ProducerThe custom serializer is set using ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG.

Additional Considerations

  • Performance: Custom serializers can be optimized based on the specific requirements and characteristics of the data.
  • Error Handling: Implement robust error handling within your serializer to manage serialization failures.
  • Compatibility: Ensure that any changes in the serializer are backward compatible if the messages are being read by different applications or older versions of the same application.

Creating custom Kafka serializers allows for great flexibility and control over the serialization process, facilitating optimizations and integrations that are not possible with standard serializers.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
Track 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.

Practice system design

All Rights Reserved.