How to Handle Database Migrations in a Team Without Downtime

Every team has a migration horror story. Someone renames a column on Friday afternoon, the deploy runs, and the app starts throwing errors because the old code still expects the old name. The database is fine. The code is fine. The two just do not match at the moment the deploy lands.

Database migrations without downtime are less about clever tooling and more about ordering. Ship the schema change and the code change in separate steps, and keep each step compatible with the other side. This guide shows the pattern that makes that work, plus the team habits that keep it from falling apart when five people merge migrations in the same week.

Why migrations break production

Most migration failures are not bad SQL. They are timing problems. The app servers run old code while the new schema is already live, or the new code expects a column that the migration has not created yet. In a team the window is wider, and each step can be fine on its own. Together they leave the app talking to a schema it was never tested against.

The second common cause is the lock. A migration that rewrites a whole table, adds a unique index, or changes a column type can hold a lock for minutes on a busy table. Reads queue up, writes time out, and the on-call engineer gets paged for a deploy that looked harmless in staging. Staging rarely has production-sized tables, so the lock never shows up there.

The fix for both is the same: never make a change that needs the code and the schema to switch at the same instant.

The one rule behind database migrations without downtime

Old code must keep working after the migration runs, and new code must keep working before it runs. If both hold, you can deploy in any order and roll back the app without touching the database.

Before you merge a migration, ask: will the current production code still run against this schema, and will the new code run against the current one? If either answer is no, split the change into smaller steps until both answers are yes. This is what people mean by zero-downtime database migrations, and it works on Postgres, MySQL, and most other relational databases the same way.

The expand and contract pattern

The expand and contract pattern turns a breaking change into three safe steps. First you expand the schema by adding the new shape next to the old one. Then you move the code over. Then you contract by removing the old shape. Nothing is deleted until no code reads it anymore.

Renaming a column shows why this matters: a direct rename breaks every query that uses the old name, including code on servers that have not picked up the new deploy yet.

Step 1: expand

Add the new column, keep the old one, and write to both. A trigger or a short backfill copies existing values into the new column. At this point the old code is untouched and still works, because nothing it depends on has changed. If you want a deeper walkthrough of the same idea at small-team scale, this practical guide to migrations without downtime for small teams covers the same ground with more examples.

Step 2: move the code

Deploy code that reads and writes the new column. Give it a full deploy cycle, or a full day if traffic is uneven, so every server runs the new code and you have time to spot problems. The old column still exists, so rolling back the app is a normal deploy, not a database emergency.

Step 3: contract

Once no code references the old column, drop it in a separate migration. Keep this step in its own pull request, with a code search proving nothing reads the column anymore. The same three steps handle most schema changes:

Change you want Safe way Unsafe way
Rename a column Add new column, copy data, switch code, drop old Rename in one migration
Change a column type Add new column, backfill, switch code, drop old Alter the type in place
Make a column required Backfill nulls, then add the constraint Add the constraint first
Drop a table Stop reads and writes, wait a release, then drop Drop it with the code removal

Write code that works with both schema versions

Backwards-compatible migrations only work if the code cooperates. During a deploy, old and new code run side by side for minutes or longer. If the new code writes a column the old code does not know about, the old servers must ignore it without crashing. If the old code writes a column the new code has stopped reading, the new code must not treat the missing value as an error.

Three habits make this easier. First, treat every new column as optional at first: give it a default, or allow nulls, until the code that fills it is live everywhere. Second, never remove a field from an API response or a query result in the same release that stops writing it. Third, when you change an enum or a status value, add the new value before any code writes it, and remove the old value only after no rows use it.

A model that selects every column by default breaks the moment a column disappears, even if no feature uses it. Select the columns you need, and check what your queries actually touch when debugging a slow database query or reviewing a migration. The query plan often shows a dependency you forgot.

Backfill data without locking the table

Adding a column is cheap. Filling it is where teams get hurt. A single UPDATE over a million rows can hold a lock long enough to stall the app.

Backfill in batches of a few thousand rows, with a short pause between batches, and run the job outside peak hours. Add an index on the filter you use to find unfilled rows, or the batch job will scan the whole table each time. If the backfill is large, run it as a background job you can pause and resume, not as part of the migration itself. A migration should finish in seconds; a backfill can take hours, and the two should not share a deadline.

Rollbacks: plan the way back before you go forward

Every migration needs a database migration rollback plan that you have actually tested, not just a down migration that exists on paper. Some changes have no clean reverse: once you drop a column, its data is gone, and a down migration cannot bring it back. That is why the contract step comes last and separately. Dropping data is a decision, not a side effect.

For the app side, keep the rollback simple. If a deploy goes wrong, the first move is to roll back the code to the previous release. That only works if the schema is still compatible with the old code, which is exactly what the expand step guarantees. Pair that with a rollback plan using Docker tags so the previous image is one command away, and practice the sequence before you need it at 2 a.m.

Team habits that keep migrations boring

Most migration pain is a process problem. A few habits cover most of it:

  • One migration per change. A pull request that adds a column, renames another, and backfills a third is impossible to roll back in pieces. Split it.
  • Review migrations like code. A second pair of eyes should check for long locks, missing indexes on new foreign keys, and defaults that rewrite the table. A reviewer who knows the table sizes catches more than any linter.
  • Run migrations in CI. Apply them against a seeded database on every pull request, and run the test suite against the migrated schema. A GitHub Actions pipeline for Node.js is a good place to wire this in.
  • Agree on who deploys when. If two people run migrations at the same time against the same database, one of them will fail or, worse, both will succeed in the wrong order. A migration lock in your tooling helps, but so does a shared deploy window or a quick message in the team channel.
  • Test on production-sized data. Restore a recent backup to staging once in a while and time the migration. A migration that takes 40 seconds on a million-row table will take far longer on a hundred million rows, and you want to learn that on a Tuesday, not during a launch.

None of this needs a maintenance window. It needs a team that treats the database as shared infrastructure and changes it in steps small enough to undo. The same thinking applies beyond the database: it pairs well with zero-downtime deployments on Kubernetes, where the app side gets the same treatment.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.