Zero-Downtime Database Schema Migrations
Deploying breaking schema changes to production databases with zero downtime using the expand-contract pattern.
Overview
Decouple schema changes from code changes
Breaking schema changes such as column renames, type changes and NOT NULL additions are among the highest-risk database operations. The expand-contract pattern decouples schema changes from code changes, removing the window where incompatible versions coexist. It runs in three phases.
- Expand: add the new column as nullable, and have the app dual-write to the old and new columns.
- Backfill: fill the new column for existing rows in rate-limited batches.
- Contract: switch reads to the new column, then drop the old one.
> [!IMPORTANT] Every intermediate state must work with both the old and the new app version, because both run during a rolling or blue-green deploy.
Interview questions
- You need to deploy a breaking schema change to a production database with zero downtime. How do you do it?
- What are the risks of running database migrations inside a deployment pipeline, and how do you mitigate them?
Sign up free to read the full topic
More SRE topics
Start free on Cariara · Coding problems · System design studio