Close the gap between business state and send intent
Suppose an order is stored in a database and then a notification is sent. A process stopping between those actions leaves the order without a durable send intention. Reversing the order can notify a change that later fails to commit. Sequential writes to separate systems leave a partial-success gap. A transactional outbox stores the business update and its message intention in one local database transaction, making unsent work recoverable.
Prerequisites are local transactions, idempotent requests and duplicate delivery. The atomic boundary covers the order and outbox row in the same database, not the database and remote destination together. A separate relay sends pending rows. The first contract is that a committed business update retains its matching send intention. This is different from promising one delivery across every system. The companion inbox article addresses the receiving side rather than treating sender persistence as sufficient for end-to-end uniqueness.
Commit two related records locally
For order-7, update business state and insert event-7 into the outbox between one BEGIN and COMMIT. Failure of either write rolls back both. Give the event a stable ID, order ID, type, required payload and, where needed, an aggregate sequence number. Reconstructing an old event solely from the order's latest state can lose the information true when it occurred. Specify sufficient payload while controlling unnecessary duplication of sensitive data.
A lost HTTP response still requires idempotent business-request handling. A unique event ID does not prevent two distinct business acceptances of the same retry from producing two legitimate-looking events. Request identity and payload matching are a separate responsibility from preserving notification intent. Do not hold the local transaction open across a slow remote send as a substitute for this separation. Commit the local state, then let delivery proceed outside that lock-holding boundary.
Trace three crash positions
Stopping before commit leaves neither local record committed under this model. Stopping after commit but before send leaves both records available for relay recovery. Stopping after the destination applies a message but before the relay saves its sent marker leaves a pending row that can be sent again. The third history explains why an outbox alone does not remove duplicates. A missing destination response also leaves the result unknown rather than proving non-application.
Marking sent before sending trades that duplicate window for a loss window: the relay can stop after the marker but before delivery. Reordering two separate writes cannot remove both possibilities. State the delivery contract and carry the stable event ID into receiver-side deduplication. Its once-only effect applies within the receiver's own atomic boundary. A local marker does not automatically make a separately invoked email or payment effect occur exactly once.
| Crash position | Durable local state | Recovery |
|---|---|---|
| Before commit | Neither committed | Restart request handling |
| After commit, before send | Order and pending event | Send event |
| After application, before marker | Order and pending event | Duplicate is possible |
Specify worker ownership and ordering separately
Multiple relays need a claim mechanism such as locking or expiring ownership, including recovery when an owner stops. A dead worker must not retain a row forever. Exclusive claiming still does not eliminate resending after destination application but before local marking. Worker coordination and delivery deduplication solve different problems. Implement the claim protocol against the chosen database's documented transaction and locking behavior.
Creation and cancellation can arrive reversed if separate relays deliver them independently. Define ordering at the required unit, such as an individual order, with sequence numbers and explicit treatment of delayed older events. Global ordering of every order may be unnecessary. Reusing one event ID for retries is distinct from ordering different events. A timestamp embedded in an ID does not itself prove causal or commit order. Keep those guarantees explicit when selecting partitioning, sender scheduling and receiver behavior.
Observe pending work and resolve poison events
Monitor oldest pending age, attempts, delivery latency, invalid payloads and receiver duplicate counts as well as queue length. An accepted order whose notification remains stuck for hours has not completed the user's expected flow. Specify isolation, repair and replay for an event that cannot be processed, instead of retrying it forever and blocking later work. Silently deleting it abandons the notification contract; retain an explicit business resolution.
Some tasks need no outbox: a permitted best-effort notification or work entirely within one database can justify a simpler design. Existing change-data-capture mechanisms are another candidate, but still require verified commit positions, retention, resumption and ordering. Evaluate what can be recovered from a committed update rather than adopting a pattern name alone. The restart lab verifies crash boundaries using disposable SQLite; it does not certify PostgreSQL or a production broker's performance and durability.
Adding another destination changes the progress contract
If the same event must reach two destinations, completing one does not complete the other. A single sent Boolean may no longer express the obligation. Define per-destination progress or an explicit handoff to a delivery system responsible for fan-out. A successful one-destination test does not verify the enlarged contract. The durable intention must preserve enough information to identify which work remains, including recovery when the relay stops between destinations. This example extends the same principle rather than demanding a particular schema: describe the business delivery obligation, then select records that can represent unfinished parts without confusing them with completed ones.
Solve the post-application crash exercise
The destination decrements inventory, then the relay stops without a sent marker. Recovery may resend the same event. The receiver therefore needs its event ID and local business effect handled atomically. Saying outbox prevents duplicates is incorrect. Marking sent earlier creates a loss window instead. Return to the crash table and explain which property each proposed ordering actually changes.
For a second exercise, generate every message later from the order's latest row. If both creation and cancellation then read only cancelled state, distinct historical facts are lost. Save event-time payload or sufficient reconstructible history. The lab asserts sender business state, pending rows, receiver IDs and effect counts across stops. Following those states establishes the intended result: durable unfinished delivery that can resume, with duplicates handled at the appropriate boundary.