Video in progress
How do you change the schema of events that many teams use without breaking each other?
Events are a public API, so a schema registry and a compatibility rule (usually backward or full) are mandatory for them; CI rejects a change that breaks compatibility. If a breaking change is needed, a new versioned topic and a period of running both in parallel.
📖 Answer
Sending JSON "freely" is convenient at the start. A year later the amount field changes from string to number, and the consumers of three teams crash in the middle of the night. The producer team didn't even know they existed.
The rules I introduce:
-
The schema is formal. Avro, Protobuf or JSON Schema, stored in the registry and bound to each topic. The producer adds the schema ID to the message when it serializes.
-
Compatibility mode.
- Backward: the new schema can read old messages, so consumers upgrade first;
- Forward: the old schema can read new messages, so the producer upgrades first;
- Full: both. In a multi-team environment I pick this one, because you can't control who deploys first.
In practice this means: you can only add an optional field with a default value; removing a field or changing its type is not allowed. In Protobuf, don't reuse field numbers (reserved).
-
A check in CI. The schema in a PR is checked for compatibility against the latest version in the registry. If it breaks, the build goes red. This rule should be handed to automation, not to people's memory.
-
A path for breaking changes. Open an
orders.v2topic, the producer writes to both for a while, consumers migrate in their own time, andv1is closed after the last consumer moves. You can see who is still readingv1from the consumer group metrics. -
Ownership and documentation. Each event has an owning team, and the meaning of fields and examples are in a catalog. The answer to "can this field be null?" should be in the schema, not in Slack.
One catch: compatibility is only about syntax. If the semantics of a field change (amount is no longer in tiyin, it's now in soum), the registry won't catch it. That kind of change should also be made as a new field or a new version.
Sign up to mark studied questions and save your progress.