0
0
Kafkadevops~10 mins

Custom SerDes in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Custom SerDes
Start
Serialize Object
Send Bytes to Kafka
Receive Bytes from Kafka
Deserialize Bytes to Object
Use Object in Application
End
This flow shows how a custom serializer converts an object to bytes before sending to Kafka, and a custom deserializer converts bytes back to the object after receiving.
Execution Sample
Kafka
public class UserSerializer implements Serializer<User> {
  @Override
  public byte[] serialize(String topic, User data) {
    return (data.getName() + "," + data.getAge()).getBytes();
  }
}

public class UserDeserializer implements Deserializer<User> {
  @Override
  public User deserialize(String topic, byte[] data) {
    String[] parts = new String(data).split(",");
    return new User(parts[0], Integer.parseInt(parts[1]));
  }
}
This code serializes a User object to bytes by joining name and age with a comma, and deserializes bytes back to a User object by splitting the string.
Process Table
StepActionInputOutputNotes
1Serialize User objectUser(name=Alice, age=30)byte[] representing "Alice,30"Convert object fields to string, then to bytes
2Send bytes to Kafkabyte[] "Alice,30"Sent to Kafka topicBytes are transmitted over Kafka
3Receive bytes from Kafkabyte[] "Alice,30"byte[] "Alice,30"Bytes received unchanged
4Deserialize bytesbyte[] "Alice,30"User(name=Alice, age=30)Split string and parse age to int
5Use User objectUser(name=Alice, age=30)User object readyApplication can now use the User object
6End--Process complete
💡 Deserialization completes successfully when bytes are converted back to the original User object.
Status Tracker
VariableStartAfter SerializeAfter SendAfter ReceiveAfter DeserializeFinal
userUser(name=Alice, age=30)User(name=Alice, age=30)User(name=Alice, age=30)User(name=Alice, age=30)User(name=Alice, age=30)User(name=Alice, age=30)
bytesnullbyte[] "Alice,30"byte[] "Alice,30"byte[] "Alice,30"nullnull
deserializedUsernullnullnullnullUser(name=Alice, age=30)User(name=Alice, age=30)
Key Moments - 3 Insights
Why do we convert the User object to a string before bytes?
Because Kafka sends data as bytes, we must convert the object to a byte array. Converting to a string first makes it easy to join fields and then get bytes, as shown in step 1 of the execution_table.
What happens if the deserializer receives corrupted bytes?
If bytes are corrupted, splitting or parsing may fail or produce wrong data. The deserializer expects the format shown in step 4. If format differs, errors or wrong objects occur.
Why do we need both serializer and deserializer?
Serializer converts objects to bytes for Kafka to send. Deserializer converts bytes back to objects for the application to use. Both must agree on the format, as seen in steps 1 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of step 1 (Serialize User object)?
AString "Alice,30"
BUser object with name and age
Cbyte[] representing "Alice,30"
Dnull
💡 Hint
Check the Output column in step 1 of the execution_table.
At which step does the data change from bytes back to a User object?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where deserialize is called in the execution_table.
If the serializer joined fields with a semicolon instead of a comma, what would change in the execution_table?
AStep 5 would produce a different User object
BStep 1 output would have semicolon instead of comma
CStep 4 deserialization would fail without changes
DNo changes needed anywhere
💡 Hint
Check the string format in step 1 output and how step 4 splits the string.
Concept Snapshot
Custom SerDes in Kafka:
- Serializer converts objects to byte arrays before sending.
- Deserializer converts byte arrays back to objects after receiving.
- Both must agree on the data format (e.g., CSV string).
- Implement Serializer<T> and Deserializer<T> interfaces.
- Used to send complex objects through Kafka topics.
Full Transcript
This visual execution shows how custom serialization and deserialization work in Kafka. First, a User object is converted to a byte array by joining its fields as a string and getting bytes. These bytes are sent through Kafka. On the receiving side, the bytes are converted back to a string, split by comma, and parsed to recreate the User object. Variables like user, bytes, and deserializedUser change as the program runs. Key moments include why conversion to string is needed, what happens if bytes are corrupted, and why both serializer and deserializer are required. The quizzes test understanding of each step's input and output and the importance of matching formats. This process allows Kafka to handle complex data types by converting them to bytes and back.