Technology

Zero-downtime database migrations with the expand and contract pattern

During a rolling deploy, old and new code share one database. Expand and contract is how you change the schema underneath both without breaking either.

  • By Systronium Team, Engineering & Delivery
  • November 2, 2026
Three versions of a users table showing the expand, migrate and contract steps of renaming a column

Most deploy outages that involve a database are not caused by a bad query. They are caused by timing. For a few minutes during every rolling deploy, the old version of your application and the new one run side by side against the same schema, and a migration that suits only one of them breaks the other.

Rename a column in one step and the old servers, still running, start throwing errors the moment the migration finishes. Drop a column the old code still reads and the same thing happens. Even adding a NOT NULL column without a default breaks every insert from code that does not know the column exists.

The expand and contract pattern, sometimes called parallel change, avoids all of this by splitting one risky change into several boring ones.

The idea in one paragraph

First expand: add the new structure next to the old one, so both versions of the code keep working. Then migrate: move the data and the code over to the new structure while both still exist. Finally contract: once nothing uses the old structure, remove it. Each step is a separate deploy, and each one can be rolled back on its own.

Worked example: renaming a column

Say the users table has a name column and you want it to be called full_name. A single RENAME COLUMN would break every running instance of the old code. Instead:

Step 1. Expand. Add the new column as nullable, with no other changes.

ALTER TABLE users ADD COLUMN full_name VARCHAR(255) NULL;

The old code ignores it. Nothing breaks.

Step 2. Write to both. Deploy code that writes every change to both name and full_name, but still reads from name. From now on, new and updated rows are correct in both places.

Step 3. Backfill. Copy the existing data across in small batches rather than one big statement:

UPDATE users
SET full_name = name
WHERE id > 120000 AND id <= 125000
  AND full_name IS NULL;

Run it in a loop that walks the primary key a few thousand rows at a time, with a short pause between batches. A single UPDATE across millions of rows holds locks for a long time, creates one enormous transaction and can leave replicas minutes behind.

Step 4. Switch reads. Deploy code that reads from full_name. Keep writing to both columns for now, so you can roll back to the previous release if anything looks wrong.

Step 5. Stop writing the old column. Once you are confident, deploy code that only knows about full_name.

Step 6. Contract. Drop the old column.

ALTER TABLE users DROP COLUMN name;

Six steps instead of one sounds slow, but each step is small, none of them needs a maintenance window, and the whole sequence can still be finished in a day or two.

Know which operations lock the table

The pattern keeps old and new code compatible. You also need each individual statement to be cheap, and that depends on the database.

PostgreSQL

  • Adding a nullable column is instant. Since PostgreSQL 11, adding a column with a constant default is instant too.
  • A plain CREATE INDEX blocks writes for as long as it runs. Use CREATE INDEX CONCURRENTLY instead. It takes longer and cannot run inside a transaction block, so many migration tools need a setting to allow it.
  • Adding NOT NULL to an existing column scans the whole table while holding a heavy lock. Add CHECK (full_name IS NOT NULL) NOT VALID first, then run VALIDATE CONSTRAINT as a separate step, which only takes a light lock. From PostgreSQL 12, a later SET NOT NULL can skip its own scan because the validated check already proves there are no nulls.
  • Set a lock_timeout for migration sessions. A migration that waits behind a long-running query also blocks every query that queues up behind it, and that is how a two-second change turns into a ten-minute outage.

MySQL

  • MySQL 8.0 can add a column instantly with ALGORITHM=INSTANT: from 8.0.12 for columns at the end of the table, and in any position from 8.0.29.
  • Many other changes, including adding a secondary index, run as online DDL with ALGORITHM=INPLACE, LOCK=NONE, which keeps reads and writes flowing. Write these clauses out explicitly. If MySQL cannot honour them, the statement fails straight away instead of quietly taking a full lock.
  • Changing a column's type usually means a full table copy. For large tables, tools like gh-ost and pt-online-schema-change build a shadow copy and swap it in at the end.
  • Online DDL still needs a brief metadata lock at the start and the end. A long-running transaction can hold that up, and everything else queues behind it. Keep an eye on SHOW PROCESSLIST during big changes.

Changing a column type

The same pattern handles type changes. Suppose orders.amount is an INT that is getting close to its limit and needs to become a BIGINT:

  1. Add amount_v2 BIGINT NULL.
  2. Write to both columns.
  3. Backfill in batches.
  4. Read from amount_v2.
  5. Stop writing amount.
  6. Drop amount.

If you want the original name back afterwards, that rename is a breaking change of its own and needs another round of the same steps. Plenty of teams simply live with the new name.

Make every step reversible

The real value of the pattern is that there is always a way back. At every point before the contract step, the previous release still works against the current schema. If the new read path misbehaves, you roll back the code and the data is still there.

That only holds if you are strict about two rules:

  • Never ship a schema change and the code that depends on it in the same deploy. The schema goes first, and the code that needs it follows in a later deploy.
  • Never contract until the old path has been unused for a while. Check logs or query statistics to confirm nothing still reads the old column, including cron jobs, reports and the script someone runs once a quarter.

A short checklist

  • Every migration is backward compatible with the code currently in production.
  • New columns are nullable or have a constant default.
  • Indexes are built concurrently or online.
  • Backfills run in small batches, separately from the migration itself.
  • Migration sessions have a lock timeout.
  • Destructive changes wait for a later release, after you have confirmed nothing uses the old structure.

It takes more planning than a single ALTER TABLE, but it turns schema changes from something you schedule for 2 a.m. into something you can ship on an ordinary afternoon.

The same pattern outside the schema

Expand and contract works for more than columns. In the OCPP Management Platform, chargers authenticate with a per-charger password, and chargers that have not been migrated yet keep connecting on a logged grace period until an operator switches enforcement on: the new rule arrives first, and the old path goes only once nothing depends on it. Planning changes this way is part of our cloud deployment and DevOps work.

Let's build something reliable

Practical software. Reliable systems. Built to scale.

Tell us what you are building and where delivery is slowing down. We will map the shortest practical path to a working product.

A remote-first software studio building web platforms, desktop software, and scalable systems that keep working after launch.

  • Web platforms
  • Desktop software
  • Cloud & DevOps
info@systronium.com