The Short Answer
How do you migrate a database without downtime?
Split every breaking change into backward-compatible steps, known as expand and contract. First add the new structure alongside the old. Then change the application to write to both and backfill existing data in batches. Switch reads to the new structure, confirm it works, and only then remove the old one. At every step the running application keeps working, and each step can be rolled back.
Read the transcript
A developer renames one database column. The new code works. But the old code is still running for a minute. And for that minute, the site is broken. Database changes are where many outages begin. Tables get locked, and old and new code disagree. So how do you change a live database safely? Use expand and contract. Add the new column. Write to both. Copy the old data across in batches. Switch reads to the new one. Then remove the old column, in a later release. It takes several deployments instead of one. But every step keeps the site working, and every step can be undone. Two more habits help. Test changes on production-sized data. And set lock timeouts, so a stuck change fails fast. Never make a change the running code can’t survive. The full guide is free at loomstrat.com.
Why Migrations Cause Outages
- Locks. Some schema changes lock a table while they run, blocking reads or writes. On a large table that can take minutes.
- Version mismatch. During a deployment, old and new versions of the application run at the same time. A column renamed for the new version breaks the old one.
- Long backfills. Updating millions of rows in one transaction overloads the database.
- No way back. A destructive change, such as dropping a column, cannot be undone if the release fails.
Check how your database locks for each operation. PostgreSQL documents which commands take which locks.
The Expand and Contract Pattern
Also called parallel change, described by Martin Fowler. Renaming a column called name to full_name shows the steps:
- 1
Expand
Add the new full_name column, allowing empty values. Nothing uses it yet.
- 2
Dual write
Deploy code that writes to both name and full_name.
- 3
Backfill
Copy existing values into full_name in small batches.
- 4
Switch reads
Deploy code that reads from full_name. Keep writing to both.
- 5
Verify
Check data matches and monitor for errors.
- 6
Stop writing the old column
Deploy code that uses only full_name.
- 7
Contract
Drop the old name column in a later release.
It takes several deployments instead of one. With a working CI/CD pipeline that is cheap, and every step is safe.
Safe and Unsafe Operations
| Change | Risk | Safer approach |
|---|---|---|
| Add a nullable column | Usually low | Add it directly |
| Add a column with a default | Depends on database version | Check behaviour; add nullable then backfill if unsure |
| Add an index | Can block writes | Build it concurrently where supported, such as PostgreSQL CREATE INDEX CONCURRENTLY |
| Rename a column or table | Breaks running code | Expand and contract |
| Change a column type | Can rewrite and lock the table | New column, dual write, backfill, switch |
| Add a NOT NULL constraint | Scans and locks | Add as not validated, backfill, then validate |
| Drop a column | Irreversible | Stop using it first; drop in a later release |
See the PostgreSQL notes on building indexes concurrently.
Moving to a New Database
The same idea scales up to moving between databases, for example from a legacy system or between providers:
- 1
Copy existing data
Take an initial copy into the new database.
- 2
Keep it in sync
Use change data capture or dual writes so new changes flow across.
- 3
Compare
Run checks that row counts and sample records match.
- 4
Shadow reads
Read from both and compare results without showing the new ones to users.
- 5
Cut over gradually
Move reads, then writes, feature by feature or customer by customer, using feature flags.
- 6
Keep the old one briefly
Retain it, in sync if possible, until you are confident.
This is the database version of the strangler approach in legacy system modernisation.
Good Practice
- Version migrations in code with a migration tool, and run them automatically in the pipeline.
- Test on production-sized data. A change that takes a second on test data can take an hour on real tables.
- Set lock timeouts so a blocked migration fails quickly instead of queueing all traffic behind it.
- Backfill in batches with pauses, and make the job safe to restart.
- Take and test backups before risky changes.
- Separate schema changes from code releases where possible.
Frequently Asked Questions
What is expand and contract?
A way of making breaking database changes in backward-compatible steps: add the new structure, migrate to it, then remove the old one.
Can I rename a column without downtime?
Yes, by adding a new column, writing to both, backfilling, switching reads, and dropping the old column later.
Why does adding an index cause problems?
A normal index build can block writes on the table until it finishes. Many databases offer a concurrent option that avoids this.
How do I migrate to a new database without downtime?
Copy the data, keep the two in sync, compare them, move reads and writes over gradually, and keep the old database until you are confident.
Is a short maintenance window ever acceptable?
Sometimes, for internal tools or rare large moves. For customer-facing products, the incremental approach is usually worth the extra steps.