Database Migrations: Safe Changes and Zero-Downtime Rollouts
Understand the idea
How to change schemas in a running production database without causing downtime.
The idea
In production software development, the database is always evolving. Application code changes often, requiring new columns, updated names, and modified . In a live app with real users active around the clock, updating database structure requires careful planning so your website never goes down.
The core rule of zero-downtime database migrations is: Every schema change must be backward-compatible with the currently running application code. In modern rolling deployments, servers update one by one, which means old code and new code will be talking to the database at the same time for several minutes.
To change database structure safely without downtime, use the Expand/Contract pattern:
- Expand Phase: Add the new column as nullable (e.g. ADD COLUMN full_name TEXT). Old application code keeps running smoothly without errors.
- Dual-Write Phase: Deploy new code that writes to BOTH old and new columns, while continuing to read from old columns.
- Backfill Phase: Run a background job in small batches to fill in the new column for existing historical rows without locking tables.
- Read Switch Phase: Deploy code that reads from the new full_name column.
- Contract Phase: Once all old server versions are retired and verified, safely drop the old columns.
What changes
| Attribute | Big-Bang Migration (Risky) | Expand/Contract Pattern (Zero-Downtime) |
|---|---|---|
| Deployment Style | Requires taking application offline during maintenance window | Executed live with zero downtime across rolling deployments |
| Backward Compatibility | Breaks immediately if old application code runs | Database schema remains fully compatible with all running versions |
| Rollback Safety | Hard rollback: failed migration leaves system broken | Easy rollback: old columns remain active until verified |
| Implementation Effort | Single dangerous script | Multi-stage phased rollout over several releases |
The command
Safe backward-compatible schema expansion
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Run the example to see SQLite safely expand the schema with a nullable column, backfill data, and verify both columns coexist peacefully.
Guided challenge
Validate safe column addition
Update the function to return true when isNullable is true (safe for rolling deploys) and false when false.
Validate safe column addition
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
true false
Hints 0/1
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.