Skip to content
Commitwise
Menu

How do you change a database schema without downtime when several app versions run at once?

BackendSenior

In shortUse expand and contract: first make backward-compatible additions that both old and new code tolerate, deploy code that uses them, backfill data in batches, and only remove old columns after no running version needs them. Every migration must be safe with the previous app version still running.

Model answer

During a rolling deploy, old and new versions run side by side, and a rollback brings the old one back. So every schema state must work with two app versions.

Expand and contract, for example renaming customer_name to full_name:

  1. Expand: add full_name (nullable). Old code ignores it.
  2. Dual write: deploy code that writes both columns and reads full_name with a fallback to customer_name.
  3. Backfill existing rows in small batches (thousands of rows per transaction, with pauses), so you don’t lock the table or flood replication.
  4. Switch reads to full_name only; add the NOT NULL constraint once every row is filled (in PostgreSQL, add it as NOT VALID and validate separately).
  5. Contract: after no running version (including the rollback target) uses customer_name, stop writing it, then drop it in a later release.

Dangerous DDL: operations that rewrite the table or take long exclusive locks (changing column types, some defaults on old database versions, adding indexes without CREATE INDEX CONCURRENTLY). Set a lock_timeout on migration sessions so a blocked migration fails instead of queuing every query behind it.

Tooling: versioned migrations with Flyway or Liquibase, run as a separate step before the deploy, reviewed like code, and tested against a production-sized copy. Mobile APIs add another constraint: old app versions keep calling old endpoints for years, so API contracts must stay compatible longer than the schema.

What they're looking for

The expand-and-contract sequence, why rolling deploys and mobile clients require compatibility, safe vs dangerous DDL (locks, table rewrites), batched backfills, migration tooling (Flyway, Liquibase), and rollback thinking.

Common mistakes

  • Renaming a column in one migration.
  • Adding a NOT NULL column with a volatile default on a huge table in one step.
  • Creating indexes without CONCURRENTLY on PostgreSQL.

More backend questions

Esc

Type to search the whole site.