HomeRoadmapsDB Migration Day 2
Database Migrations & Zero-Downtime Schema Evolution Β· Day 2 Β· 20 min

Expand-and-Contract State Machine & Dual-Write
Zero-Downtime Schema Evolution β€” Day 2

Model the 4-phase zero-downtime migration lifecycle (Expand, Dual-Write + Backfill, Switch Reads, Contract) and prove N-1 and N application pods coexist without errors.

⚑ Runtime: Node.js 22 LTS Β· VitestπŸ›‘οΈ Database Reliability & Schema Evolution QE Architect
Expand-and-ContractDual-WriteN-1 CompatibilityState MachineRolling Deploy
Interactive Schema Evolution Lab

Expand-Contract & Lock-Risk Migration Simulator

Safe Expand-and-Contract (Zero-Downtime)
SET lock_timeout = '2s';
ALTER TABLE users ADD COLUMN display_name TEXT;
CREATE INDEX CONCURRENTLY idx_users_display_name ON users (display_name);
EXPAND
Lock: NONE (metadata)
N-1: OK Β· N: OK
Nullable column added
DUAL_WRITE
Lock: ROW_EXCLUSIVE
N-1: OK Β· N: OK
Chunked backfill + dual-write
SWITCH_READS
Lock: NONE
N-1: OK Β· N: OK
N reads display_name; N-1 safe
CONTRACT
Lock: ACCESS_EXCLUSIVE (2s guard)
N-1: OK Β· N: OK
N-1 pods retired, drop full_name
PASS: Highest lock is SHARE_UPDATE_EXCLUSIVE with lock_timeout guard.
πŸ“¦Sample Codebase & Working Solution (Day 2)Available Now
View on GitHub

The official reference codebase for Day 2 is hosted in the repository. Clone the repository and navigate to tests/day2-expand-contract.spec.ts to run the working solution.

Git Clone β€” Working Solution
git clone https://github.com/777abhi/db-migration-qe-roadmap.git
cd db-migration-qe-roadmap/tests/day2-expand-contract.spec.ts
🏭Production Context & Enterprise Implementation
πŸ’‘ Why We Are Doing This: During a Kubernetes rolling deployment, old (N-1) and new (N) pods run simultaneously against the same database schema. Breaking column renames or premature drops cause immediate 500s.
🏒 Real-World Production Usecase: Validating multi-release schema evolution (e.g., renaming full_name to display_name) across canary and rolling deployments.
βš™οΈ How Implemented in Production: Explicit state machine enforcing EXPAND -> DUAL_WRITE_BACKFILL -> SWITCH_READS -> CONTRACT with automated N-1/N compatibility assertions.
Phase 1

Verify N-1 & N Compatibility Across All Phases

Step 2.1: Run Day 2 Expand-Contract Tests
Terminal β€” Day 2 Lab
npm run test:day2
Step 2.2: Sample Codebase & Working Solution (Day 2)
src/stateMachine/expandContractEngine.ts
const VALID_TRANSITIONS: Record<MigrationPhase, MigrationPhase[]> = {
  V1_BASELINE: ['EXPAND'],
  EXPAND: ['DUAL_WRITE_BACKFILL', 'ROLLED_BACK'],
  DUAL_WRITE_BACKFILL: ['SWITCH_READS', 'ROLLED_BACK'],
  SWITCH_READS: ['CONTRACT', 'ROLLED_BACK'],
  CONTRACT: [],
  ROLLED_BACK: ['EXPAND'],
};
Day 2 Key Learnings
  • Enforced valid Expand-and-Contract phase transitions so Contract cannot run prematurely.
  • Simulated concurrent N-1 and N writers during DUAL_WRITE_BACKFILL and SWITCH_READS.
  • Verified that Contract safely removes the legacy column only after N-1 pods are retired.

🧠 Knowledge Check

In an Expand-and-Contract migration, when is it safe to execute the CONTRACT phase (dropping the old column)?