← Writing

Modernizing a legacy monolith, part 3: Move order notifications without sending duplicates

On this page

When order confirmation emails move to a new worker, the old queue still contains emails to send. Some jobs have yet to start. Others reached the point where the worker called the email provider, but the worker never got a response. Hand both groups to the new worker as unsent and a customer may receive the same confirmation twice.

In this fictional shop, start with one notification type, the order confirmation email, and internal test accounts. Create new send jobs from the order events verified in part 2. Reconcile the old queue and hand over only jobs known to be unsent. Move SMS and cancellation notices the same way once you can check the send status for each channel.

1. Identify the send operation

The legacy batch and the new event consumer can generate different message IDs for the same order. Comparing IDs alone makes both jobs look new. Give both paths a shared business key. For one confirmation email per order, orderId + ORDER_CONFIRMATION + EMAIL is enough. If each partial cancellation triggers a notice, include the cancellation ID as well as the order ID.

Use the same key to reconcile old jobs with new ones. The new consumer registers jobs in a send ledger with a uniqueness constraint on that key. If the key already exists, inspect the existing record. Log an operator’s deliberate resend as a separate send so it can be distinguished from a normal retry. Use the outbox event ID to track duplicate event delivery and the send business key to prevent the two paths from creating the same customer notification twice. AWS’s Transactional Outbox pattern

2. Reconcile requests with no response first

Record the business key, attempt time, send path, provider message ID, and last confirmed status in the ledger. Distinguish these three cases.

Send record Action
Provider call never started The new worker can send after handover.
Provider acceptance confirmed Exclude it from the new worker’s send jobs. Track delivery through provider records or callbacks.
Response lost after the call Keep it out of automatic retries. Reconcile acceptance through a request key or message lookup supported by the provider. If acceptance cannot be determined, hold the job for an operator decision.

If the provider supports idempotency keys, check their validity period and scope, and reuse the same key for the same request. If the provider lacks support or its records do not show the request, a local database uniqueness constraint alone cannot establish that the external send happened once. Record provider acceptance and delivery to the recipient separately. Twilio, for example, reports send and delivery as distinct messaging statuses. Twilio’s outbound message status callbacks

The last confirmed send status determines whether to hand over, track delivery, or hold for reconciliation.

This pseudocode classifies send records. It leaves out provider calls and the check for the new worker’s sending authority.

classify(lastConfirmedStatus):
    if lastConfirmedStatus == NOT_CALLED:
        return HANDOVER_CANDIDATE
    if lastConfirmedStatus == ACCEPTED:
        return TRACK_DELIVERY
    return HOLD_FOR_RECONCILIATION

3. Hand over only the scope where the old worker has stopped

  1. Compare both paths without sending from the new one. The new consumer calculates the recipient, notification type, and whether to send. It writes the result only to a verification log. Compare it with the legacy result, including cases where eligibility changes at the last moment, such as just before cancellation.
  2. Fix the scope of the handover. Record the accounts, notification type, and channel. Stop the old path from claiming new send jobs in that scope and check for runs that already claimed jobs or calls that are still scheduled. Keep newly arriving jobs queued during this time.
  3. Classify the remaining records. Reconcile the old queue, in-flight jobs, and provider acceptance records by business key. Separate jobs confirmed unsent from jobs pending verification. Preserve their business keys when moving queued jobs.
  4. Enable the new path. Confirm that the old worker can no longer call the provider in scope, then give sending authority to the new worker. Verify acceptance and delivery on internal test accounts before expanding to the next group of accounts.

A recorded setting change alone cannot establish that handover is complete. Work claimed before the change may still run later. At handover, confirm that old runs have finished or use a common path that checks current sending authority immediately before calling the provider. Keep all requests already sent to the provider in reconciliation, even if responses are late.

Cutover day runbook

Signal Immediate action When to resume
The same order confirmation email is accepted twice Stop new sends in the affected scope. Determine the impact using the business key and provider message ID. The duplicate path is blocked and already accepted requests are out of the retry set.
Provider responses or callbacks are delayed Exclude unknown acceptance from automatic retries. Match send attempts with late callbacks. Accepted, unaccepted, and pending cases are distinguished. Late callbacks cannot incorrectly overwrite a settled status.
The queue is not shrinking Stop expansion. Check the age of the oldest job, provider errors, and retry volume. Resolve the cause, then resume with a small batch. The oldest job’s wait is decreasing and notifications for new orders are handled within the agreed time.
A rollback to the legacy path is required Stop new sends by the new worker and check in-flight calls. Exclude requests accepted through the new path and those pending verification from legacy retries. Hand back only jobs confirmed unsent. Only the old worker sends in scope, and representative orders are handled without repeat sends.

Before cutover, simulate provider acceptance followed by the worker stopping before it receives the response. If that one job is still excluded from automatic resends after rollback, you have verified that both workers use the same send history when making decisions.

The next part uses the same order events to aggregate statistics in parallel. Before comparing order counts, check whether the old and new reports include the same transactions in the same period when partial refunds occur or at month-end boundaries.

Previous — Legacy modernization, part 2: Add an outbox to the order transaction

Next — Legacy modernization, part 4: Run order reports in parallel before switching over

Discussion

View on GitHub ↗