Design a Zero-Downtime Database Migration
Understanding the Problem
🚚 What is a zero-downtime database migration? A database migration moves a live application's data from one database to another: a new engine (MySQL to Postgres, or to DynamoDB), a new schema (splitting one big table into several), a new sharding layout, or a new region. "Zero downtime" means users keep reading and writing the whole time. There's no maintenance window where the site says "back soon," and no point where a write is lost.
Interviewers phrase this a few ways: "migrate a database with no downtime," "migrate when the old database doesn't support CDC," "use dual writes and backfill to move to a new store," or "replicate 30 regional databases into central tables." They share one core problem: two databases that must hold the same data while one of them is still taking writes. The regional variant is the same machinery running continuously, from many sources into one. We'll design the one-time migration first, then extend it to the 30-region fan-in in a deep dive.
Functional Requirements
Make the scenario concrete with your interviewer. Ours: the orders service stores everything in one large MySQL database with a schema that's grown messy over the years. We're moving it to a new Postgres cluster with a cleaner schema (the orders table is split into orders and order_items, and some columns change types).
Core Requirements
- The system should copy all existing data from the old database to the new one, transforming it to the new schema (the backfill).
- The system should keep the new database in sync with every write made to the old one until cutover, even though the old database doesn't expose a change log we can use.
- The team should be able to verify that the two databases hold the same data before trusting the new one.
- The team should be able to move reads, then writes, to the new database gradually, with no downtime and with the option to roll back at every step.
Below the line (out of scope)
- Rewriting the application's queries for the new schema (we assume the data access layer is updated as part of the project).
- Migrating other services that read the old database directly; we'll assume they go through the
ordersservice API.
Non-Functional Requirements
Core Requirements
- No downtime. Reads and writes keep working throughout. A pause of at most a second or so for a small slice of traffic at cutover is acceptable; a minutes-long freeze is not.
- No lost or corrupted writes. Every committed write to the old database must end up in the new one, in its final state.
- Limited production impact. The migration must not push the old database's p99 latency up by more than a small amount, say 10%.
- Reversible. Until the very last step, we must be able to switch back to the old database within minutes without losing writes.
Below the line (out of scope)
- Speed. The migration can take weeks; safety matters more.
Numbers: 2 TB, about 3 billion rows, 50,000 reads per second and 5,000 writes per second at peak.
Here's why the order of operations matters. If we throttle the backfill to 20,000 rows per second to protect the old database, copying 3 billion rows takes 150,000 seconds, about 42 hours. In that time, at peak rates, up to 750 million writes hit the old database. A snapshot copy is stale long before it finishes. So we must start capturing changes before the backfill starts, and the hard part of the design is making the backfill and the stream of live changes agree on every row.
The most common mistake is to describe a migration as "copy the data, then switch over." Your interviewer is waiting for the question "what about the writes that happen while you're copying?" Answer it before they ask.