Intermediate LevelSoftware Engineering Principles
What is the Expand-Contract (Parallel Change) pattern for zero-downtime database schema migrations?
❓ Interview Prompt: Explain Expand-Contract Pattern (Parallel Change), 3 phases (Expand, Transition, Contract), zero-downtime database migrations, and column renaming.
Renaming a database column in a live system can break running application instances. The Expand-Contract (Parallel Change) Pattern executes schema refactoring in 3 zero-downtime phases:
3 Migration Phases:
1
Expand: Add the new column ('new_name') alongside the old column ('old_name'). Application writes to both columns simultaneously.
2
Transition: Backfill historical database data from 'old_name' to 'new_name'. Update application code to read from 'new_name'.
3
Contract: Remove legacy application code writing to 'old_name', then drop 'old_name' column from the database!
Code Implementation Example
sql
-- Phase 1 (Expand): Add new column without removing old column
ALTER TABLE users ADD COLUMN phone_number VARCHAR(20);
-- Phase 2 (Transition Data Backfill)
UPDATE users SET phone_number = legacy_phone WHERE phone_number IS NULL;
-- Phase 3 (Contract): Drop legacy column after application code migration
-- ALTER TABLE users DROP COLUMN legacy_phone;💡Executing Expand-Contract database schema refactoring in SQL.
🎯 Interview Key Takeaway
Refactor live database schemas using Expand-Contract (Parallel Change) to guarantee zero-downtime application deployments.
#Expand Contract#Database Migration#Zero Downtime#Intermediate
Back to all Software Engineering Principles Questions→