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).
Diagram
Section titled “Diagram”- Only once every row has been backfilled and verified does any code read from the new column.
- 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.
Safe and unsafe changes
Section titled “Safe and unsafe changes”| 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.
How it works
Section titled “How it works”- 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.
When to use it
Section titled “When to use it”For any database behind a system that must stay available, and any team practicing continuous delivery or rolling deployments.
When to avoid it
Section titled “When to avoid it”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.
Common pitfalls
Section titled “Common pitfalls”- 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.
Related pages
Section titled “Related pages”- Failure story: The backfill that overwrote a live update
- See it applied: Example Lending, a three-stage migration with a rehearsed cutover
- Replication and high availability
- Partitioning and sharding
- Infrastructure as code
- Scenario: legacy core system
Glossary: strangler fig
Further reading
Section titled “Further reading”- Martin Fowler, Parallel change (opens in a new tab) (by Danilo Sato)
- Martin Fowler and Pramod Sadalage, Evolutionary database design (opens in a new tab)
- PostgreSQL, CREATE INDEX: building indexes concurrently (opens in a new tab)
- GitHub, gh-ost: online schema migrations for MySQL (opens in a new tab)
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.