Schema evolution for event contracts
An API change affects the clients calling today. An event schema change also affects consumers that read old events next month during a replay, and consumers your team has never heard of. Event contracts therefore need stricter evolution rules than request-response APIs — and tooling that enforces them before anything is published.
Contents
Key facts
- Backward compatibility means consumers using the new schema can read data written with the old one; forward compatibility means consumers using the old schema can read data written with the new one.
- Schema registries store versions of each schema and can reject incompatible changes at registration time.
- In Avro, adding a field is backward compatible only if the new field has a default value.
- In Protobuf, fields are identified by number; numbers of removed fields should be reserved and never reused.
Why event contracts are harder
Events are stored. A topic or log may keep months of history, and new consumers often start by reading from the beginning. A consumer deployed today must therefore understand events written by every producer version in the retention window, not only the current one.
Events are also broadcast. A producer usually does not know every consumer: analytics pipelines, audit systems and other teams' services subscribe without asking. A change that seems safe to the producing team can break a consumer it has never met.
Finally, producers and consumers deploy independently and in any order. For some time, old producers and new consumers coexist, and so do new producers and old consumers. The schema has to work in both directions during that window.
Compatibility modes
| Mode | Guarantee | Typical safe changes | Deployment order |
|---|---|---|---|
| Backward | New readers read old data | Add optional field with default; remove field | Upgrade consumers first |
| Forward | Old readers read new data | Add field; remove optional field with default | Upgrade producers first |
| Full | Both directions | Add or remove optional fields with defaults | Any order |
| Transitive variants | Guarantee holds against all previous versions | As above, checked against every version | Needed for long retention and replays |
For event streams with long retention and independent consumers, full transitive compatibility is the safest default. It is restrictive — fields can only be added or removed if they are optional with defaults — but it means any consumer version can read any event in the log.
Format-specific rules
Avro resolves data using both the writer's and the reader's schema. Fields are matched by name; a field present in the reader but missing in the data takes the reader's default. Adding a field without a default breaks backward compatibility, because old data has no value for it. Renames are possible through aliases but are best avoided.
Protobuf matches fields by number, not name. Unknown fields are preserved and ignored by older readers, which makes adding fields safe. Changing a field's type is risky, and reusing the number of a deleted field for something else silently corrupts meaning — mark removed numbers and names as reserved.
JSON Schema offers the least built-in help. Whether a change is compatible depends on how consumers treat unknown properties and missing ones, so conventions must be explicit: consumers ignore unknown properties, producers never remove required ones, and additionalProperties is handled consistently.
Enforcing rules with a registry
A schema registry holds every version of every event schema and checks new versions against a configured compatibility mode. Producers register the schema before publishing, and messages carry a schema identifier, so consumers can fetch the exact writer schema for each record.
- Run the compatibility check in CI, against the registry, before a pull request can merge — not only at runtime.
- Disable automatic schema registration from production producers, so schemas reach the registry only through the reviewed path.
- Name an owner for each event type and record its consumers where they are known.
- Document the meaning of fields, not just their types; most breaking changes are semantic.
When the change really is breaking
Some changes cannot be made compatible: a field's meaning changes, an identifier format changes, an event is split into two. Forcing them into the same schema creates confusion. Publish a new event type or a new topic version instead, produce both for a transition period, migrate consumers, and retire the old one with a communicated date.
For consumers that must process historical events in a new shape, upcasting helps: a small adapter converts old event versions into the current model when they are read, so business logic only deals with one version. Keep upcasters tested with real samples of every historical version in the retention window.
Writing tolerant consumers
Compatibility rules protect consumers only if consumers cooperate. A tolerant consumer ignores fields it does not know, treats missing optional fields as their documented defaults, and does not fail on enum values it has not seen — it routes them to a default branch and records a metric instead.
Consumers should also validate what they rely on, not everything they receive. Checking that the fields used by business logic are present and plausible catches real problems, while rejecting an event because of an unexpected extra field creates them. Events that cannot be processed go to a dead-letter destination with enough context to replay them after a fix.
Worked example: adding currency to an order event
An OrderPlaced event in Avro has a total field holding an amount in euros as a decimal. The business expands to other currencies.
- A new optional field
currencywith default"EUR"is added. The registry, set to full transitive compatibility, accepts it: old consumers ignore it, new consumers reading old events get the default. - A new field
total_minor(integer minor units) is added with a default of null, populated alongsidetotalby the updated producer. - Consumers migrate to
currencyandtotal_minorat their own pace; the event owner tracks which consumers still readtotal. - Once no known consumer reads
totaland the retention window has passed for events withouttotal_minor,totalis marked deprecated in the schema documentation; removal is a separate, announced change.
At no point did a producer or consumer deployment need to happen in a particular order, and replays of last year's events still work.
Common pitfalls
- Allowing producers to register schemas automatically in production.
- Using non-transitive compatibility on topics with long retention.
- Adding Avro fields without defaults.
- Reusing Protobuf field numbers or names after removal.
- Changing a field's meaning while keeping its name and type.
- Breaking changes squeezed into an existing event type instead of a new one.
FAQ
Avro, Protobuf or JSON Schema?
All work with registries. Avro and Protobuf have stronger, well-defined evolution rules; JSON Schema needs more explicit conventions. Choose what your producers and consumers can support consistently.
Do we need a registry if we have few consumers?
A registry is the simplest way to make compatibility checks automatic. With very few consumers, contract tests in CI can play a similar role.
How long do we keep upcasters?
As long as events in that old version can still be read — at least the retention period of the topic or the event store.
Checklist
- Compatibility mode chosen per topic; full transitive for long retention.
- Compatibility check against the registry in CI.
- Auto-registration disabled for production producers.
- Owner and known consumers recorded per event type.
- Semantic changes handled as new fields or new event types.
- Upcasters tested against samples of every version in retention.