Course map

Topic 189 / Advanced

Zero-downtime schema changes: expand-and-contract

Chapter 11.5 · CI/CD and Safe Change

What and why

Deployment strategies (→ 187) assume old and new application code can coexist briefly during a rollout — but a schema change that renames or removes a column breaks that assumption instantly, because the old code and the new code can't both be correct against the same shape of data at the same time. Expand-and-contract sequences a schema change into safe, independently deployable steps so the database is never in a state only one version of the application can handle. Builds on: → 187 (rolling, blue-green and canary deployments).

Diagram

Expand         Migrate              Switch                Contract
add new   ──►  backfill +      ──►  deploy code      ──►  remove the
shape          dual-write/read      reading only            old shape
(additive,     (old & new code      the new shape
no one          both stay correct)
breaks)

How it works

  1. Expand: add the new schema shape — a new column, a new table — without touching or removing the old one. This step is purely additive, so every currently running version of the application, old or new, still works unchanged.
  2. Migrate: backfill existing data into the new shape, and have the application dual-write (write both old and new shape on every write) and often dual-read (read the new shape, falling back to the old if absent) during the transition — this is what makes rolling deployment (→ 187) safe for a schema change, because any mix of old and new code running simultaneously during the rollout still sees consistent, correct data.
  3. Switch: once the backfill is complete and dual-writing has run long enough to trust that the new shape is fully populated and correct, deploy the version of the application that reads and writes only the new shape — the old shape is now unused, not yet removed.
  4. Contract: after confirming nothing still depends on the old shape — no running code, no other service, no report job — remove it: drop the old column or table.
  5. Each step is independently deployable and independently reversible by deploying the previous step's code — the application-level rollback (→ 187) stays available throughout, because the database was never forced into a state only one version understands.
  6. The same four-step sequence governs breaking API contract changes (→ 35): add the new field or shape, support both, switch consumers over, remove the old — expand-and-contract is one mechanism applied at two different layers.
  7. Skipping steps — renaming a column directly, dropping the old column the moment the new one exists — collapses the safe window to zero: any instance still running old code, which rolling deployment guarantees will briefly exist, breaks the instant the schema changes underneath it.

Practical example

(numbers illustrative). Renaming a name column to full_name directly would break every currently running instance of the old application code the instant the migration runs, mid-rolling-deploy. Using expand-and-contract instead: add full_name (expand), backfill it from name and write both on every update (migrate — old code writing only name and new code writing both now coexist safely), deploy the version reading only full_name (switch), then drop name once nothing reads it anymore (contract). The rolling deployment that would have caused an outage with a direct rename now completes with zero failed requests, because at no point did any running code see a shape it didn't understand.

When to use it

  • Use expand-and-contract for any schema change that isn't purely additive — renames, type changes, removing a column or table, splitting a table — anything an old and a new version of the application couldn't both handle correctly at once.
  • Skip it for genuinely additive changes (a new nullable column nothing reads yet) — there's no incompatible window to manage.
  • Avoid collapsing the steps under deadline pressure — the migrate step specifically is the single most common one skipped, and skipping it is the most common cause of a "the deploy was fine, the database wasn't" outage.
  • Failure scenarios: dual-writing implemented inconsistently — one code path writes both shapes, another forgets to — so the backfill looks complete until a gap surfaces as missing data for rows written through the forgotten path; contracting before confirming every consumer (including batch jobs, reports, and other services) has switched over, breaking something that wasn't part of the original rollout at all; a migrate step left running indefinitely because no one tracked when it was safe to contract, leaving double-write overhead and complexity as a permanent cost instead of a temporary one.
  • Simpler alternative: for a table small enough and a system with a maintenance window that's actually acceptable to the business, a direct, brief-downtime migration is simpler than expand-and-contract's four deploys — reserve the full sequence for tables and systems where downtime genuinely isn't acceptable.

Practice

(mechanism kind — trace by hand, 20–30 min, shared lab: Postgres). Starting from a users table with a name column, execute expand (add full_name, backfill from name), migrate (modify a minimal write path to write both columns, and verify with a read-after-write check that both stay in sync for new writes), switch (point reads at full_name only), and contract (drop name) — at each step, state which currently running code version(s) would still work correctly if deployment were rolling right now. Done when you can justify, at every step, that no running code version would see a schema it couldn't handle.

Shared lab setup

Key takeaway

Expand-and-contract sequences a breaking schema change into four steps, each individually safe for old and new application code to run against simultaneously — exactly the overlap a rolling, blue-green or canary deployment (→ 187) creates. Done when you can: sequence a specific breaking schema change into expand/migrate/switch/contract and justify why skipping any step reintroduces the outage. Used next in Part 12 (disaster recovery and resilience testing assume migrations this safe are already the norm, not a special case to test around).

References

None — this topic makes no external claims.

Source directory

Search the guide