Database Migrations Without Downtime: A Practical Guide for Small Teams
A schema change that takes five seconds on your laptop can take your app offline for an hour in production. The table is bigger, the lock lasts longer, and the old code still expects the old columns. Database migrations without downtime come from a few habits any small team can follow: small steps, old and new code working at the same time, and always a way back.
This guide covers that pattern, the rules for backward-compatible migrations, and a step-by-step example: renaming a column.
Why migrations take apps down
Most downtime comes from three causes. First, a migration locks a table while it rewrites data, and every query behind it queues up. On a small table this is invisible; on a table with a few million rows, adding a column with a default or rebuilding an index can block writes for minutes.
Second, deploy and migration disagree about the data: new code reads a column that does not exist yet, or old code writes to one just dropped. Third, there is no tested way back when a migration fails halfway.
The expand and contract pattern for database migrations without downtime
The expand and contract pattern is the core idea behind database migrations without downtime: instead of changing the schema in one step, split it into deploys that are each safe on their own.
Expand means adding the new thing without removing the old one: a new column, a new table, a new index. The old code keeps working because nothing it depends on has moved. Then move data and code to the new shape while both shapes stay live, so old and new code run side by side.
Contract means removing the old thing only after every running instance uses the new shape. The drop happens in a later deploy, never the same one that introduced the replacement. It feels slow the first time. In practice it beats the alternative, because a failed deploy becomes a normal rollback instead of an incident. The same discipline starts with the basics, as covered in our guide on how to install PostgreSQL on Ubuntu and secure it properly.
Backward-compatible migrations: the rules that matter
A migration is backward-compatible when the old version of your code still works after it runs. These rules make backward compatible migrations routine:
- Add columns as nullable, or with a default that does not rewrite the table. In PostgreSQL 11 and later, a constant default skips the rewrite; in MySQL it may not, so test on production-sized data first. See our MySQL vs PostgreSQL comparison for limited RAM.
- Never drop or rename a column in the same deploy that stops using it. Remove code references first, deploy, then drop the column once no old instances remain. A rename is a drop plus an add from the old code’s point of view, so use the multi-step process in the next section.
- Make new constraints safe to add. Backfill first, then add the NOT NULL constraint. A unique constraint on a large table takes a lock, so use the concurrent index option where offered.
- Keep data types compatible. Widening a column, for example from integer to bigint, is usually safe. Narrowing it, or changing text to an enum, can fail on existing rows, so check the data first.
- Write migrations that can run twice. Use IF NOT EXISTS and IF EXISTS where supported, so a half-finished run can be retried.
Renaming a column safely, step by step
Renaming a column breaks more deploys than any other change. Say your users table has name and you want full_name.
Step 1: Add the new column
ALTER TABLE users ADD COLUMN full_name TEXT;
Nothing breaks. Old code ignores the new column.
Step 2: Backfill in small batches
Copy the data over in batches so you do not hold a long lock:
UPDATE users SET full_name = name
WHERE id BETWEEN 1 AND 10000 AND full_name IS NULL;
Repeat until no rows are left, ideally from a background job during quiet hours.
Step 3: Write to both columns
Change the code so every write updates both name and full_name, then deploy.
Step 4: Switch reads to the new column
Read from full_name and deploy again. If something goes wrong, the old column still holds the same data, so rolling back the code is enough.
Step 5: Stop writing to the old column
Once every instance reads the new column, deploy a change that writes only to full_name.
Step 6: Drop the old column
ALTER TABLE users DROP COLUMN name;
Do this in its own deploy, days later if you can, in case a forgotten report still reads the old column.
Six deploys for one rename sounds like a lot, but each is small, safe, and reversible on its own.
Rolling deploys and running old and new code side by side
Expand-and-contract only works if your deploy runs two code versions at once for a short window. A rolling deploy does that: it replaces instances one at a time instead of stopping everything and starting the new version.
For that window to be safe, every migration must be compatible with both versions: new code must tolerate the old schema, and old code must tolerate the new one. That is the test for every migration: if it ran now and the code deploy failed halfway, would the fleet still serve traffic?
Two habits make rolling deploys boring instead of scary. Run migrations before the new code goes out, and confirm the migration alone does not break the old code. Switch a few instances at a time, so a bad deploy affects a fraction of traffic and you can stop the rollout early. The same thinking applies beyond the database: if you run a single VPS, our guide to zero-downtime deploys with blue-green on a single VPS shows how to structure releases so the old version stays live until the new one proves itself.
Rollback plans that work when the deploy goes wrong
A rollback plan is not “restore from backup and hope.” Backups are slow, and restoring one loses every write made since the backup. A real plan covers three things: the code rollback, the migration rollback, and the trigger.
First, the migration rollback. Some migrations reverse cleanly: dropping an added column, removing an added index. Others do not. A rename that already dropped the old column cannot be undone without restoring data, so destructive steps go last and alone. Before any migration, write down the reverse migration and check it loses no data. If it does, split the change further.
Second, the trigger. Decide in advance what makes you roll back: an error rate above a threshold, a specific exception in the logs, or a check that fails after deploy. A written trigger stops the team debating while the site is down. If the migration cannot be reversed, plan to roll forward with a fix prepared before the deploy.
Testing migrations before they touch production
The cheapest place to catch a bad migration is a copy of production data. A migration that runs in a second on an empty local database can take twenty minutes on a table with real volume.
Restore a recent production backup to staging, scrubbed of anything sensitive, and run every migration there first. Measure how long each one takes and watch for locks. One that holds a lock for more than a few seconds on a busy table should be split into smaller steps or run concurrently.
Also test the failure modes: run the old code against the new schema and check it still works, then the new code against the old schema. In CI, apply migrations to a fresh database twice to confirm they are repeatable, then run the test suite.
Index changes deserve the same treatment: a missing index can turn a backfill from seconds into hours. Our guide to writing faster SQL queries with indexing basics shows how to check whether a query uses the index you expect.
A checklist you can reuse for every migration
Walk through this list before each release:
- Can the old code run against the new schema? Can the new code run against the old schema?
- Does the migration lock any table, and for how long?
- Is every destructive step, like a drop or rename, in a later deploy on its own?
- Is there a tested reverse migration, a roll-forward fix, and a rollback trigger everyone knows?
- Was the migration run against staging restored from production?
- Are large backfills batched, and can they be paused and resumed?
Teams that follow this list stop treating migrations as risky events. A schema change becomes a routine sequence of small, reversible steps. Start with the next migration on your board and split it into an expand and a contract.