Modernization ·Build
Replacing a live system one role at a time
Lessons from production migrations applied in a personal lab
For an owner, the risk in replacing a system is rarely the new technology. It is attrition: users who lose trust after a bad day, a support team overwhelmed in the same week, data that goes missing between two systems, and a parallel run that never ends. The answer is to move one role at a time, with both systems in use and synchronised both ways, so every step can be undone until the old system is closed to that role. I built that move in a lab on public sample data, with what the production migrations I have worked on taught me: a planner's work-order desk moved from a WPF desktop client to a web application while the plant floor stayed on the old one. A change reached the other side in about 110 ms, and a 15-second broker outage lost nothing.
- Problem
- replacing a system people use every day with the least attrition: no lost working day, no lost data, no support queue that cannot keep up, and not years of paying for two systems.
- Deciding constraint
- for months both systems are in use and write the same records, so every user must be able to keep working and every change must reach the other side.
- Choice
- move one role at a time, paced by support load, with both systems synchronised both ways and a reconciliation report that must show zero unexplained differences.
- Result
- until the old system is closed to a role, each move can be undone. In the lab, an edit crosses in about 110 ms, a 15-second broker outage lost nothing, and 52 automated tests cover eight failure cases.
Changes grouped by when they were written; nothing was lost or duplicated.
The lab is public, with its tests and the command behind each of its figures: workorder-desk-modernization.
When this applies to you
If one group of users can move first and the rest must stay on the old system, the main difficulty is not the new UI. It is the same record written from both sides, and what you can show when something fails. The first step is a short assessment of what the old database does without the application's knowledge and what the sync must never lose. That comes before any code.
Modernization → discuss a legacy product
For your technical team How it was diagnosed, the options rejected and why, what went wrong, and the decision record.
Context and scope
The lab moves a plant's work-order desk from a WPF app on SQL Server to a web app on PostgreSQL, while the floor keeps the desktop app. The old system's data is Microsoft's AdventureWorks sample database (MIT licensed; its people are fictional). The role that moves is the planner's work-order desk: create an order, release it, record routing progress per work center, record scrap, close it. The desktop client is deliberately simple: three screens, SQL in code-behind, stored procedures.
In scope: moving one role while both systems write the same data; what fails and how it shows. Out of scope: choosing the target stack, the screens, and moving every role (the lab moves one). Assumption: the old database can be given triggers and two tables. If it cannot, making it report its changes is the first job.
Reading the schema before writing any sync code found the risks that determine the design:
| What the sample has | Why it matters |
|---|---|
WorkOrder has no status column and no person column |
The lifecycle has to be derived (no routing rows = created, routing rows = released, any actual start = in progress, end date = closed), and "who did it" needs a table of its own |
WorkOrderRouting has a composite key (order, product, operation sequence) |
The new side needs its own key and a mapping that rebuilds the old one |
WorkOrderID is an identity value, assigned by the old database |
An order created on the web has no legacy id until the old side accepts it |
| Triggers write history rows behind the application and roll back the caller's transaction on error | Any writer, the sync included, runs them; a failure arrives as error 3609 ("the transaction ended in the trigger"), not as the real error |
ModifiedDate is a datetime (about 3 ms steps) set by whoever writes |
It cannot order changes |
| No routing template exists | "Release" has to copy the operations of the product's most recent earlier order |
Drivers
Constraints
- The floor keeps the desktop app for the whole move, and any writer, including a script, changes the old database.
- Each user should work in one system at a time, and support should never have to absorb everyone's surprises on the same day.
- Running both systems costs money every month, so the parallel period has a budget.
Scenarios
- When the planner edits an order, the change reaches the old database and the old database's own answer comes back. Measured by the round trip: 215 ms median, 320 ms p95.
- When the broker stops for 15 seconds under load, nothing is lost or duplicated and lag returns to normal. Measured by lag per window and a reconciliation report with zero unexplained differences.
- When both sides change the same order, the rule for that kind of field decides and the conflict is logged on the side that resolved it. Measured by the conflict tests and logs.
- When a message keeps failing, it is retried, then parked in a dead-letter queue, and the reconciliation report names it. Measured by the report's dead-lettered class and the dead-letter list.
- When the move is on track, fewer users depend on the old system each week. Measured by that count.
How people move: options
The first decision is about people, not code.
| Option | What a user works in | When the risk arises | Who one failure reaches | Fallback | Sync |
|---|---|---|---|---|---|
| Big bang | The new system, all at once | One day | Everyone | No: data entered since is lost | None |
| By feature | Both, until the end | Spread over weeks | Everyone who uses that feature | Per feature | Wherever data is shared |
| By role | One, at a time | Spread over weeks | One role at one customer | Per role | Both ways when both sides change the same data |
The more a move is spread over time, the fewer people any single failure reaches. That trade-off is what justifies a gradual move, and its cost is a sync and months of running two systems.
How changes cross: options
With both systems writing the same orders, the second decision is how a change on one side reaches the other.
| Decision | Chosen | Rejected, and why |
|---|---|---|
| How changes leave the old database | Triggers on the work-order tables write an outbox row in the caller's transaction | Dual writes from application code: no atomicity between the database and the broker. Outbox written inside our own procedures: misses every other writer. Polling ModifiedDate: not an ordering key. Change data capture with Debezium: needs SQL Agent and Kafka Connect, and Kafka is more than this volume needs |
| What a message carries | A whole-order snapshot with a per-order version (scrap in commands travels as a delta) | Field-level deltas for everything: duplicates and reordering then need per-field logic. A snapshot makes duplicate, reordered and overlapping messages safe by construction |
| Broker | RabbitMQ behind a small interface | Kafka: more than the volume needs. Azure Service Bus: the interface is what would let it replace RabbitMQ |
| Framework | None | The outbox, retries and idempotency are the subject; a framework would hide them |
| Identity of the actor | An employee id only, never a name | Names are not needed to apply a change, and they make erasure harder |
Also left out: an identity-provider container, chaos tooling (stopping a container is enough) and Kubernetes. Here, none of them is needed.
The decision and the rules it rests on
Every writer is captured. The triggers fire for the desktop app, for the sync's own consumer and for a script alike. The broker is never on the write path: a change and its outbox row commit together, and the relay publishes later.
Idempotent on both sides. Each consumer records the message id in the same transaction as its effect, and a version on every snapshot stops an older change overwriting a newer one.
A change knows where it came from, and is never sent back as a change. The consumer marks its
session before calling the procedure, so the trigger stamps the row origin = new. The relay sends
that row back as a confirmation: the new side applies it as state and writes nothing outbound.
One command out, one confirmation back, and the exchange ends.
Optimistic, then corrected. The planner sees an edit at once, and the old database's answer replaces it when the confirmation arrives. A new order stays pending until the old side assigns its id, and cannot be edited meanwhile.
One rule per kind of field, with every conflict logged on the side that resolved it:
| Field group | Who wins | Why |
|---|---|---|
| Quantity, due date | The planner (new side) | The moved desk owns planning. The command carries the values the planner saw; if the old side holds something else, it is logged and overwritten |
| Scrap | Nobody: deltas add up | Scrap entered on both sides is a sum, not a conflict. An unconfirmed scrap can be hidden for a moment by a snapshot already in flight; protecting it would risk double counting |
| Routing actuals | The old side | The floor records them; the web has no endpoint that writes them |
| Editing a closed order | The old side | The command is refused and logged, and the current state is sent back |
How each rule is enforced, and where each conflict is logged: the lab's conflict policy.
The planner's edit, end to end:
How the move is made, and how it is undone
The sequence that has held in production migrations I have worked on:
- Map who uses what, where data is written, and what cannot stop.
- Build the new system beside the old one and start the sync one way, old to new.
- Move the simplest role first, with the sync both ways, and rehearse falling back before real work depends on it.
- Move one role at a time within each customer, and keep several customers at different stages, so support load stays level and the cost of running both systems keeps falling.
- Once a role is stable on the new system, close the old one to that role.
- Make the old system read-only, then switch it off on the date set on day one.
Until a role's old access is closed, moving it back is a routing change: the sync has kept both sides current. I have had to do this when features for a role turned out to be badly built in the new system; its users went back to the old one and returned once the features were fixed, with nothing to re-enter. The point of no return is the switch-off, so that is the step to rehearse.
What the lab shows of this: steps 2 and 3 for one role. The sync went one way first, then both ways, with the floor still on the desktop app. The precondition for falling back holds, since the reconciliation report shows both sides equal. What it does not show: the planner actually moved back to the desktop app, or more than one role.
How the decision is kept true
A reconciliation report compares every field of every order on both sides and classes each difference as in flight, dead-lettered (needs a person) or unexplained. It exits non-zero if anything is unexplained, so it is the acceptance test for each move. It also lists every message in the dead-letter queues, whether or not its order still differs.
52 automated tests run against real SQL Server, PostgreSQL and RabbitMQ in containers and cover eight failure cases: duplicate delivery on each side, out-of-order delivery (25 random shuffles of a full order history, each message delivered one to three times, always ending equal), both sides editing the same order, the broker unavailable, a consumer stopping abruptly between commit and acknowledgement, a poison message, a trigger rollback, and a desktop user mid-migration.
Traces and two gauges. One trace follows a change from the client call through the SQL Server outbox, the broker and the consumer into PostgreSQL (about 30 spans for a planner round trip). SQL Server emits no spans, so the outbox row carries the trace context. The gauge that shows an outage as it happens is the age of the oldest unpublished outbox row, not lag.
Everything ran on one shared machine, one run per scenario, with lag read from the databases: the outbox row's creation time to the consumer's committed record. 203 edits went through the stored procedures, as the desktop app calls them, and 99 through the planner API.
| Path (relay polls every 200 ms) | p50 | p95 | p99 | max |
|---|---|---|---|---|
| old outbox → PostgreSQL | 111 ms | 196 ms | 208 ms | 216 ms |
| new outbox → SQL Server | 102 ms | 195 ms | 241 ms | 241 ms |
| planner edit → confirmation back | 215 ms | 320 ms | 383 ms | 383 ms |
With a 50 ms poll the same load gave 34 / 58 / 60 / 79 ms (old → new) and 66 / 106 / 147 / 147 ms for the round trip. The lag floor is the poll interval: the idle sleep is the longest bar in every trace. That is the price of an outbox: latency against idle queries.
The broker was stopped for 15 seconds while 5 edits a second continued. RabbitMQ then needed several seconds to boot, and that is in the figures. Nothing was lost or duplicated.
| Change written at | Count | p95 lag |
|---|---|---|
| before the outage | 46 | 0.2 s |
| 15–20 s | 22 | 21.9 s |
| 20–25 s | 12 | 17.0 s |
| 25–30 s | 19 | 12.4 s |
| 30–35 s | 14 | 7.0 s |
| 35–40 s | 17 | 2.5 s |
| 40 s onward | 73 | 0.2 s |
While the broker was unavailable, the oldest unpublished row climbed to 20 seconds and the backlog to 69 rows; then both fell back to zero.
Where it went wrong, and the risks that remain
The sample cannot change an order's quantity. Microsoft's update trigger inserts a history row without a NOT NULL cost column, so any quantity change fails, and because the trigger rolls back and swallows the error the caller sees only 3609. The desktop app could not do it either. I found it when the first quantity change ended up in the dead-letter queue, patched the trigger in a script labelled as a lab patch, and kept the unpatched behaviour as a failure test. A legacy database behaves in ways nobody documented, and the first live write through a new path reveals them.
I tried suppressing echoes, and abandoned it. The plan was to drop changes that came from the other side. That loses the planner's edit when an old-side snapshot already in flight overwrites it. The fix was to send the result back as a confirmation that is state, never a command: applying it writes nothing outbound, so it cannot loop. Production work taught me the same rule: where an event-driven sync between two live systems could update itself back and forth at a critical step, only the new system was allowed to trigger it. One writer per step, wherever a loop is possible.
Tests that proved nothing. Planner commands in two scenario tests were being refused, because a lookup table was empty in the test database, and the tests ignored the answer. Everything still passed. The harness now asserts that every planner command is accepted. I also introduced deliberate errors into the code to confirm that the tests fail. They did, except one. The test showing that the origin marker does not leak through a pooled connection passed with my clearing code disabled, because SQL Server's own connection reset already clears it. So the test checks the behaviour, and the code stays as a guard.
A report that raised false alarms. Under load the reconciliation report once flagged an order that was only mid-flight: it reads the two databases one after the other, and a change arrived between the two reads. It now reads again two seconds later and reports only what is still unexplained. In 368 reports under 10 edits a second, 9 had such an order and none ended with an unexplained row.
A dashboard that could not show the outage. The first dashboard drew a 20-second outage as a 3-second rise, because the datasource's minimum step was 60 seconds. The raw series were right and the panels were not. I had checked the queries, never the picture.
Users with two doors use both. In production, a role that could still reach the old system kept using it alongside the new one, and nobody was sure which one was right. The fix was to close the old system to that role. Fallback is a safety net with a cost, so it is closed as soon as the role is stable (step 5).
| Risk | Likelihood | Impact | Mitigation | Owner |
|---|---|---|---|---|
| The same record changes on both sides before the sync runs | High | Lost or contradictory data | One rule per kind of field; every conflict logged | Tech lead |
| A retried message is applied twice, or out of order | Medium | Duplicates, an old change over a new one | Inbox row in the same transaction; a version on every snapshot | Developer |
| A change loops between the two systems | Medium | Endless updates, load, flipping values | Origin on every change; a confirmation is state, never a command; one writer per step | Tech lead |
| The dead-letter queue is never read | High | Missing data found by a customer | A named daily owner; an alert on the oldest row's age; dead letters listed in the report | Support lead |
| Undocumented behaviour is not reproduced | High | Wrong results after a role moves | Run the first live writes through the new path early; compare both sides before each move | Tech lead |
| A moved role keeps using the old system | Medium | Double entry, doubt over which system is right | Close the old system to the role once it is stable | Product owner |
| Two systems run too long | Medium | Months of paying for and supporting both | Switch-off date on day one; several customers in progress at once | Sponsor |
What I would do differently: add the dead-letter view and the oldest-row-age gauge on day one, not after the first outage. Decide the conflict rule per field before writing the first consumer. Run the desktop client on Windows before building the second half of the sync, not after.
Operations, cost and scale boundary
To run: the dead-letter queue, daily; the reconciliation report, on a timer (every five minutes in the lab) and before every move; the oldest-row-age gauge, alerted. To learn: the old database's behaviour, which is a larger job than learning the new system. To maintain: the old database gains triggers and two tables, kept until switch-off.
Cost: every month of parallel running is a month of paying for and supporting both systems. The pace is a trade between the two. Going faster removes the duplicated infrastructure sooner, but going slower keeps support load level. Moving roles across several customers at once is how both can be held.
Tested range: one machine, up to 10 edits a second, one moved role. Where the choice flips: when both sides write the same fields heavily, conflicts dominate and no rule per field remains reliable. Then give each step one writer, as at the critical step above, or put the two systems behind one write path. At volumes far beyond this, change data capture and a log-based broker justify their cost. That is reasoning, not something the lab measured.
On a different stack
The lab is .NET and SQL Server. The outbox in the same transaction, the inbox, versions, the rule per field and the reconciliation report carry over to any stack. What changes is how the old database reports its changes, and that decides the sync's shape, so it is the first thing to examine in yours.
Modernization → discuss a legacy product
Built on Microsoft's AdventureWorks sample database (MIT licence).