Engineering 3 min read Updated
Zero-downtime database migrations in practice
In short
Code deploys and schema changes rarely happen at the exact same millisecond. Use expand-and-contract migrations so old and new application code run safely against the same database.
Taking an e-commerce checkout or SaaS workspace offline for twenty minutes to run a database migration is rarely acceptable once customers rely on it. Yet deploying code and altering tables at the same time causes errors if old and new application code disagree on columns, constraints or types. Separating schema transitions into backward-compatible steps eliminates both downtime and broken rollbacks.
Why single-step migrations break live releases
During a rolling deploy, two versions of your application share the same database for seconds or minutes. Even on a single-server deploy (Release and rollback), if you need to roll back the application binary after a schema change, any dropped or renamed column immediately breaks the previous release. The rule of thumb is simple: every schema migration must remain compatible with both the currently running code and the next release.
The expand-and-contract pattern
Instead of modifying a column in place, split breaking changes—such as renaming a column, splitting a field or adding a NOT NULL constraint—into three small releases:
| Phase | Database change | Application change | Safe rollback? |
|---|---|---|---|
| 1. Expand | Add new nullable column or table; add concurrent index | None yet (or prepare read fallback) | Yes; old code ignores new column |
| 2. Dual-write & backfill | Run batched backfill script for older rows | Write to both old and new columns; read new with fallback to old | Yes; old column stays populated |
| 3. Contract | Validate constraint, drop old column after soak period | Read and write new column only | Previous release no longer depends on old column |
Locking hazards in ALTER TABLE
Even an innocent-looking migration can stall an entire application if it takes a heavy table lock. In PostgreSQL’s ALTER TABLE, operations such as changing a column type or adding certain constraints acquire an ACCESS EXCLUSIVE lock. If that migration waits behind one slow reporting query, every subsequent SELECT and INSERT queues behind the waiting ALTER, exhausting web worker connections within seconds.
Similarly, SQLite’s ALTER TABLE supports fast metadata-only ADD COLUMN under strict conditions, whereas dropping or altering columns in older patterns can rewrite tables. Always test migrations against a production-sized copy first (Staging environments), set a short lock timeout so a blocked migration aborts instead of freezing traffic, and create indexes without blocking writes where your database supports it (see Database indexes).
Backfilling existing rows without overloading I/O
Never update millions of rows in a single giant UPDATE transaction. Long transactions hold row locks, inflate undo/WAL logs and starve user requests. Instead:
- Process rows in primary-key ranges of 500 to 5,000 rows per transaction with a brief pause between batches.
- Make the backfill idempotent (WHERE new_col IS NULL) so it can be stopped and resumed safely.
- Monitor database CPU, replication lag and disk I/O while the backfill runs.
Checklist before running a production migration
- Verify the migration script does not rename, drop or tighten a column still used by the currently live code.
- Confirm a tested database backup exists right before structural changes.
- Set an explicit lock timeout on DDL statements so a stuck lock fails fast rather than queuing traffic.
- Run heavy backfills as resumable background batches outside the HTTP deploy hook.
Exact lock behaviour differs substantially across PostgreSQL, MySQL/InnoDB and SQLite versions. Always consult your target engine’s documentation and rehearse the migration on a realistic dataset before touching production.
Planning a complex schema refactor on a live product? Discuss it through our enquiry form.
Common questions
- Why do standard rename or drop-column migrations break live deployments?
- During rolling deploys or rollbacks, old and new application code run against the same schema simultaneously. Dropping or renaming a column in one deploy breaks whichever version still reads or writes the old name.
- What are the three steps of expand-and-contract?
- First expand the schema by adding the new nullable column or table compatibly; second deploy code that dual-writes and backfills historical rows in small batches; third contract by removing old reads, writes and the deprecated column once traffic no longer uses it.
- Why can ALTER TABLE stall a production database even on small queries?
- Some ALTER TABLE operations acquire an exclusive table lock or rewrite the entire table. While waiting for the lock behind a long-running query, incoming reads and writes queue up behind it and exhaust connections.
Sources
- ALTER TABLE, PostgreSQL Documentation. Accessed 24 November 2026
- ALTER TABLE, SQLite Documentation. Accessed 24 November 2026
- TechnicalDebt, Martin Fowler. Accessed 24 November 2026
Further reading
-
Releases and rollbacks you can reverse
Small, frequent releases are easier to test and easier to reverse than large ones, so plan the way back before you ship.
-
When a database index helps and when it only adds cost
An index helps when a frequent query can use it to find, filter or sort rows, and every write pays for it, so add one only for a query you actually run.
-
SQLite or MySQL for a small product
Choose SQLite when one application on one host reads and writes the data and writers can take turns; choose MySQL when many machines or simultaneous writers share it.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea