liveBook · Manning

Serialization

As explained in Kafka Streams in Action, Second Edition

Serialization is the process of converting structured data into a representation suitable for storage or transmission.

Serialization converts structured event data into binary byte arrays for storage or transmission. Kafka producers serialize records before sending them, while consumers deserialize fetched records; the broker stores and forwards bytes without interpreting their data type or language. section 3.0

A schema makes the serialized object’s structure explicit by naming the object, fields, and field types. It acts as a contract between producers and consumers, while Schema Registry centralizes schemas, supports compatible evolution, and supplies serializers and deserializers. section 3.2

Serialization separates application objects from Kafka’s byte-oriented transport:

  • Producers provide serialized bytes; consumers provide deserialization logic. section 3.2
  • Applications can use schemas to enforce expected message structure and manage changes. section 3.2
  • Custom implementations use Kafka’s Serializer and Deserializer interfaces when Schema Registry is unavailable or unnecessary. section 3.8

Custom object serialization

A Java serializer can return null for null data, convert an object to bytes with Jackson’s objectMapper.writeValueAsBytes(), and wrap failures in SerializationException. The matching deserializer reconstructs the object with objectMapper.readValue(). section 3.8

Listing 3.43 Serialize method of a custom serializer
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);    #1
    } catch (JsonProcessingException e) {
        throw new SerializationException(e);
    }
}

Inspect the custom serialize method that converts an object into a byte array and handles serialization errors. section 3.8

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains broker boundaries, schema contracts, and custom Java interfaces, but defers detailed schema-registry workflows.
Examples
Uses a JSON Person schema and Java Jackson-based custom serializer and deserializer implementations.
What it assumes
Assumes basic Kafka broker and topic concepts while defining schemas and serialization responsibilities explicitly.
What you can do after
Builds custom Kafka serializers and deserializers or applies Schema Registry-provided implementations.
How it connects
Connects Kafka brokers, schemas, Schema Registry, deserialization, Kafka Streams, Connect, and ksqlDB.

Also explained in

Compare how 24 books explain serialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.