← Lessons

quiz vs the machine

Gold1470

Databases

Schema Migration Expand and Contract

Splitting a breaking schema change into add, backfill, and remove phases lets old and new code run side by side during a deploy.

5 min read · core · beat Gold to climb

Why Not Just Change It

Renaming a column in one step breaks every running instance of the old code the instant it lands, because deploys are not atomic across many servers. The expand and contract pattern, also called parallel change, makes the schema backward and forward compatible through the rollout.

The Three Phases

  • Expand: add the new column or table without removing the old one. The schema now supports both shapes.
  • Migrate code and backfill: deploy code that writes to both old and new, and backfill existing rows so the new column is populated.
  • Contract: once all instances use the new shape and data is migrated, drop the old column.

Each phase is independently deployable, and at no point does the schema reject the code that any live instance is running.

Why It Is Safe

Because expand only adds, it can never break old code. Contract only runs once nothing reads the old column. Between them the system tolerates a mixed fleet, which is exactly what a rolling deploy produces.

Key idea

Expand and contract splits a breaking schema change into additive expand, backfill with dual writes, and a final contract, so old and new code coexist safely throughout a non atomic deploy.

Check yourself

Answer to earn rating on the learn ladder.

1. Why is a one step column rename dangerous in production?

2. What does the expand phase do?

3. When is it safe to run the contract phase?