AlgoViz

When events arrive twice and out of order

medium 6 min

A subscription service receives a renewal twice, then receives an older update.

Video lesson · Chapter 1 of 5

A parcel is already shipped

0:00 / 4:12

Yesterday’s subscription update arrives last

1:14Reel

Theory

Read or listen first. About 1 min.

What it is

A successful delivery response tells the sender it can stop retrying that attempt. If the receiver acknowledged while the event existed only in RAM, a crash can erase it permanently. Persist the verified event and its processing state before returning success. Slow downstream work can then run outside the delivery request.

Delivery identity and business revision

Incoming eventX · revision 18Inbox lookupID already storedSubscriptionAlready appliedOne event ID causes one business effect.
Event X at revision 18 arrives again. Its ID already exists in the inbox, so the receiver acknowledges the retry without applying the renewal twice.

Why it exists

The inbox records receipt. A business revision records the state of the subscription. Two copies of one event share a delivery identity, while two different updates can have distinct identities and arrive in reverse business order. The consumer needs a policy for both cases.

View diagram · Subscription updates
Provider → Signature check (raw event); Signature check → Durable inbox (verified); Durable inbox → Consumer (pending event); Consumer → Subscription (version guard)
Advance with an authoritative version or reconcile with current state.
Full size

Words to know

Inbox
Durable storage of received events and their processing status.
Outbox
A durable record of an outgoing action committed with the local state change.

How it works

Step through the idea, or press Play.

Step 1 of 3

Verify and persist. Verify the provider signature against the raw payload. Durably record the event under a unique provider and event ID before acknowledging receipt.

Understand it

Start here

The subscription mailroom

Treat incoming subscription updates like envelopes at a desk. Read both the envelope ID and the revision inside it. One identifies a repeated delivery and the other tells you which state is newer.

The subscription mailroom
The subscription mailroomDeliveryX · rev 18Inboxsave XCurrentrevision 18Apply the renewal

Two different checks handle two failure modes.

Structure

Subscription updates

Subscription updates

Provider → Signature check (raw event); Signature check → Durable inbox (verified); Durable inbox → Consumer (pending event); Consumer → Subscription (version guard)
Advance with an authoritative version or reconcile with current state.

Replay a renewal

Deliver events for revisions 18, 18 and 17 in that order. Processing should leave the subscription at 18 with one renewal effect. Then crash immediately after persisting a new event and before applying it. Recovery must find the pending event.

When delivery is acknowledged

Read the delivery log

DeliveryInbox decisionSubscription revision
Event X carries revision 18Store and apply18
Event X is retriedRecognize duplicate18
Event Y carries revision 17Store receipt, skip stale transition18
Event Z carries revision 19Apply new transition19

Where a crash can split the work

Marking an event done before applying its effect loses the update after a crash. Applying the effect first and marking done later risks duplication. When both live in one database, commit them together. When an effect calls another service, persist an outbox entry and deliver it using a stable downstream key. The receiving service still needs to honor that identity.

When the payload lacks a revision

Transport arrival time tells you when your server received a message. It does not prove when the business event happened. For a current-state projection, use the notification as a signal to fetch authoritative state and reconcile it. For immutable financial movements, preserve individual event identities and reconcile missing movements instead of overwriting history with one snapshot.

Track receipt and revision

  • Two different checks handle two failure modes.

Order by arrival

  • The newest delivery can carry older business state.

Summary

The whole topic in one minute.

Track durable receipt, local application and external side effects separately. Test redelivery and reversed event order as distinct failure modes.

Sources