# Schema Evolution

**Also known as:** Expand/contract · Schema registry  
**Category:** Data & Storage (Where the data lives)  
**Complexity:** 3/5  
**Guide:** https://layra4.dev/architectures/data-storage/schema-evolution

*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 one sentence.** 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.

## Origin (2006)

Ambler and Sadalage's Refactoring Databases described changing a live schema through a transition period in which old and new shapes coexist. Google had open-sourced Protocol Buffers by 2008 and Avro followed in 2009, both built so that data written years apart could still be read. The parallel change name came later, from continuous delivery practice.

## 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.

- A rolling deploy runs two versions for minutes
- A mobile release lives for months
- A stored event lives as long as you keep it

### 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.

- Backward: the new reader fills a default for a field the old writer never sent
- Forward: the old reader ignores a field it does not know
- Full: both at once, the safe default for shared topics

### 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.

> add display_name → write both → backfill → read new → drop name

### 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.

- Protobuf: never change or reuse a tag; mark removed ones reserved
- Avro: a new or removed field needs a default to stay compatible
- JSON: readers ignore unknown fields and tolerate missing optional ones

### 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.

> Confluent Schema Registry, AWS Glue Schema Registry, Apicurio

### 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.

## Examples

### Renaming a column with expand/contract in Postgres

```sql
-- Goal: rename users.name (not null) to users.display_name, with zero downtime.

-- 1. Expand: a nullable column with no default is a quick catalog change, not a rewrite.
alter table users add column if not exists display_name text;

-- 2. Deploy: write both columns; read display_name, falling back to name.

-- 3. Backfill in small batches, so no long lock and no giant transaction.
update users set display_name = name
where id in (select id from users where display_name is null limit 5000);
-- repeat until it updates 0 rows

-- 4. Enforce it without a long scan under lock: add unchecked, then validate.
alter table users add constraint display_name_present
  check (display_name is not null) not valid;
alter table users validate constraint display_name_present;

-- 5. Deploy: read and write only display_name.

-- 6. Contract: once no running version reads name, drop it.
alter table users drop column if exists name;
```

Six steps, at least three deploys. Old and new code both work after every step, so any deploy can roll back. The steps that are only code changes are comments here; they matter as much as the SQL.

## When to use

- 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

## When not to use

- 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

## Benefits

- Schema changes stop requiring downtime or coordinated releases
- Every step can roll back, because the old shape still exists
- Consumers of shared topics break at build time, not in production

## Costs

- One logical change becomes several deploys over days
- Dual writes and fallback reads add temporary code to remove later
- A registry is one more service to run, and to keep available

## 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.

## Related patterns

- [Change Data Capture](https://layra4.dev/architectures/data-storage/change-data-capture) — where a table's schema change reaches every downstream consumer
- [API Styles](https://layra4.dev/architectures/core-building-blocks/api-styles) — the same compatibility rules, applied to request and response bodies
- [Event Sourcing](https://layra4.dev/architectures/architectural/event-sourcing) — events kept forever, read by code that keeps changing
- [Published Language](https://layra4.dev/architectures/strategic/published-language) — a shared, versioned schema between teams

## Further reading

- *Designing Data-Intensive Applications, ch. 4* — Martin Kleppmann (2017) — Encoding and evolution: Protobuf, Thrift and Avro compared.
- *Refactoring Databases* — Scott Ambler, Pramod Sadalage (2006) — Safe schema changes with a transition period where old and new shapes coexist.
