Automated Rollback Mechanisms
in Database Migration Frameworks

Confidential — Public Healthcare · Clinical Systems

Snapshot
Duration
One release cycle for the gateway; the parallel period ran until the old engine was retired
Scope
One clinical system — schema, stored procedures, and the .NET and Java code in front of it
Role
Architecture and implementation — the gateway, the fallback path and the dual-write design
Roles involved
Solution architect, Java and .NET engineers, DBA, QA, clinical system owners
Deliverables
Gateway design, dual-write mechanism, fallback procedure, schema and procedure migration, per-phase report
NDA
Client name, system name and clinical data withheld. The gateway is described at a structural level; configuration and volumes are as delivered.
0
CDC licences purchased
2
Databases written on every transaction
972
Tables in the gateway proof, at 268 GB
143
Stored procedures behind one access layer

Problem

Migrating a clinical system is not one migration. The schema has to move, the procedures have to move, and the code in front of them has to move — three schedules, three risks, and only one of them can be rolled back cleanly.

The usual answer is a change-data-capture product: buy a licence, let the vendor's tool keep the two databases in step, cut over when the tool reports a zero delta. It works. It also costs a licence, adds a component nobody on the team wrote, and puts the one fact you cannot afford to be unsure about — whether a write reached both databases — behind a support contract.

The requirement here was the opposite: no additional CDC tooling, both databases written, zero data loss, and no interruption to a system that clinicians were using.

Approach

Three decisions.

Put the decision in one place. A gateway in front of both engines, exposing the same operations whichever database is behind them. Application code calls the gateway; the gateway decides where the write goes and where the read comes from. Neither database knows about the other, and the application does not know there are two — which is what makes the whole arrangement reversible.

Write both, and take the second write seriously. Database triggers plus the gateway's own CRUD path keep the two in step, with the mechanism chosen per operation. When the second write fails, the gateway does not guess: it records the failure, and the read path is built so that a partial write becomes visible rather than silent.

Design the failure path first, not last. If the call to the new engine throws, the gateway answers from the old one. If the row cannot be found in the new engine, it looks in the old. If both hold it, they are compared and only a match is returned. This is not an exception handler bolted on at the end; it is the mechanism that made it reasonable to attempt the migration while the system was in use.

Figure 1 — One access layer in front of two engines

One access layer in front of two enginesThe application asks for an operation. Which database answers is not its businessApplication.NET surfacerouted to JavaJava servicescall the gatewayGatewayone set of operationsroute the writeroute the readcompare on doubtfall back on failurePostgreSQLthe target — schema, proceduresand data migrated firstMSSQLthe incumbent — kept currentfor as long as it is neededcallsync: triggers / CRUDThe application never knows there are two databases.That is the whole design: the fact of the migration is kept in one place, so it can be undone in one place.

Figure 2 — The read path, including the part designed to fail

The read path, including the part designed to failEvery read goes to the new engine first. What happens next is the designA read enters the gateway and is answered by the PostgreSQL access module.SQL exceptionthe call itself failsAsk MSSQLthe incumbent answersthe answer is markedas a fallback readansweredand flaggedRow not foundthe module returns nothingAsk botheither engine may holdthe row; whichever hasit answersansweredfrom one engineRow foundthe module returns a rowCompare boththe same row, read fromtwo engines; only amatch is returnedreturnedor raised as a disagreementThe fallback is not an error handler bolted on afterwards. It is the reason the migration could be attempted while the system was in use.Fallback and comparison conditions follow the delivered gateway behaviour.

How it was run

Infrastructure first: a new PostgreSQL estate configured for the workload before any data moved, because a migration that starts by discovering the target is misconfigured has already lost the schedule.

Schema, replication and stored procedures moved through a converter, with analysis attached to each rather than a bulk load followed by hope. The .NET integration surface was routed to Java wherever the data access had to be rewritten, and each phase produced its own report.

The gateway was proven on reads before it was trusted with writes. The proof was not a sample: it enumerated 972 tables and 143 stored procedures across 268 GB, which is the point at which “the design works” stops being an opinion.

During the parallel period both databases were written on every transaction. That is more load than the system would carry in steady state, and it was accepted deliberately — a short period of excess cost in exchange for the ability to compare.

Figure 3 — A write is not finished until both databases have it

A write is not finished until both databases have itTwo writes per transaction, and a failure path that is designed rather than discoveredInsert / update / deleteone operation, from the appGatewaydecides where it goesPostgreSQLthe write of recordMSSQLthe second writetriggeror the gateway's own CRUD path, chosen per operationBoth writes landThe transaction is acknowledged. The twoengines hold the same row, and the readpath has nothing to reconcile.The second write failsThe gateway does not guess. The failure isrecorded, and because the read pathcompares on doubt, the divergence is visible.A partial write that nobody can see is the failure this design is built around. It is also the reason the gateway stayed after the migration finished.Two synchronisation paths were implemented; which one applies is decided per operation.

What we delivered

01
Database gateway
One set of operations in front of two engines, with routing for reads and writes kept in a single place.
02
Dual-write mechanism
Triggers and a CRUD path, applied per operation, so a write lands in both databases.
03
Automatic fallback
Exception, not-found and found-and-compared paths, so a failure degrades the read rather than the service.
04
Schema and procedure migration
Table, view, index, trigger and stored procedure conversion with analysis per object.
05
.NET to Java integration
The routing and rewrite needed to put the new engine behind the existing front end.
06
Per-phase migration report
What moved, what was compared, and what was still outstanding at each phase boundary.

Clinical system, client and data withheld. The design is described at a structural level.

Impact

No CDC licence was bought, and both databases were written for the length of the parallel period — which is the requirement, met by construction rather than by procurement.

Zero data loss was the stated requirement, and the design's answer to it is structural: a transaction is not acknowledged until both writes are accounted for, and a second write that fails is recorded rather than assumed.

The gateway stayed after the migration finished. It is now the system's access layer, which means the next database change is a change behind it rather than a rewrite in front of it — the cost of the approach was paid once and is being amortised by every change that follows.

CONTACT US

We provide a custom made service for just drop us a line.
We would be glad to have feedback from you.