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:
- Expand: add
full_name(nullable). Old code ignores it. - Dual write: deploy code that writes both columns and reads
full_namewith a fallback tocustomer_name. - Backfill existing rows in small batches (thousands of rows per transaction, with pauses), so you don’t lock the table or flood replication.
- Switch reads to
full_nameonly; add theNOT NULLconstraint once every row is filled (in PostgreSQL, add it asNOT VALIDand validate separately). - 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
- What is the N+1 query problem, and how do you find and fix it?Mid-level
- What does Spring Boot's auto-configuration do, and how do you change what it set up?Mid-level
- How does a Spring backend authenticate requests from a mobile app, and how do you make sure users only see their own data?Mid-level
- Your Spring Boot service in Kubernetes keeps restarting with exit code 137. How do you investigate and fix it?Senior