The Expand-Contract Pattern: Zero-Downtime Migration
The most dangerous operation in backend engineering is a breaking database schema change (e.g., renaming a column). If you just rename it, your existing application code will crash. The solution is the Expand-Contract (or Parallel Change) Pattern.
1. The 4-Step Playbook
Step 1: Expand
Add the new column to your database, but do not delete the old one.
- Action:
Step 2: Dual-Write
Update your application code to write to both the old and the new columns.
- Action: Every and now populates both fields.
Step 3: Backfill
Run a background script to copy data from the old column to the new column for all existing rows.
Step 4: Switch & Contract
- Update your code to read only from the new column.
- Stop writing to the old column.
- Finally, delete the old column:
Summary
The Expand-Contract pattern turns a "Breaking Change" into a series of "Non-Breaking Changes." It is the industry standard for maintaining 100% availability during complex refactoring.
