# Dual Write: Migrating Data from Gen 1 to Gen 2 | gen2.dev

> Dual write keeps Gen 1 and Gen 2 data in step during a migration. The steps, why naive dual writes drift apart, and the safer variants: CDC and outbox.

Source: https://gen2.dev/guide/dual-write/

---

Field Guide

# Dual write and data migration

Moving the code is the easy half. The data has to move while both systems are live, and at every moment exactly one of them has to be right.

![Slide: the application writes to both Gen 1 data and Gen 2 data, with a backfill of old rows and a verify step.](https://gen2.dev/img/dual-write.png)

Requests can be routed to Gen 2 in an afternoon. Data cannot. The old store holds years of records, it is being written to right now, and both systems need to agree about it for as long as both are live.

The usual answer is dual write: for a period, every change goes to both stores. It is the right shape and it is easy to get wrong. This page covers the pattern, the way it fails, and the variants that fail less.

## The pattern

In 2017 Jacqueline Xu described how Stripe moved hundreds of millions of subscription records to a new table while the system stayed in use. She called it a common four-step dual writing pattern:

1.  "Dual writing to the existing and new tables to keep them in sync."
2.  "Changing all read paths in our codebase to read from the new table."
3.  "Changing all write paths in our codebase to only write to the new table."
4.  "Removing old data that relies on the outdated data model."

Spelled out for a Gen 1 to Gen 2 migration, with the steps that are implied:

1.  **Start writing to both.** Every new change goes to the old store and the new one. The old store is still the source of truth.
2.  **Backfill.** Copy the records that existed before dual writing began. Stripe did the search for records to copy offline, against database snapshots, so that production carried no extra load, and then ran the same job again to confirm nothing was missing.
3.  **Verify.** Compare the two stores until they hold the same information. Compare continuously, not once.
4.  **Switch reads.** Read from the new store, in stages, checking each read path against the old one. Stripe used GitHub's Scientist for this, the tool described on the [parallel run](https://gen2.dev/guide/parallel-run/) page.
5.  **Reverse the writes.** The new store becomes the source of truth. Stripe's wording: "write data to the new store and then archive it in the old store." The old store is now the copy, and it is still a way back.
6.  **Stop writing to the old store.**
7.  **Remove the old data** and the code that depended on it.

This is the data version of what Danilo Sato calls parallel change, or expand and contract: add the new form beside the old, migrate, then remove the old. His warning applies in full. If the contract phase is skipped, "you might end up in a worse state than you started."

## Why naive dual writes go inconsistent

The naive version is two lines of application code: write to A, then write to B. It has three ways to fail.

**No shared transaction.** If the two stores cannot commit together, one write can succeed and the other fail. Gunnar Morling describes the same problem for a service that writes to its database and then publishes to Kafka: "we cannot have one shared transaction that would span the service's database as well as Apache Kafka." The process can crash between the two writes, or the second store can be unreachable. Either way the stores now disagree and nothing has recorded that they do.

**Ordering.** Two requests update the same record at nearly the same time. Store A receives them in one order and store B in the other. Both writes succeeded on both sides, no error was raised, and the stores hold different values.

**Writers you did not find.** The nightly batch job. The admin script. The other team's integration that writes straight to the table. Any write path that was not changed writes to one store only.

None of these produces an error at the moment it happens. The stores drift apart quietly, and the difference is found weeks later by a customer or a finance report.

## The safer variants

The common idea is to stop the application making two independent writes. One store takes the write. The other is derived from it.

Approach

How the second store gets the change

The price

Naive dual write

The application writes to both

Silent drift on any partial failure

Transactional outbox

The change and a message are saved in one transaction; a relay delivers the message

Messages can arrive twice, so the consumer must be idempotent

Change data capture

A tool reads the source database's transaction log and replays changes

New infrastructure to run; the copy lags slightly

**Transactional outbox.** Chris Richardson's pattern answers the question of how to update a database and send a message atomically. The service stores the message in an outbox table in the same transaction as the business data. A separate process then sends it on. The message is sent if and only if the transaction commits, and in the order the application wrote it. The known drawback is that the relay may publish a message more than once, so the receiving side has to handle duplicates.

**Change data capture.** Instead of the application reporting what it changed, a tool reads the database's own transaction log. Morling's article pairs the two: Debezium tails the log and forwards new outbox rows. For a migration, the appeal is that the log sees every write, including the batch jobs and scripts nobody remembered.

**One source of truth at any moment.** This is a rule, not a tool, and it matters more than the other two. For each piece of data, at each stage, one store is authoritative and the other is a copy. When they disagree, everybody already knows which one wins. The migration is a planned sequence of handovers, and step 5 above is the handover.

## How to verify

-   **Compare continuously.** Run a reconciliation job that walks both stores and reports differences. Counts first, then checksums, then field by field on a sample.
-   **Make the repair job routine.** Differences will appear. A tested job that copies from the source of truth to the copy turns each one into a minor event.
-   **Do not move on with open differences.** Each step in the pattern starts only when the previous one reconciles.

## When not to use it

-   **A planned outage is acceptable.** Stop writes, copy, verify, switch. It is simpler and has no drift.
-   **Gen 2 can read Gen 1's database for now.** Move the code first and the data later.
-   **Change data capture or an outbox is available.** Prefer one source of writes over two.
-   **Nothing can compare the two stores.** Without verification, dual write only creates two answers.

## Where it goes wrong

-   **Staying in dual write.** Two stores, double the writes, and a reconciliation job somebody has to watch. The [running two systems](https://gen2.dev/guide/running-two-systems/) page covers what that costs.
-   **Switching reads before verifying.** Users now see whatever the backfill got wrong.
-   **Dropping the old store too early.** Until writes have been reversed and proven, the old store is the rollback plan.
-   **No owner for mismatches.** A report nobody reads is not verification.

## Checklist

-   The source of truth for each data set is written down for each stage.
-   Every writer is known, including jobs, scripts and other teams.
-   The application does not make two independent writes without a way to detect and repair a partial failure.
-   The backfill can be rerun safely and cannot overwrite newer data.
-   Reconciliation runs continuously and its output has an owner.
-   Reads switch in stages, with the old store still current.
-   There is a date for the last write to the old store, and for deleting it.

Dual write sits underneath most [strangler fig](https://gen2.dev/guide/strangler-fig/) migrations and makes [cutover](https://gen2.dev/guide/cutover/) a non-event when it is done well. The last step, removing the old store, belongs to [killing Gen 1](https://gen2.dev/guide/killing-gen-1/). When reading the [Twitter](https://gen2.dev/cases/twitter-jvm/) and [Digg v4](https://gen2.dev/cases/digg-v4/) case files, ask one question: which store was the source of truth on the day of the switch?

## Sources

1.  [Jacqueline Xu - Online migrations at scale (Stripe, February 2, 2017)](https://stripe.com/blog/online-migrations) - the four-step dual writing pattern, the backfill, and verification
2.  [Gunnar Morling - Reliable Microservices Data Exchange With the Outbox Pattern (Debezium, February 19, 2019)](https://debezium.io/blog/2019/02/19/reliable-microservices-data-exchange-with-the-outbox-pattern/) - why dual writes fail, and the outbox with change data capture
3.  [Chris Richardson - Pattern: Transactional outbox](https://microservices.io/patterns/data/transactional-outbox.html)
4.  [Danilo Sato - Parallel Change (May 13, 2014)](https://martinfowler.com/bliki/ParallelChange.html) - expand, migrate, contract

Last updated November 29, 2026.

## Field Guide

-   [Rewrite vs refactor](https://gen2.dev/guide/rewrite-vs-refactor/)
-   [The 7 Rs](https://gen2.dev/guide/seven-rs/)
-   [Characterization tests](https://gen2.dev/guide/characterization-tests/)
-   [Estimating a rewrite](https://gen2.dev/guide/estimating-a-rewrite/)
-   [Strangler fig pattern](https://gen2.dev/guide/strangler-fig/)
-   [Monolith to microservices](https://gen2.dev/guide/monolith-to-microservices/)
-   [Lift and shift](https://gen2.dev/guide/lift-and-shift/)
-   [Second-system effect](https://gen2.dev/guide/second-system-effect/)
-   [Branch by abstraction](https://gen2.dev/guide/branch-by-abstraction/)
-   [Anti-corruption layer](https://gen2.dev/guide/anti-corruption-layer/)
-   [Parallel run](https://gen2.dev/guide/parallel-run/)
-   [Shadow traffic](https://gen2.dev/guide/shadow-traffic/)
-   [Dual write and data migration](https://gen2.dev/guide/dual-write/)
-   [Expand and contract](https://gen2.dev/guide/expand-and-contract/)
-   [Data migration tools](https://gen2.dev/guide/data-migration-tools/)
-   [The cutover](https://gen2.dev/guide/cutover/)
-   [Feature flags and dark launches](https://gen2.dev/guide/feature-flags-and-dark-launches/)
-   [Big-bang cutover](https://gen2.dev/guide/big-bang-cutover/)
-   [API versioning](https://gen2.dev/guide/api-versioning/)
-   [Running two systems](https://gen2.dev/guide/running-two-systems/)
-   [Killing Gen 1](https://gen2.dev/guide/killing-gen-1/)
-   [Rewriting a mobile app](https://gen2.dev/guide/rewriting-a-mobile-app/)
-   [Selling a rewrite](https://gen2.dev/guide/selling-a-rewrite/)
-   [AI-written code](https://gen2.dev/guide/ai-written-code/)

## Should we rewrite it?

Sixteen questions about your system. A recommendation, and the reasoning behind it.

[Start the questions](https://gen2.dev/should-we-rewrite/)
