Deployment automation has come a long way: blue-green releases, canaries, feature flags, instant rollbacks. And then, at the bottom of the stack, many of those same teams still run schema migrations the way we all did in 2010, a maintenance window, a long-running ALTER statement, and a prayer. The database is where zero-downtime discipline goes to die, mostly because the techniques are less known, not because they are harder.
We apply one pattern for virtually every schema change on live systems: expand/contract. It is older than most of the tools we use, and it works on Postgres, MySQL, and everything in between.
Why naive migrations take you down
Two separate hazards hide inside an ordinary migration. The first is locking: many DDL operations take locks that block reads or writes for the duration of the operation, and on a table with a hundred million rows, rewriting or scanning it can mean minutes of a frozen application. Worse, in Postgres, even a fast ALTER can queue behind a long-running query and stall every subsequent statement behind its lock request. A one-millisecond schema change can cause a multi-minute outage purely through lock queueing.
The second hazard is deploy-order coupling. Old application code and new schema, or new code and old schema, inevitably coexist during any rolling deploy. Rename a column in one step and there is a window where running code references a column that no longer exists. No maintenance window fixes this; it is a logical problem, not a speed problem.
Every migration incident we have ever been called into reduces to one of two mistakes: a lock nobody predicted, or two versions of the code disagreeing about the shape of the data.
The expand/contract pattern
Expand/contract resolves the coupling by making every change backward-compatible at each step. Three phases.
Expand: add the new structure alongside the old, a new column, a new table, a new index, without touching anything existing code depends on. Additive changes are cheap and safe almost everywhere.
Migrate: teach the application to write to both old and new structures, backfill historical data, then move reads over to the new structure. Every step here ships as an ordinary deploy, observable and reversible.
Contract: once nothing reads the old structure, verified, not assumed, remove it. The contract phase is deliberately boring and often happens weeks later.
A column rename, the classic example, becomes: add the new column; deploy code that writes both and reads the old; backfill in batches; deploy code that reads the new; watch for a while; drop the old column. Six small steps, each individually harmless, each independently rollbackable, instead of one atomic rename that is neither.
Dual writes and backfills, done carefully
The migrate phase has sharp edges worth naming. Dual writes should happen in the same transaction whenever both structures live in the same database, that is the whole advantage of doing this inside one system, and it eliminates drift by construction. When the target is a different store and transactions cannot span the two, prefer change-data-capture from the source's replication log over application-level double writes, and reconcile continuously rather than hoping.
Backfills are bulk operations wearing a migration costume, and they deserve bulk-operation respect.
- Batch in small chunks, thousands of rows, not millions, with pauses between batches, keyed by primary-key ranges rather than OFFSET.
- Make the job idempotent and resumable, because it will be interrupted, and rerunning it must be safe.
- Throttle against replication lag: pause the backfill when replicas fall behind, because a backfill that saturates the write path is just a slow outage.
- Verify with counts and checksums per range before declaring the migrate phase done. Trust is not a verification strategy.
The operational details that actually bite
Beyond the pattern, a short list of mechanics prevents most database-migration incidents on Postgres. Set a lock timeout of a few seconds for every DDL statement, so a migration that cannot get its lock fails fast and retries later instead of stalling the queue behind it. Create indexes concurrently, always, and expect to handle the invalid-index case on failure. Add columns without volatile defaults on older versions, and add NOT NULL constraints in two steps, as a NOT VALID constraint first, validated afterwards, so the table is never scanned under an exclusive lock. Treat foreign keys the same way. And separate migration deploys from code deploys, so either can roll back alone.
A checklist we actually use
Before any production migration ships, the pull request answers five questions in its description. What locks does each statement take, and for how long on the largest table it touches? Is every step backward-compatible with the currently deployed code? What is the rollback for each step, and is any step irreversible, like dropping a column? How is the backfill batched, throttled, and verified? And when does contract happen, with a ticket that has an owner and a date, because an expand without a scheduled contract is how databases accumulate a decade of ghost columns.
Zero-downtime migrations are not an advanced technique. They are a small vocabulary of habits, expand before you contract, write both before you read new, timeout your locks, batch your backfills, applied with enough consistency that schema changes stop being events. The best migration weekend is the one nobody can remember scheduling.