How to Migrate a Legacy Order System to Event-Driven Architecture
On this page
When adding events to a live order system, start with the order list in the admin console. Keep order acceptance and confirmation in the legacy system and record an event when an order changes. A new consumer can build the admin list from those events so you can check for gaps, duplicates, and delays without changing how orders or payments are processed.
The previous article distinguished order acceptance from confirmation and looked at what must finish before the API responds. Here we keep the existing order API contract and work through when to move reads, notifications, and writes. The order system is fictional, and the criteria below are for planning the migration. Event-driven architecture and what happens after an order arrives
1. Write down the current order contract
What does the customer expect when POST /orders returns success? If the existing API returns an order with inventory reserved and payment authorized, the same success response must keep that meaning after you add events. To accept an order first and take payment later, first update the customer interface and API contract to explain what PENDING means and what happens on failure.
The example uses three migration criteria.
| Operation | Behavior to preserve during migration |
|---|---|
| Order confirmation | The existing order API responds after checking the inventory and payment results. |
| Admin order list | The list can lag briefly. If the lag exceeds the allowed window, switch back to the legacy read path. |
| Order notifications | Send a given notification once per order. Apply the same rule during retries and handoffs. |
With these boundaries in place, you can add event recording and consumption within the same application and move each function once it has been verified. Microsoft Azure’s Strangler Fig pattern
2. Add an outbox to the existing order transaction
When the legacy order code updates orders, write an event to outbox in the same database transaction. For example, when an order commits as CONFIRMED, leave an OrderConfirmed event ready to publish. If the transaction rolls back, neither the order change nor the outbox row takes effect. A separate publisher reads committed rows and sends them to the broker. AWS transactional outbox pattern
Include eventId, orderId, orderVersion, eventType, and schemaVersion in the event. Use eventId to spot redelivery and orderVersion to tell how far the read model has caught up for this order. Review the order update code and batch jobs to make sure every path that changes order state also writes to the outbox.
If the publisher stops right after sending to the broker, it may send the same message again because it never marked the row as published. Consumers must expect duplicates. The example’s read consumer commits the processed eventId record and read table update in the same transaction, then acknowledges the message to the broker. If the consumer stops before committing, it processes the message again. If the message returns after the commit, the processed record lets it skip the duplicate. AWS transactional outbox considerations for duplicates and ordering
The diagram and pseudocode show the read consumer’s flow. The input event contains all order state needed by the admin list. A unique constraint on the event ID protects the processed record, and the read table compares and updates each order’s version atomically. A failed transaction rolls back before any acknowledgement is sent.
consume(event):
transaction:
if insert_processed_if_absent(event.eventId):
upsert_admin_order_if_newer(
event.orderId, event.orderVersion, event.adminState
)
acknowledge(event)
3. Compare results side by side before switching reads
Keep the admin screen on the legacy database while you populate a separate read table from an order snapshot and let the consumer apply subsequent changes. Capture the snapshot’s reference point and the starting position for changes together so the backfill covers orders that arrive while it runs.
In this example, each event carries the full order state needed by the list. The consumer applies only a higher orderVersion. After it applies delivered version 4, a late shipped version 3 leaves the displayed state intact. If events carry only deltas, determine whether you need the skipped versions and define gap recovery or replay rules separately.
Before switching, compare status and amount for the same order at the same version. If the read model is behind, record it as projection lag and investigate any case that persists beyond the allowed window. Start with internal operators on the new list and compare filtering, sorting, and pagination. If there is a problem, turn off the read flag and return to the legacy list. With the same write owner at this stage, newly stored orders can stay where they are.
4. Hand off notification delivery one route at a time
Once the read results agree consistently, move on to order notifications. First let the new consumer calculate and record what it would send. The legacy code still sends the notifications. Check the new delivery intents against past sends to find missing or duplicate intents.
Assign each order a fixed notification route at cutover. For example, the new consumer takes only orders accepted after the cutover flag goes on, and the legacy route finishes orders accepted before that point. If both routes can send the same notification, use a shared business key such as orderId + notificationType to control duplicates. The message’s eventId alone cannot identify a notification the legacy route has already sent.
If the email request times out, the provider may have accepted it even though the application sees a failure. Retry with the same idempotency key if the provider supports it, or check the delivery result to decide whether to retry. If neither option is available, stop automatic resends and leave the item for manual reconciliation. On rollback, route new orders to the legacy sender. Let notifications already queued finish on their assigned route, or reconcile their delivery status before handing them off.
5. Transfer write ownership after checking what you can roll back
If the separate admin list and notifications solve the operational problem, order writes can stay in the legacy system. If inventory reservations or payment writes also need to move, start with a scope where one side can own the writes, such as a function or a tenant. Draw the cutover boundary around the actual inventory or payment state being updated. If two tenants update the same inventory, routing by tenant alone leaves them sharing write ownership.
You can route back while the legacy code can read the new state and you can settle ownership of in-flight writes. Once the new system records state the old code cannot interpret or authorizes an external payment, routing back by itself cannot restore the system. Resolve in-flight work, reconcile it with the source of record, and use business compensation such as releasing an inventory reservation or cancelling a payment when needed. Microsoft Azure’s incremental replacement pattern
Failures to reproduce before cutover
| How to trigger the failure | Pass criteria |
|---|---|
| Roll back the order transaction | Neither the order change nor the event to publish takes effect. |
| Stop the publisher just after it sends to the broker | Sending resumes from rows without a published marker. Redelivery leaves the read model free of duplicate changes. |
| Send an older version after the latest one | The read view keeps the newer state. |
| Make the email request time out | Automatically retry only when the same idempotency key or a delivery lookup can prevent a duplicate. |
| Stop the new consumer just after cutover | New orders can safely return to the old route, and in-flight orders and notifications remain traceable. |
Keep the first deployment small. Add the outbox to the existing order transaction and build one internal admin read model for comparison. Check that it catches up without gaps, prevents duplicates, and lets you switch reads back before deciding what to move next.
Discussion