Implement a transactional outbox to save database changes and event intent together
The transactional outbox pattern reduces dual-write inconsistency when one operation must update a database and publish an event to a message broker. Store the business data and the event to publish in the same database transaction, then use a separate relay to send committed outbox entries to the broker. This makes the state change and publish intent atomic, but it does not guarantee exactly-once broker delivery, so consumers must handle duplicates.
The transactional outbox pattern reduces dual-write inconsistency when one operation must update a database and publish an event to a message broker. Store the business data and the event to publish in the same database transaction, then use a separate relay to send committed outbox entries to the broker. This makes the state change and publish intent atomic, but it does not guarantee exactly-once broker delivery, so consumers must handle duplicates.
Why it is needed
If the application sends a message after committing the database, it can crash in between and lose the event. If it sends the message first and the database transaction rolls back, consumers process a state change that was never saved. Without one distributed transaction spanning database and broker, application statement order alone cannot make both writes atomic.
Basic flow
**Write state and event in one transaction.** Change the business row and insert an outbox row. Roll back the whole transaction if either write fails. Include a unique event ID, aggregate or entity ID, event type, schema version, sequence or ordering data, and payload.
**Let the relay read only committed entries.** A polling relay reads unpublished rows in batches; a CDC approach reads the database change log or stream. Choose based on database support, throughput, acceptable delay, operational complexity, and recovery model. Ensure neither approach exposes uncommitted state.
**Update broker acknowledgement and relay state recoverably.** If the relay crashes after the broker accepts an event but before it records publication, it may send the same event again. Deleting the row before publication risks losing it. Define retries and retention, and do not delete before the required acknowledgement.
**Specify the scope that needs ordering.** If event order for one entity matters, design sequence numbers or versions, a stable partition key, and relay ordering. Verify the order survives multiple relay instances and restarts. Global ordering is costly and may not be necessary.
**Make consumers duplicate-safe.** Relay retries or at-least-once broker delivery can deliver an event more than once. Consumers can atomically record processed event IDs with their effects, or make repeated processing of the same ID harmless. Do not assume the outbox alone provides end-to-end exactly-once effects.
**Plan cleanup and operations.** Define retention or deletion for published entries and quarantine or replay for failures. Monitor old unpublished rows, publish delay, retry count, and outbox size. Coordinate schema changes and payload compatibility with consumer rollout.
Choosing polling or CDC
Polling is straightforward to understand and does not require change-log integration, but it can add query load and delay up to the polling interval. CDC can use a database log or stream for lower-latency publication, but adds connector, log-retention, checkpoint, and recovery operations. Review the guarantees your database-specific CDC provides for transaction boundaries, ordering, and replay. Do not generalize a broker feature in one example to other products.
How to verify
Exercise a database rollback, process crash immediately after commit, crash after broker send but before recording acknowledgement, relay restart, and concurrent updates to one entity. Confirm rolled-back events are not published, committed events are relayed after recovery, duplicates cause only one consumer effect, and required order is preserved. No live system was deployed or tested for this article.
Sources
[Microservices.io: Transactional outbox pattern](https://microservices.io/patterns/data/transactional-outbox.html)
[AWS Prescriptive Guidance: Transactional outbox pattern](https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/transactional-outbox.html)