Kafka
Message Versioning
Data Management
Technology
Apache Kafka

Kafka message versioning: version fields, versioned topics and Schema Registry

Codemia Knowledge Hub ·

There are three common ways to version the message format on a Kafka topic: put a version number in each message, publish each version to its own topic, or manage schemas centrally with a schema registry. All three exist for the same reason: producers and consumers in a distributed system are updated at different times, so schema changes have to stay compatible.

Why message formats need versioning

A Kafka topic is a category or feed name that records are published to, and topics are multi-subscriber. As systems evolve, the structure of the messages sent to a topic changes: fields are added, old fields are removed, or a field's data type changes. Those changes must be managed so that every producer and consumer can still work with the data.

The strategies:

  1. Single topic with version number: include a version number in the message so consumers can adapt their processing.
  2. Multiple topics: use different topics for different message versions.
  3. Schema registry: manage and enforce schemas across all messages from a central registry.

A version number in the payload

Each message carries metadata about its version, and consumers read it to decide how to process the message.

Example

A JSON message:

json
1{
2  "version": 2,
3  "data": {
4    "newField": "example",
5    "oldField": "data"
6  }
7}

Consumers branch on the version field:

java
1if (message.version == 1) {
2    processVersion1(message.data);
3} else if (message.version == 2) {
4    processVersion2(message.data);
5}

The consumer has to handle every version it may receive, which takes care.

A separate topic per version

Create one topic per schema version. For instance, user_updates_v1 carries version 1 messages and user_updates_v2 carries version 2.

Trade-offs

AspectAdvantagesDisadvantages
Consumer SimplicityEasier for consumers as they process a consistent message format per topic. Decouples version handling from business logic.Requires more management overhead to handle multiple topics. May lead to duplication of consuming logic across services.
Producer ManagementClear segregation of message versions at the source level.Increases complexity in managing topic lifecycle and partitioning.

A schema registry

A schema registry is a central repository for storing and retrieving message schemas and enforcing a standard schema across all messages. The most common implementation is the Confluent Schema Registry.

Workflow with a schema registry

  1. Define the schema: create an Avro schema for your messages.
  2. Register it: upload the schema to the registry.
  3. Produce: serialize data with the schema when sending messages.
  4. Consume: consumers use the same schema to deserialize and process messages.
java
1String schemaString = "{\"namespace\": \"example.avro\", \"type\": \"record\"," +
2   "\"name\": \"user\",\"fields\": [{\"name\": \"name\", \"type\": \"string\"}]}";
3Schema.Parser parser = new Schema.Parser();
4Schema schema = parser.parse(schemaString);
5
6DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
7GenericRecord datum = new GenericData.Record(schema);
8datum.put("name", "test");
9
10DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(writer);
11dataFileWriter.create(schema, file);
12dataFileWriter.append(datum);
13dataFileWriter.close();

Choosing a strategy

Embedding a version in each message gives immediate flexibility. Separate topics give better isolation and simpler consumer logic. A schema registry is the most robust, centralized way to manage schema evolution. Each fits different scenarios.


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