Video in progress

Staff 🌱 Spring

How do you change the format of events in Kafka without breaking dozens of consumers (schema evolution)?

Events get a schema (Avro/Protobuf/JSON Schema) and a compatibility mode in the schema registry (usually BACKWARD or FULL). Changes that don't break compatibility are allowed, and those that do become a new event type or a new topic.

📖 Answer

In a REST API you can put the version in the URL and migrate the old clients off. With events it is harder: a message sits in the topic for days, the sender often doesn't even know who the consumers are, and during a replay old messages are read by new code.

  1. A schema is mandatory. "Just JSON" means there is no contract. Use Avro or Protobuf + a schema registry (Confluent, Apicurio). The producer registers the schema before sending; the registry rejects a schema that breaks the compatibility rule. That way a breaking change is caught in CI, not in production.

  2. Compatibility mode:

  • BACKWARD: a new consumer can read old messages (important for replay).
  • FORWARD: an old consumer can read new messages (important when the producer is deployed first).
  • FULL: both. In a multi-team environment this is what I choose.
  1. Safe and unsafe changes:
Safe Breaking
A new optional field with a default value Adding a required field
Removing an optional field Changing a field's type
Renaming a field (in Protobuf, reusing a field number)
Removing an enum value
  1. If you need a breaking change: a new event type (OrderPlacedV2) or a new topic. The producer sends both during the transition, and once consumers have moved, the old one is stopped. You can watch who is still reading the old topic through consumer groups.

  2. Tolerance on the consumer side: ignore unknown fields (FAIL_ON_UNKNOWN_PROPERTIES=false in Jackson, which is Boot's default), and handle unknown enum values with a default.

The organizational part: an event is a public API. It has an owner, its schema lives in a separate repo or in the registry, changes go through review and consumers are notified. Serializing the internal domain model (a JPA entity) directly as an event is the fastest way to break order.