The order was saved. The event disappeared.
A transactional outbox closes one failure window, not every one
Save business state and publication intent together. A relay can recover after crashes, but duplicate delivery still needs handling.
An API saves an order to PostgreSQL, then publishes OrderCreated to a broker. If it crashes between those operations, the order exists and the event does not.
Reversing them creates another failure window: an event can escape even though the order transaction later fails. Two successful API calls do not form one atomic transaction.
Prerequisites: durability and idempotent retries.
Save intent in the same database transaction
Instead of publishing inside the request path, write the order and an outbox row together:
A separate relay finds committed pending rows, publishes them, then records delivery progress. The outbox is stored intent, not a second independent write after the order.
If the transaction rolls back neither row exists. If it commits and the process immediately crashes, a relay can still discover the event later.
Where duplicates remain
The relay publishes an event and receives broker confirmation. Before it marks the row sent, it crashes.
On restart, the row still looks pending. Republishing is the conservative choice: assuming delivery merely because a send was attempted could lose an event. A repeated publication must preserve the event_id.
| Crash point | Durable order/outbox | Delivery consequence | |---|---|---| | Before transaction commit | Neither committed | No committed intent to relay | | After commit, before publish | Both committed | Relay can publish later | | After broker confirmation, before sent marker | Both committed; pending marker | Duplicate publication possible |
An outbox closes the lost-intent window. It does not itself promise exactly-once delivery or end-to-end exactly-once effects.
Consumers need their own boundary
A consumer can store a processed-event identifier and its local business change in one transaction, using a suitable uniqueness constraint. Checking an identifier and recording it after the effect leaves another crash window.
For remote effects such as sending a payment request, that local transaction cannot make the provider's operation atomic. Its idempotency contract or another explicit reconciliation design is still required.
What the design costs
Publication is asynchronous, so the order and downstream view are temporarily inconsistent. Relays need retry policy, monitoring for oldest pending events, cleanup and handling for repeatedly failing messages.
Multiple relay workers need safe coordination. Per-aggregate ordering requires deliberate design rather than relying on whichever worker finishes first.
Check yourself
The broker acknowledged an event but the relay did not persist that fact. Can a second delivery be correct behavior? Yes. Reuse the event_id and make the consumer's local handling duplicate-safe.
Draw the three crash points before choosing a broker. The database transaction defines what is saved together; the relay and consumer define what repeated delivery means.