Save the change and the event in one local transaction, then publish the event safely.
The outbox pattern makes sure a service saves its data and sends its event together, or does neither. It does this by writing the event into an outbox table in the same database transaction as the data. A separate process then reads the outbox and sends the events.
A service often needs to do two things: change its database (save an order) and tell others (publish an OrderCreated event). This is called a dual write. The trouble is that a database and a broker cannot share one transaction. Whatever order you pick, a crash in the middle leaves a mess.
The outbox pattern turns the dual write into a single write. In one database transaction the service inserts the order and also inserts a row in an outbox table that describes the event. Both commit or both roll back. A relay (also called a message relay or publisher) then reads new rows from the outbox, publishes them to the broker, and marks them as sent.
| Polling publisher | Log tailing (CDC) | |
|---|---|---|
| How it works | A loop selects unsent rows on a timer | A tool reads the database transaction log and streams new rows |
| Delay | Up to the polling interval | Near real time |
| Load on the database | Repeated queries, even when idle | Low, reads the log |
| Set-up | A small piece of code | A CDC connector and database settings |
| Best for | Small to medium systems, simple set-up | High volume and low delay |
| Column | Example | Why |
|---|---|---|
| id | 7c1e... (unique) | Lets consumers spot duplicates |
| aggregate / key | order-1042 | Same key goes to the same partition, so order is kept |
| type | OrderCreated | Tells consumers what happened |
| payload | { "orderId": 1042, ... } | The event data, often JSON |
| created_at / sent_at | timestamps | Order of sending and cleaning up |
A simplified model. The crash hits the 2nd order (or the 1st if there is only one). Real relays poll the outbox table or read the database log (CDC).
SELECT ... FOR UPDATE SKIP LOCKED, or let a single leader run the relay.| Term | Meaning in easy words |
|---|---|
| Dual write | Writing to two systems (database and broker) with no shared transaction |
| Outbox table | A table that holds events waiting to be sent, written in the same transaction as the data |
| Relay / publisher | The process that reads the outbox and sends events to the broker |
| Transactional outbox | Another name for the outbox pattern |
| Polling publisher | A relay that checks the outbox on a timer |
| CDC (change data capture) | Reading the database change log to stream row changes |
| At-least-once delivery | Every event arrives, but some may arrive more than once |
| Idempotent consumer | A receiver that handles a repeated message safely |
| Inbox table | A table on the receiving side that remembers which messages were handled |
| Atomic | All of it happens or none of it does |
| Backlog | Events saved in the outbox but not yet sent |
| Choice | We gain | We lose |
|---|---|---|
| Outbox instead of dual write | No lost or phantom events | An extra table and process to run |
| Polling relay | Simple to build | Added delay and constant queries |
| CDC relay | Low delay and low load | More moving parts to operate |
| Short polling interval | Fresher events | More load on the database |
| Delete rows after sending | A small, fast table | Less history for audits |