The shape
You need to move state from one place to another while it is being read and written. It might be a table moving to a new shard, a data store moving to a different engine, a cache tier moving to a new cluster, or a queue moving to a different broker. You cannot stop traffic to do it, because nobody will give you a maintenance window. So a migration that needs a window never happens, and an overloaded shard stays overloaded for another six months.
The migration runs in five phases: backfill, double write, verify, shift reads and retire.
The order of the phases is not the hard part. One property has to hold through all of them: the source remains authoritative and complete until the final step. Authoritative means the source's copy is the one that counts. While the source is complete, you can undo any earlier step just by pointing back at it, and no rollback is cheaper than that. If you design the migration so this property holds, the rest is mechanics.
The five phases
1. Backfill. A job copies the existing data to the destination while the source keeps serving all traffic. Nobody reads the destination yet, so an incomplete copy there does no harm. The job can take as long as it needs. Throttle it, meaning cap how many rows per second it copies, so it does not overload the source. When it fails, restart it.
2. Double write. Every write now goes to both stores. The source is still the only store that serves reads. From now on the destination keeps up with the source instead of falling further behind. Double writing has to be switched on before the backfill copies its first row, so in practice phases 1 and 2 run together. If it starts later, a row that changes after the backfill copied it, and before double writing began, stays out of date in the destination for good.
3. Verify. Traffic does not change at all in this phase. A job compares the two stores all the time and reports drift, meaning rows where the two disagree. It keeps running until it has been green, with no drift reported, for days rather than minutes.
4. Shift reads. Reads move to the destination gradually: 1 percent, then 10, then 50, then all of them. Double writes continue the whole time, so the source is still complete, and moving reads back is a config change.
5. Retire. Stop writing to the source, then drop it. This is the first step you cannot undo, so take it deliberately.
Why double writing diverges
"Write to both" sounds atomic, meaning it either happens completely or not at all. It is two separate writes, and they can drift apart in four ways.
Each of the four has its own fix. Once you know which ones you are exposed to, you know what the verifier has to look for.
A partial failure, where the second write fails, needs that write to be retried durably, so the retry survives a crash. That usually means feeding the destination from a change stream or an outbox, instead of writing to it inline in the request. A change stream is the source database's own log of changes, such as the Postgres write-ahead log (WAL) or the MySQL binlog. A change data capture (CDC) tool such as Debezium reads that log and applies each change to the destination. An outbox is a table that the request writes in the same transaction as the source row, and a separate relay process reads it and writes to the destination. Writing to the destination inline, in the request handler, is the simplest way to build this pattern and the most fragile. The handler can crash between the two writes, and nothing records that the second write is still owed.
A backfill race needs the backfill to skip any row in the destination that is newer than the copy it holds. It usually checks this by comparing a version number or a timestamp before it writes.
A non-deterministic value, one that comes out different each time it is computed, has to be computed once and passed to both writes. Any default the database fills in, any generated identifier and any now() will differ between two calls.
An ordering difference needs one place that decides the order of writes. In practice that means feeding the destination from the source's own change log, instead of writing to both stores in parallel. This is the strongest form of the pattern, because it removes three of the four failures by design. The log is durable, so a failed write is retried. It carries the values the source stored, so nothing is computed twice. And it has one order, the order in which the source committed. Only the backfill race is left, so the backfill still needs its version check. For anything large, CDC is worth its setup cost for this reason. The CDC tool has to start reading the log no later than the moment the backfill starts, for the same reason double writing has to start first.
What to verify, and how
The comparison runs all the time, not as a single audit at the end. It has three levels, from cheapest to most expensive.
Counts and checksums by range. Split the keys into ranges, and compare each range's row count and a checksum of its keys on both sides. This is cheap enough to run all the time. It catches missing rows and badly corrupted ranges. It misses a wrong value inside a row, because the row count and the keys stay the same.
Row-level sampling. Pick random keys, fetch each row from both stores and compare every field. This catches encoding and type problems. Set the sampling rate high enough to find a defect on 0.01 percent of rows in a time you are willing to wait.
Read shadowing. Serve every real read from the source, and also send the same read to the destination. Compare the two answers, and do not use the destination's answer. This is the most valuable of the three. It checks exactly the reads your application makes, including the unusual ones, instead of the rows you thought to sample.
A defect affecting 0.01 percent of rows, on 400M rows
affected rows 40,000
Row-level sampling at 1,000 comparisons/minute
P(a given sample hits an affected row) = 0.0001
expected samples to find one = 10,000
→ about 10 minutes to the FIRST hit
But a defect on a rare code path, say 1 in 10M rows
affected rows 40
expected samples to find one = 10M
→ about 7 days of sampling
Read shadowing instead, at 20,000 reads/minute
it exercises real access patterns, so a defect on a
rare path is hit as often as that path is used
→ found in roughly the time it takes real traffic
to touch it, which is the only real bound there is
"Green for days" is not caution, it is the sampling
arithmetic. Green for an hour tells you about your
common rows and nothing about your rare ones.What it costs
Double writing roughly doubles the write cost while it runs, and it usually runs longer than planned. You operate both systems at once, with monitoring and on-call for each. The migration code is temporary, but it will be in production for months, so it has to be as good as permanent code.
The less obvious cost is that the application now has a mode. Which store is authoritative is now a piece of state, and every instance has to check it. If two instances hold different values for it, that is a failure of its own. Keep this switch in one place, and make it dynamic so a rollback does not need a deploy. Give it a version number, so an instance can tell when its copy is out of date.
Where it shows up in the course
| article | the form it takes there |
|---|---|
| Change Data Capture | feeding the new store from the old one's log, which leaves only the snapshot handoff to get right |
| Choosing a Database | keeping a derived store, such as a search index, in step with its source while both are live |
| Deployments: Blue-Green and Canary | expand and contract applies the same rule to one column: the way back narrows when writes to the old column stop |
| Idempotency and Exactly-Once Delivery | moving the key store without losing the de-duplication guarantee while both data stores are live |
| Message Queues vs Event Streams | writing to a database and publishing an event is a dual write, and the outbox is the same fix applied in one step rather than five |
| Partitioning and Sharding | moving a shard to a new machine without a maintenance window, and why every phase before the last is reversible |
Getting it wrong
Skipping verification. Verify is the one phase where nothing visibly changes, and it is the only one that finds what double writing broke.
Doing the second write inline. The request handler can crash between the two writes, and nothing records that the second one is owed. Feed the destination from a change stream or an outbox instead.
Letting the backfill overwrite live data. The backfill has to skip any row that is newer than its copy. So it compares versions before it writes, instead of writing blindly.
Computing non-deterministic values twice. Compute timestamps, identifiers and database defaults once, and pass the same value to both stores.
Cutting reads over all at once. A gradual move shows you a problem while it still affects few users. Real traffic at 1 percent of reads tests the destination better than any staging environment.
Retiring the source too early. The source is your rollback. Keep it, and keep double writing, until the destination has served all reads long enough for any problem to have shown up.