Skip to content
engineering workbench.by StudyPort Academy
Learning path
Real-World18 min

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:

  1. Expand Phase: Add the new column as nullable (e.g. ADD COLUMN full_name TEXT). Old application code keeps running smoothly without errors.
  2. Dual-Write Phase: Deploy new code that writes to BOTH old and new columns, while continuing to read from old columns.
  3. Backfill Phase: Run a background job in small batches to fill in the new column for existing historical rows without locking tables.
  4. Read Switch Phase: Deploy code that reads from the new full_name column.
  5. Contract Phase: Once all old server versions are retired and verified, safely drop the old columns.

What changes

Big-Bang Migration vs Expand/Contract Pattern
AttributeBig-Bang Migration (Risky)Expand/Contract Pattern (Zero-Downtime)
Deployment StyleRequires taking application offline during maintenance windowExecuted live with zero downtime across rolling deployments
Backward CompatibilityBreaks immediately if old application code runsDatabase schema remains fully compatible with all running versions
Rollback SafetyHard rollback: failed migration leaves system brokenEasy rollback: old columns remain active until verified
Implementation EffortSingle dangerous scriptMulti-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.

    01What is the core principle of the Expand/Contract pattern in database migrations?
    02Why is directly renaming a database column dangerous in zero-downtime deployments?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress