Online Migrations
By Ankur NayakReviewed
An online migration moves data to a new table, schema or database while the application keeps running. The common recipe: write to both old and new, backfill history, verify with shadow ('dark') reads, switch reads, switch writes, then remove the old path. Stripe, Notion, Figma and Slack have all described versions of it.
How it works
Stripe's 2017 post lays out the four-step pattern: dual-write to the old and new stores; change reads to the new store; change writes so only the new store is written; remove the old data and code. Each step is independently reversible, and the system is correct at every point in between.
Notion's 2021 move to sharded Postgres followed it closely: an audit log captured every write to the tables being migrated, a backfill copied historical data in about three days, and 'dark reads' fetched from both old and new databases and compared results before the switch-over, which needed five minutes of scheduled maintenance. In 2023, re-sharding from 32 to 96 machines used Postgres logical replication instead of application double writes, and finished with no perceived downtime.
Figma's 2023–2024 horizontal sharding added a cheaper first step: represent logical shards as Postgres views on the existing table, route all traffic through them, and roll back in seconds by pointing traffic at the main table again, before physically moving any data. Slack migrated its MySQL query load to Vitess over several years starting in 2017.
Key points
- Dual-write (or replicate) to old and new
- Backfill history in the background
- Verify with dark reads before trusting the new store
- Switch reads, then writes; each step reversible
- Clean up the old path last
When to use it, and pitfalls
Use it for
- Sharding or re-sharding a database
- Changing a schema or data model in a live system
- Moving to a new database technology
Watch out for
- Skipping verification: dark reads catch subtle mismatches
- Backfills overwriting newer writes; make them idempotent and ordered
- Forgetting background jobs and other code paths that still write the old store
- No rollback plan for each step
Trade-offs
Application double writes vs database replication
Use database-level replication when the target is the same engine; application double writes when the data model changes.
Pros: Replication: no application changes, faster sync (Notion: 3 days → 12 hours); Double writes: can transform data on the way
Cons: Replication: needs compatible schemas; Double writes: every write path must be found and changed
Used in
Related building blocks
Frequently asked questions
How do you migrate a database with zero downtime?
Write to both the old and new stores, backfill historical data, compare results with dark reads, switch reads to the new store, then switch writes, and finally remove the old path. Each step should be reversible. Notion's 2023 re-shard and Figma's sharding work used variations of this.
What are dark reads?
Reads sent to the new store in addition to the old one, whose results are compared but not returned to users. They show whether the new store gives the same answers before any user depends on it.