The Dual Write Pattern, part 2: Pitfalls
What's in a write?
Series: Dual Writes for Database Migrations (2 articles)
- 1The Dual Write Pattern, part 2: Pitfalls This article
In the previous article, The Dual Write Pattern: Will it work for Migrating Relational Databases to NoSQL? , we introduced the dual-write pattern for database migrations.
Where It Gets Complicated
Despite its appeal, the dual write pattern introduces serious complexity that is often underestimated.
Data consistency is a central challenge. Writing atomically to two separate systems is fundamentally difficult. If the source write succeeds and the target write fails (or vice versa), the systems diverge. Without a robust reconciliation mechanism, silent data drift becomes a real risk.
Not all writes are the same. Even if we assume the dual writes will both succeed 100% of the time, there are still problems. Some writes will be very difficult to perform on the target database. Let's break down the challenges by imagining a read-only database migration, and then we will add the various write operations to the mix and explore the implications. We will begin with operations to create, delete, and update a single record.
Insert-only migrations
A read-only database would be easy to migrate. Since no data is changing, the target can be prepared and loaded at your leisure, and cutover is straightforward.
Now let's imagine the simplest kind of database write, a SQL INSERT of a new record into a table. For this scenario, the dual-write pattern shines. The write operation can perform a SQL INSERT and a target database put-item call at the same time. These writes can occur while historical data is also being loaded, as the two write paths will not interfere with each other.
Insert and Delete migrations
Let's extend our sample application and imagine it needs to perform dual-write INSERTs and DELETEs during the migration window. Remember, the target database is in a partially-loaded state throughout this phase. Imagine that we have loaded the first three items into the target table, when the application requests a delete of record one. This will be performed on both source and target tables as expected.
Next, imagine the application needs to delete the fifth item in the table. Since this record has not yet been loaded into the target, there is nothing there to delete. But, a few minutes later the bulk loading job may write record five, unaware that it was supposed to have been deleted. We now have a problem on our hands.

Figure 5: A deleted item remains

Our bulk loading job really needs to keep track of which dual-write delete requests succeeded in the source but found no item in the target. A special table called Do-Not-Load is needed.
We can summarize the algorithm changes needed by both the dual-writers and the bulk loader:
Dual writers
IF DELETE single item:
IF DELETE source item affects 1 row AND DELETE target item affects 0 rows
THEN add item key to a Do-Not-Load table
Bulk Loader
FOR EVERY Item:
Lookup Item key in Do-Not-Load table
IF found: skip loading the item

As you can see, things are getting complicated. Our loading routine itself is turning into an application with business logic and a database table of its own.
So far, we have found a strategy for loading single item INSERTs and DELETEs. Unfortunately, the challenge is only getting harder. We still need to consider a few other kinds of SQL write operations.
SQL Write Operations
Writes based on the key of a single item:
INSERT, DELETE
Writes that may affect some but not all columns of a target item:
UPDATE
Writes affecting multiple items, based on WHERE conditions or subqueries:
INSERT INTO <mytable> SELECT <some row(s)> WHERE <conditions> DELETE WHERE <conditions> UPDATE <mytable> SET attr4 = 'abc', attr7 = 'xyz' WHERE city = <value>
Writes based on the current state
UPDATE <mytable> SET attr5 = attr5 * discount, attr6 = attr6 * 2 WHERE attr6 < 100 DELETE <mytable> WHERE ID IN <SUBQUERY>
Take a moment and consider what each of these SQL statements are doing. Consider how the database is like a finite state machine . Each write operation is a transition that takes the machine from one state to the next state. Because each write operation may have a WHERE clause, writes may be highly dependent on not just the state of the target item, but on the state of ALL items in the database. Therefore, it becomes impossible to try and replay a particular write request against our migration target, since it is only partially full during the bulk loading phase. We simply cannot devise tracking tables like Do-Not-Load that would adequately capture all aspects of how a transition is performed against the state of the state machine. The before image, after image, SET conditions, WHERE conditions can only be evaluated correctly against the complete source database as of the point in time of the request.
Further, we have not yet considered migrations that require major schema changes, such as multiple relational tables being transformed and denormalized into a single NoSQL table. If a customer's phone number is kept just once in the source database, but exists duplicated across thousands of items in the target database, then an UPDATE that changes the phone number will require custom routine to apply correctly against the target. See this discussed in What is a good DynamoDB data model .
Summary
Let's summarize what we learned regarding the Dual Write migration pattern. This pattern can work well for the most basic kinds of database applications: those that simply add new records to the end of a table. Applications that perform SQL UPDATES, bulk SQL write operations, or state machine transitions will require special attention.
A great starting strategy for a migration would be to identify all SQL statements that the source database receives, and classify each one based on difficulty to migrate.
What is your go-to migration strategy? Share your thoughts below!
Series: Dual Writes for Database Migrations (2 articles)
- 1The Dual Write Pattern, part 2: Pitfalls This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article