There is a small gap in event-driven systems that can swallow an important fact.
The application updates the database. It then publishes an event. The process dies between those two operations.
The user sees the updated state. Downstream systems never receive the event that says it changed.
Reversing the order creates the opposite lie: the event is published, then the database transaction fails. Consumers act on a state that never committed.
This is the dual-write problem. It appears whenever one logical action must update two systems that do not share a transaction.
After commit is still a failure window
A common improvement is to publish only after the database commit. That avoids announcing rolled-back state, but it does not guarantee publication.
textcommit database -> process dies -> publish never runs
An after-commit hook defines the correct order. It does not make the second operation durable.
That difference matters for notifications, search indexing, analytics, audit streams, and background workflows. The business record can be correct while every dependent view remains stale.
Put the event intent in the same transaction
The transactional outbox pattern stores the business change and the intent to publish an event in one database transaction.
textbegin transaction -> update business state -> insert outbox row commit
If the transaction commits, both records exist. If it rolls back, neither exists.
A separate dispatcher reads unpublished outbox rows and delivers them to the broker or destination.
The outbox row might contain:
textevent id event type and version aggregate id occurred time correlation id minimal payload attempt count next attempt time published time
The payload should contain what consumers need without turning the outbox into an uncontrolled copy of private application state.
The dispatcher is at least once
The dispatcher can publish an event and crash before marking the row complete. On restart it publishes the same event again.
That is expected.
The event needs a stable identity, and consumers need idempotent handling. A consumer can keep an inbox table of processed event IDs or enforce a domain-specific unique constraint around the resulting action.
The goal is not to make delivery magically exactly once. It is to make repetition detectable and harmless.
Retry with a policy, not a tight loop
When a destination is unavailable, immediate repeated attempts can make the outage worse.
I want the dispatcher to record attempts and apply bounded backoff. Errors should be classified where possible:
- Retryable network or dependency failure.
- Non-retryable payload or contract failure.
- Authentication or configuration failure requiring an operator.
- Unknown outcome requiring reconciliation.
After a defined limit, the event should move into an observable dead-letter state rather than disappearing from the active query.
The original outbox row remains evidence of what the application intended to publish.
Commit ownership matters
Outbox code becomes tricky when service methods can participate in a caller-owned transaction.
The method that changes the state should insert the outbox event through the same transaction object. It should not independently commit, and it should not publish before the caller's surrounding work succeeds.
textcaller opens transaction -> service changes aggregate -> service inserts outbox event using caller transaction -> caller performs related work -> caller commits everything
If the inner service commits early, it breaks the caller's atomic boundary. If it defers event creation until later, it recreates the dual-write gap.
Event versions protect consumers
An event is a public contract, even when the public is only another team.
I include an explicit type and version rather than asking consumers to infer shape from whichever fields happen to be present. Additive changes can often remain compatible. Meaning changes deserve a new version.
Consumers also need a policy for unknown event types and versions. Crashing an entire queue because one new event arrived is rarely the right answer.
Monitor the age, not only the count
An outbox with ten fresh rows may be healthy. An outbox with one row stuck for six hours may not be.
Useful signals include:
- Age of the oldest unpublished event.
- Publish latency distribution.
- Attempt count by destination and event type.
- Dead-letter additions.
- Dispatcher heartbeat.
- Difference between inserted and published rates.
A cumulative failure counter should usually be alerted on its increase over a window. Alerting on "counter is greater than zero" keeps the system permanently red after the first historical failure.
Events are part of the write model
I no longer treat publication as something the application does after the real work. If downstream behaviour depends on the event, recording the event intent is part of the real work.
The outbox does not remove every distributed-systems problem. It gives the system a durable place to stand: the business state and the statement that something must be published either commit together or not at all.