Sub topic 9
Designing and Versioning Event Schemas
This chapter assumes you know what events, producers, consumers and brokers are, and that delivery is at-least-once. It covers how to shape an event so that it can change over time without breaking the consumers that read it.
Why the schema is a contract
A producer does not know who consumes its events. That is the point of decoupling, but it also means every field you publish may be relied on by code you cannot see. The event schema is the contract between teams. Changing it carelessly breaks consumers silently, often days later.
Design the envelope and the payload
Separate the metadata that every event carries (the envelope) from the business data (the payload).
{
"id": "6f1c2d9e-3b7a-4c55-9d0e-1a2b3c4d5e6f",
"type": "order.placed",
"version": 1,
"occurredAt": "2026-11-26T11:00:00Z",
"data": {
"orderId": "o-1",
"customerId": "c-9",
"totalCents": 4599
}
}idis unique per event. Consumers use it for deduplication.typenames a fact in the past tense, such asorder.placed.versionidentifies the schema ofdata.occurredAtis when the fact happened, not when it was published.
Use integer cents instead of floating-point money, and ISO 8601 timestamps in UTC. Both avoid ambiguity across languages.
Compatible and breaking changes
Two directions of compatibility matter:
- Backward compatible: consumers on the new schema can read old events.
- Forward compatible: consumers on the old schema can read new events.
Because a broker may hold old events while new producers run, and old consumers may run while new producers deploy, aim for both.
| Change | Safe? | Why |
|---|---|---|
| Add an optional field | Yes | Old consumers ignore it. New consumers handle its absence. |
| Remove an optional field | Usually | Only if no consumer relies on it. |
| Rename a field | No | Old consumers no longer find it. |
| Change a field's type | No | Parsing fails or values are misread. |
| Add a required field | No | Old events lack it. |
| Change a field's meaning | No | Nothing fails, but results become wrong. |
The last row is the most dangerous. If totalCents starts to include tax, no parser will complain.
The tolerant reader
Consumers should read only the fields they need and ignore the rest. They should also supply defaults for optional fields.
import json
def handle_order_placed(raw: str) -> None:
event = json.loads(raw)
data = event["data"]
order_id = data["orderId"]
# "currency" was added later; old events do not have it.
currency = data.get("currency", "USD")
print(f"order {order_id} total {data['totalCents']} {currency}")
handle_order_placed(
'{"id": "e-1", "type": "order.placed", "version": 1, '
'"data": {"orderId": "o-1", "totalCents": 4599}}'
)This consumer survives the addition of currency and any other new field. A consumer that rejects unknown fields breaks on the first additive change.
Versioning when a break is unavoidable
Sometimes you must make a breaking change. Two common approaches:
- Publish a new event type or version, such as
order.placedversion 2, alongside version 1. Run both until every consumer has migrated, then retire version 1. - Use a schema registry. It stores each schema version and rejects a producer's change if it fails a configured compatibility check.
Dual publishing has a cost. You maintain two versions and must set a retirement date. Without a date, version 1 never leaves.
Practical rules
- Add fields; do not rename or repurpose them.
- Keep payloads documented, with a named owner for each event type.
- Put the version in the event, not only in the topic name.
- Test compatibility in the producer's build, not in production.
Exercise
A producer wants to rename customerId to buyerId in order.placed. Several consumers use customerId. What is a safe way to do it?
Show answer
Add buyerId as a new field while keeping customerId with the same value, so old consumers keep working. Tell consumers to migrate to buyerId. Once all have migrated, stop publishing customerId, ideally under a new schema version with a planned retirement date. Renaming in place would break every consumer that reads customerId.
Exercise
Why is changing totalCents so that it includes tax more dangerous than removing an optional field?
Show answer
The type and name stay the same, so no parser or schema check fails. Consumers keep running and compute wrong results. Changes in meaning need a new field or a new version.