Skip to content

Field Guide

Expand and contract: changing a schema both generations use

During a migration the database is the one thing both generations share. Expand and contract is how its shape changes underneath them without a single breaking release.

Slide: expand, add the new shape beside the old; migrate, move every reader and writer; contract, remove the old shape. Gen 1 and Gen 2 share the database the whole time.

A rewrite rarely gets its own clean database on day one. For months, often years, Gen 1 and Gen 2 read and write the same tables. Every change to those tables has to work for both at once, and for whatever else is quietly connected to them.

The answer has two names. Danilo Sato calls it parallel change, "also known as expand and contract": a way "to implement backward-incompatible changes to an interface in a safe manner, by breaking the change into three distinct phases: expand, migrate, and contract." A database schema is an interface, and usually the most widely shared one in the building.

The three phases

  • Expand. Add the new shape next to the old one. Nothing that exists is changed or removed. Sato: "In the expand phase you augment the interface to support both the old and the new versions."
  • Migrate. Move every reader and writer to the new shape, one at a time. Sato warns that "in the case of external clients, this will be the longest phase."
  • Contract. Once nothing uses the old shape, remove it. In Sato's words, "Once all usages have been migrated to the new version, you perform the contract phase to remove the old version."

Pramod Sadalage and Martin Fowler describe the middle of this for databases as a transition phase: "a period of time when the database supports both the old access pattern and the new ones simultaneously. This allows older systems time to migrate over to the new structures at their own pace." In a Gen 1 to Gen 2 migration, Gen 1 is the older system.

Step by step on a live table

Prisma's data guide spells the same idea out for a table in production. Its example is splitting a name column into first_name and last_name.

  1. Add the new columns or table. Do not alter the old ones. Make the new columns nullable or give them defaults, so the code that does not know about them keeps working.
  2. Write to both. The application writes every change to the old and the new structure, and still reads only the old one.
  3. Backfill. Copy and convert the existing rows. This is where meaning gets lost. Prisma's example: a script can split John Smith, but it "may not do the right thing" with Juan Carlos Ferdinand Garcia.
  4. Test the new structure with real queries, while the old one is still the source of truth.
  5. Move reads to the new structure, one client at a time.
  6. Stop writing to the old structure.
  7. Remove it.

Steps 2 to 6 are the dual write pattern applied inside one database. Stripe followed the same sequence to move hundreds of millions of subscription records with the system up the whole time.

The point of no easy return

Up to step 5, going back costs nothing: the old structure still has everything. Prisma marks step 5 as "the first time where you are in danger of losing data if you have to roll back," because from then on the new structure can hold data the old one never saw.

Treat step 5 as a cutover in miniature. Decide in advance what would make you roll back, and move one reader at a time so a rollback is small.

Tricks for the transition

  • A view for the old name. Sadalage and Fowler's rename example renames the table and then creates a view with the old name, so old code keeps working: ALTER TABLE customer RENAME to client; CREATE VIEW customer AS SELECT .... The view is removed in the contract phase.
  • Triggers or the application to keep both in step. Either can copy writes from one shape to the other during the transition. Pick one, write it down, and remove it in the contract phase.
  • Small changes in sequence. Prisma's advice is that larger changes "should be broken up into smaller, isolated steps." Three small expand and contract passes are safer than one big one.

Why it fits a rewrite

Sato points out that "deployment techniques such as canary releases and BlueGreenDeployment are applications of the parallel change pattern." So is the whole Gen 1 to Gen 2 project: a strangler fig migration is expand (Gen 2 beside Gen 1), migrate (slice by slice) and contract (killing Gen 1).

The schema version runs inside the bigger one. When Gen 2 needs a table shaped differently, Gen 1 must keep working against it until the day Gen 1 is gone. Each schema change is a small expand and contract of its own, with Gen 1 as the slowest client.

When not to use it

  • Nothing else uses the table and a short outage is fine. Change it in one step.
  • The change is purely additive. A new nullable column needs no migrate or contract phase.
  • You cannot find every reader and writer. Find them first, or the contract step will break one.
  • Gen 1 is days from retirement. Wait, then change the schema once.

Where it goes wrong

  • The contract phase never happens. The old column stays in case it is needed, the double writes stay, and the next developer cannot tell which one is real. Schedule the contract step when you schedule the expand step.
  • A writer nobody knew about. A report, a batch job or another team's integration still writes only to the old column, so the new one quietly falls behind. Find every client before step 6.
  • Constraints added too early. A NOT NULL or foreign key on the new column before the backfill is done breaks the code that does not know the column exists.
  • A backfill that guesses. If the conversion is ambiguous, as with names, the new data is wrong in a way no test catches. Decide what the rule is, and flag the rows that do not fit it.

Checklist

  • Every client of the table is known, including reports, jobs and other teams.
  • The new structure is added with no change to the old one, and with no constraint the old code would break.
  • Writes go to both shapes, and the backfill can be rerun safely.
  • Reads move one client at a time, with a rollback trigger agreed for each.
  • The step after which rollback loses data is written down.
  • The contract step has a date and an owner.

Sources

  1. Danilo Sato - Parallel Change (martinfowler.com, May 13, 2014) - the definition, the three phases, and its use for database refactoring and deployments
  2. Pramod Sadalage and Martin Fowler - Evolutionary Database Design (May 2016) - the transition phase, and the rename-table example with a view
  3. Using the expand and contract pattern for schema changes - Prisma's Data Guide - the step-by-step version for a live schema, and the point where rolling back starts to lose data
  4. Jacqueline Xu - Online migrations at scale (Stripe, February 2, 2017) - the same sequence used to move hundreds of millions of records while the system stayed up

Last updated November 29, 2026.