Schema evolution in event streams: the compatibility rules that save you a rewrite

A team adds a currency field to their order event. It is required, because every order obviously has a currency and making it optional felt sloppy.

The producer deploys. Three downstream consumers immediately start failing, and one of them is a payments reconciliation job that now cannot read any event, including the two million already in the topic that predate the field.

The change was correct as a data model. It was breaking as a contract, and the difference is that a topic is not a table.

A log cannot be migrated

The reason this trips people up is that database migrations create a false intuition. ALTER TABLE ADD COLUMN rewrites or reinterprets every row, and afterwards exactly one schema exists. There is no old version to worry about.

An event log is append only and immutable. Every event ever written stays in the shape it was written in. Rewriting history means republishing, which changes offsets, invalidates every consumer’s committed position, and re-triggers side effects for anything that reprocesses.

So the log holds every schema version you have ever produced, simultaneously, and readers have to cope.

flowchart LR
    subgraph db["Database table"]
        T1["rows in schema v1"] -->|"ALTER TABLE"| T2["all rows now v2"]
    end

    subgraph log["Event log"]
        L1["offsets 0-2M: v1"]
        L2["offsets 2M-3M: v2"]
        L3["offsets 3M+: v3"]
        L1 --- L2 --- L3
        R["Every reader must handle<br/>v1, v2 and v3"]
    end

    style T2 stroke:#4ade80,stroke-width:2px,color:#fff
    style R stroke:#f59e0b,stroke-width:3px,color:#fff

Which direction you need decides who deploys first

The two compatibility modes are named from the reader’s perspective and the naming causes endless confusion, so it is worth stating them as deployment orders instead.

Backward compatible: new schema can read old data. Upgrade consumers first, then producers. This is the default in Confluent Schema Registry and it is the right choice when you control consumers and want producers to move once everyone is ready.

Forward compatible: old schema can read new data. Upgrade producers first, and lagging consumers keep working. This is what you need when consumers are owned by teams you cannot coordinate with, which is most of the time in a large estate.

Full: both. Deploy in any order. More constraining on what changes you can make and the only mode that genuinely removes the coordination problem.

ChangeBackwardForward
Add optional field with defaultsafesafe
Add required field, no defaultbreakssafe
Remove field with defaultsafesafe
Remove required fieldsafebreaks
Rename fieldbreaksbreaks
Widen int to longsafebreaks
Change field typebreaksbreaks

The row that explains the incident above: adding a required field is backward incompatible, because a new reader hitting an old event finds no value and no default to fall back on.

The rename row is the one people are most surprised by. A rename is a delete plus an add, so it breaks in both directions. Doing it safely means adding the new name, writing both for a transition period, migrating consumers, then removing the old one, which is the same expand-migrate-contract shape as a schema change on a large table.

Defaults on everything

The practical rule that avoids most of this is to give every field a default, always, including ones that feel mandatory.

{
  "type": "record",
  "name": "OrderPlaced",
  "fields": [
    {"name": "orderId",  "type": "string"},
    {"name": "amount",   "type": "long"},
    {"name": "currency", "type": ["null", "string"], "default": null},
    {"name": "channel",  "type": "string", "default": "web"}
  ]
}

currency as a nullable union with a null default is readable from every old event. The consumer has to handle null, which is the honest representation of the fact that old events genuinely do not have a currency, and pretending otherwise is what caused the failure.

Choosing a sensible default like "web" for channel is better than null where one exists, because it lets consumers treat old and new events uniformly.

Enforce it before it reaches the log

The registry is what turns this from a convention into a guarantee.

props.put("schema.registry.url", "http://schema-registry:8081");
props.put("auto.register.schemas", false);   // deliberate registration only
props.put("value.serializer", KafkaAvroSerializer.class.getName());

Setting auto.register.schemas to false is the part worth arguing for. With auto-registration on, a producer with an incompatible schema registers it at runtime and the incompatible event is in the log before anyone notices. Turning it off means schemas are registered explicitly, ideally in CI, and an incompatible change fails the build rather than the pipeline.

# In CI, before merge. Fails if the change violates the configured mode.
curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data @order-placed-v3.json \
  http://schema-registry:8081/compatibility/subjects/orders-value/versions/latest

That check is the highest value thing in this whole area, because it moves the failure from a Tuesday afternoon incident to a red pull request.

Protobuf and JSON differ in useful ways

Avro is the common choice with Kafka and it carries a schema id with each message, so the reader fetches the writer’s schema and resolves against its own. That resolution step is what makes defaults work.

Protobuf handles unknown fields by preserving them, so a consumer reading a newer message keeps fields it does not understand and can write them back out unchanged. That is genuinely useful in a pipeline where a service reads, modifies and republishes, because it stops intermediate services stripping data they were not updating. Field numbers rather than names mean renames are free and reusing a retired number is catastrophic, which is why reserved exists.

JSON Schema is the most flexible and the least enforced. Unknown fields are ignored by most parsers, which gives you accidental forward compatibility and no protection against a type change that silently coerces.

What I would put in place

Set the compatibility mode deliberately rather than inheriting the default, and pick FULL if producers and consumers are owned by different teams.

Give every field a default, including the ones that feel required, because required is a statement about new events and the log contains old ones.

Turn off auto-registration and check compatibility in CI.

And keep a consumer running against the oldest schema you still have in the log, as a test if not in production. The compatibility rules describe what should work, and the only thing that tells you whether an event from 2024 still deserialises is deserialising one.

Frequently Asked Questions

What is the difference between backward and forward compatibility in schema evolution?
Backward compatible means code written against the new schema can read data written with the old one, so consumers must be upgraded before producers. Forward compatible means code written against the old schema can read data written with the new one, so producers can be upgraded first and lagging consumers keep working. Full compatibility satisfies both and is the safest choice when producers and consumers are owned by different teams and cannot be deployed in a coordinated order.
Why can't you just migrate an event stream schema like a database table?
Because the events are immutable and there can be years of them. A database migration rewrites every row so only one schema exists afterwards, whereas rewriting a log would mean republishing history, which changes offsets, breaks every consumer's committed position, and re-triggers side effects for anything that reprocesses. The log therefore accumulates schema versions and every reader must be able to handle all of them.
Is adding a field to an event schema a breaking change?
Adding an optional field with a default is safe: old readers ignore it and new readers substitute the default when reading old events. Adding a required field with no default is breaking, because a reader using the new schema encounters old events that lack the field and has no value to use. Removing a field is the mirror image, safe if it had a default and breaking otherwise, which is why defaults on every field is the practical rule.

Sources

[ RELATED_LOGS ]

TTFB: -- ms LOAD: -- s PAYLOAD: -- kb