Skip to content

Zero-downtime schema changes

Releasing application code without downtime is now routine. Changing the database underneath it often isn’t. A single ALTER TABLE can lock a busy table for minutes, and a renamed column breaks every application server still running the old code during a rolling deployment.

The solution is to treat a schema change not as one step but as a sequence of backward-compatible steps, each safe to run while old and new code are both live. This is known as expand and contract (or parallel change).

Expand and contract: renaming a column safely1. Only once every row has been backfilled and verified does any code read from the new column. 2. The old column is dropped last, after no running or rollback-target version uses it. Rolling back gets harder after step 5 and needs a restore after step 6; see the rollback window below. 1. Expandadd the new column 2. Write bothapp writes old and new 3. Backfillold rows, in batches6. Contractdrop the old column 5. Stop old writesapp uses new only 4. Switch readsreads the new column12
Expand and contract: renaming a column safely At every step, the versions of the application that may be running together keep working, so each step can be deployed on its own.
  1. Only once every row has been backfilled and verified does any code read from the new column.
  2. The old column is dropped last, after no running or rollback-target version uses it. Rolling back gets harder after step 5 and needs a restore after step 6; see the rollback window below.
Change Usually safe online? Safer approach
Add a nullable column, or one with a constant default Yes, in modern PostgreSQL and MySQL versions Just do it
Add an index Not by default on a busy table Build it concurrently or online (for example, CREATE INDEX CONCURRENTLY in PostgreSQL)
Rename a column or table No: breaks running code Expand and contract
Change a column’s type Often rewrites the table Add a new column, backfill, switch
Add a NOT NULL constraint or foreign key Can lock while validating Add it as not yet validated, then validate separately
Drop a column Only when no code uses it The final “contract” step
Large data backfill Not in one statement Small batches, throttled, resumable

Exact locking behavior varies by database engine and version. Check your engine’s documentation before relying on any of these.

  • Migrations are code. Every change is a versioned migration file (with tools such as Flyway, Liquibase or the framework’s migrator), reviewed, tested and applied by the pipeline, never by hand in production.
  • Decouple schema deploys from code deploys. Expand before the code that needs it ships; contract after the code that used the old shape is gone.
  • Every step is backward-compatible with the versions of the application that may be running at the same time, so rolling deployments work, and you can roll back one step at a time.
  • Know your rollback window. While the application still writes the old column (steps 2 to 4), it stays complete and current, so you can return to an earlier application version. Once old writes stop (step 5), the old column goes stale: rolling back to a version that reads it requires copying recent changes back first. Once you drop it (step 6), versions that read it can no longer run at all. Drop it only after the last release that needed it is outside your rollback policy, and take a backup first.
  • Keep old and new values consistent while both exist. During dual writes, every write path must update both columns: application code, batch jobs, admin tools and manual fixes. Mixed-version deployments make this harder, because old application servers write only the old column. Options include a database trigger that copies changes between columns while both are in use, or making the new version write both and backfilling again after the last old server is gone. Verify with a reconciliation query that compares the two columns before you switch reads.
  • Online schema change tools copy a large table in the background and swap it in with a brief lock, for example gh-ost and pt-online-schema-change for MySQL.
  • Set lock timeouts on migrations so a blocked change fails fast instead of queueing every other query behind it.
  • Backfill in batches, throttled by replication lag and load, and resumable if interrupted.
  • Rehearse on production-sized data. A migration that takes a second on a test database can take an hour on a billion rows.
  • No maintenance windows for most schema changes.
  • Easy rollback through most of the change, with a clearly defined point after which rolling back needs extra work or a restore.
  • Faster delivery, because database changes no longer gate releases.
  • More steps and more releases for what used to be one change.
  • The final drop is irreversible without restoring from backup, so its timing needs care.
  • Temporary duplication: two columns and dual writes until the change completes.
  • Discipline needed to finish the contract step instead of leaving old columns forever.

For any database behind a system that must stay available, and any team practicing continuous delivery or rolling deployments.

A small internal system with an agreed maintenance window may not need the extra steps. Even then, versioned migrations in source control are worth it.

  • A “simple” rename deployed with the code, breaking the old servers still running.
  • An index build that locks a hot table in the middle of the day.
  • One giant UPDATE for a backfill, flooding replication and the transaction log.
  • Expand without contract: schemas full of columns nobody can remember the purpose of.
  • Manual hotfixes in production that never make it into the migration history.

Glossary: strangler fig

Written and edited by Abhinav Taduka, enterprise software architect.