Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Architecture Leadership
Lesson 24 of 32

Database migrations without downtime

Change schemas and move data on live systems with expand-and-contract, online tooling and safe backfills.

16 min read

Why schema changes cause outages

During a rolling deploy, old and new application versions run at the same time against the same database. A migration that renames a column breaks the old version instantly. Some operations also take locks: on large tables, adding an index without the concurrent option, or changing a column type, can block writes for minutes. Every schema change must be compatible with both the version before it and the version after it. Treat the database as a shared, live dependency whose shape changes in small, compatible steps.

Expand and contract

The parallel change pattern splits a breaking change into compatible steps, each deployed separately. To rename email to email_address: Each step is independently deployable and reversible.

  • 1. Expand: add the new nullable column; deploy code that writes both and reads old
  • 2. Backfill: copy existing values in small batches
  • 3. Switch reads: deploy code that reads new (with a fallback if needed), still writing both
  • 4. Stop writing old: deploy code that writes only new
  • 5. Contract: drop the old column after a safety period

Safe backfills

Backfilling a billion rows in one statement will hold locks, bloat the transaction log and spike replication lag. Process in batches of a few thousand rows keyed by primary key, commit each batch, sleep between batches, and throttle on replica lag (pause if it exceeds a few seconds). Make the job idempotent and resumable from a checkpoint. Run it during lower traffic, watch database CPU and latency, and keep a kill switch. Verify the backfill with a count of rows still missing the new value before moving on.

Tooling and guardrails

Use the database's online features: CREATE INDEX CONCURRENTLY in Postgres, adding constraints as NOT VALID then validating separately, and setting a short lock_timeout so a migration fails fast instead of queueing behind a long transaction and blocking everything. For MySQL, gh-ost and pt-online-schema-change copy the table in the background. Lint migrations in CI (tools such as squawk for Postgres) to reject dangerous operations, and test migrations against a production-sized copy before release. Always have a tested rollback plan for each step.

Key takeaways

  • Every migration must work with both the old and new application version running at once.
  • Expand and contract breaks a breaking change into separate compatible deploys.
  • Backfill in small, idempotent, throttled batches that watch replica lag.
  • Use online index and constraint options, short lock timeouts and migration linting in CI.

Sign in to track lessons and earn badges.

Migration strategiesDeprecating systems

Coming up

Course: 0/32 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 9/10Design an E-commerce CheckoutDesign a tier-0 checkout with PCI scope reduction, idempotent orders, vendor fallbacks and an SLO you can defend.MAJORMedium
10/10
Design a Payment LedgerBring the whole course together on a money-critical ledger and frame it as a decision an executive team could approve.
MAJORHardPRO
All 10 challenges →