Schema Evolution
Expand/contract · Schema registry
Written by Darjus Kazakevic · reviewed
Change the shape of your data while yesterday's code and last year's data are still around.
A schema is the shape of your data: the columns of a table, the fields of an event, the body of an API response. It will change. The problem is that the change never lands everywhere at once. During a rolling deploy, old and new servers run side by side. Mobile apps stay on old versions for months. Events written last year still sit in a topic, and backups older still.
Schema evolution is the discipline of changing a schema so every reader and writer that still exists keeps working. For databases the tool is the expand/contract migration, split over several deploys. For messages it is a small set of compatibility rules, checked by a schema registry before a producer can publish.
In essence — Old data and old readers never disappear at the moment you deploy. Make every change in steps that both versions can live with: expand, migrate, contract.
When to reach for it
Reach for it when
- Deploys roll out gradually, so two versions run at once
- Events or messages are stored and replayed, or read by other teams
- Clients you do not control, such as mobile apps, read your data
Reconsider when
- A one-off script or prototype with a single reader you can redeploy together
- Data you can drop and rebuild, such as a cache or a search index
- A planned maintenance window where everything stops anyway
Key concepts
- Old data and old readers
- Every schema has a past that keeps running. Rows written before a migration, events in a retained topic, messages in a dead-letter queue and clients on last month's release all use an older shape, and none of them update when you deploy.
- Backward and forward compatibility
- Backward compatible: new code can read data written by old code. Forward compatible: old code can read data written by new code. A rolling deploy needs both, because each version reads what the other writes.
- Expand/contract (Parallel change)
- A breaking change split into safe steps. Expand: add the new shape next to the old one. Migrate: write both, backfill old rows, switch reads. Contract: remove the old shape once nothing uses it. Each step ships alone and can be rolled back.
- Field tags and names
- Protobuf and Thrift identify a field by its number, not its name. Avro matches fields by name and resolves the writer's schema against the reader's. JSON has no schema at all unless you add one, so the rules live in the readers.
- Schema registry
- A service that stores every version of each message schema and checks a new version against a compatibility rule before accepting it. Producers tag each message with a schema id, so a consumer can fetch the exact schema it was written with.
- Tolerant reader
- A consumer that reads only the fields it needs, ignores the rest, and does not fail on an extra field or a new enum value. It is what makes forward compatibility work for JSON, where no tool enforces it.
Design rules
- Never make a breaking change in one step
- A rename, a type change or a split is a remove plus an add. Add the new shape first, move readers and writers over, and drop the old shape last, each in its own deploy.
- Add optional fields; never remove required ones
- A new field needs a default, so old data still reads. A removed field must have been optional, so old readers still work. Changing a field's meaning under the same name is a breaking change too.
- Run migrations that do not lock the table
- Add columns without a volatile default, build indexes concurrently, add constraints as not valid and validate later, and backfill in small batches. One long lock on a busy table is an outage.
- Check compatibility in CI, not in production
- Register schemas in CI and fail the build when the registry rejects one. Set the rule per topic, usually backward or full, and make a deliberate breaking change a new topic instead.
Common pitfalls
- Dropping the old column in the same release that stops writing it, so servers still on the old version fail.
- Reusing a deleted Protobuf tag, so old messages decode an old value into a new field.
- Adding a new enum value that old consumers reject, which is a breaking change no field rule catches.
- Backfilling millions of rows in one update, which locks rows, bloats the table and lags every replica.