Tutorial
Handle Out-of-Order Events
The Event That Arrives Before Its Cause
An automation receives a shipment notification before the order creation event. It receives a status change before the record exists. It receives an update carrying an older version than the one it has already applied.
Out of order delivery is normal, because most event sources do not guarantee ordering. Handling it is a design decision that is easy to make early and expensive to retrofit.
Four Patterns
Ignore older versions. Each event carries a version or timestamp; anything not newer than the current state is discarded.
Wait for a dependency. The event references a record that does not exist yet; park it and retry after a delay.
Buffer and reorder. Hold events for a short window and apply them in order. Only viable when the window is small.
Make the operation commutative. Design the state change so the order does not matter, such as setting an absolute value rather than incrementing.
The fourth is the strongest and the least used, because it requires thinking about the state rather than the event.
Version Checks
The workhorse technique.
Three requirements. Every event carries a version or a monotonic timestamp. The stored state carries the version it was derived from. And an event with a version not greater than the stored one is discarded and logged.
The log matters: a stream of discarded events is a signal that something upstream is replaying, which may be a defect.
Waiting for Dependencies
When an event references a record that has not arrived.
Three implementations.
A short delay and retry, up to a limit. Simple and adequate for events that arrive seconds apart.
A pending queue, keyed by the missing identifier, drained when the record appears.
A reconciliation sweep, which processes anything stuck in a pending state older than a threshold.
The third is the safety net and it should exist regardless, because the first two both fail eventually.
Commutativity
The design change worth making where it is possible.
An increment is order dependent: add five then add three is not the same as three then five, if a read happens between. A set is order independent, provided the versions are compared.
Where the business operation is naturally a state transition rather than a delta, storing the target state removes the ordering problem entirely.
The Status Change Case
The most common real instance: a record moves from pending to shipped to delivered, and the events arrive in a different order.
Three rules. Define the allowed transitions and reject anything that is not one. Store the state with the event that produced it. And treat a delivery for a record that is still pending as a signal to fetch the current state rather than to apply the event.
That last rule is the practical one: when an event is inconsistent with the state, trust the system of record over the event.
Time Skew
A related problem that looks like ordering.
Clocks differ between systems, so an event's timestamp may be earlier than an event that arrived before it. Two practices: use the source's sequence number rather than a timestamp where available, and allow a small tolerance window when comparing timestamps.
Detecting the Problem
Three signals worth monitoring.
Discarded events, which indicate replays or ordering issues.
Records in a pending state beyond a threshold, which indicate a missing dependency.
State regressions, where a record moves backwards. These should be impossible by design and are the clearest signal of a defect.
Testing
Three cases in the fixture.
Deliver the events in reverse order. Deliver a duplicate in the middle. And deliver an event for a record that does not exist, then create the record.
An automation that passes those three handles most of what production will do.
Next step
Read recover from a checkpoint for the resumption side, or contact us to review event handling.
FAQ
Question: Should we sort events before processing?
Only within a bounded window, and only when the window is small. Long buffering delays the work and grows the failure surface.
Question: What if the source has no version numbers?
Use the source's own update timestamp, with a tolerance, and add a reconciliation sweep for anything ambiguous.
Question: How long should a pending event wait?
Minutes for interactive work, hours for back office. Beyond that, escalate rather than wait.