Skip to content
Optimal Lab

System design · interactive

Zero-downtime schema migrations

You want to rename users.name to full_name. During a rolling deploy, old and new app versions run at the same time against one database, so every schema change must work with both. Compare a one-step rename with expand and contract.

  1. before
  2. migrate
  3. deploy
  4. done

users table columns

name

server 1

v1

server 2

v1

server 3

v1

All servers run v1, which reads and writes name.

Approach

1 of 4

Step 1 of 4

  1. 1

    Old and new code overlap

    Rolling deploys, canaries and rollbacks mean several versions hit the same schema at once.

    So each step must be compatible both ways.

  2. 2

    Expand: only add

    New columns are nullable or have defaults; old code ignores them.

    Big-table changes need online tools.

  3. 3

    Dual write, backfill, switch reads

    Each is its own deploy, and each can be rolled back on its own.

    Backfill in batches to avoid locking.

  4. 4

    Contract: remove the old

    Once nothing reads or writes it, drop the old column in a later release.

    Slower, but nothing breaks.

In production

Tools and technologies

Migration tools help, but expand and contract is a process.

Not tied to one language — use these names when you search docs or design a system.