Transactional Outbox
The history and mechanics of recording a business change with a pending message, including relay delivery, duplicates, ordering, and retention.
· Last updated: 2026-11-07 · 9 min read
Overview
The transactional outbox pattern writes a business data change and an outgoing message into the same database within a single local transaction. A relay worker then ships those committed rows to a broker.
Why not just commit to the database and publish to the broker in two consecutive lines of code? Because the gap between those two systems is a failure window: crash after committing the order but before publishing the event and downstream services never hear about the order, or publish the event first and watch another service act on an order whose database transaction subsequently rolls back.
An outbox closes that gap by turning “a message needs to be sent” into a durable row committed alongside the order itself. Even if the relay crashes or the broker is briefly unreachable, the pending event sits safely on disk, cleanly separating the lifetime of the HTTP request that created the order from the lifetime of message delivery.
Table names and cron intervals are secondary details. What defines an outbox design is its transaction boundary: which records commit together, how the relay extracts them, and how receivers deduplicate the inevitable replays.
Timeline
1981: Jim Gray's VLDB paper The Transaction Concept: Virtues and Limitations discussed the boundary between transaction-protected data and external actions.1
2003: Gregor Hohpe's “Enterprise Integration Patterns” presentation compared file transfer, shared databases, remote procedure invocation, and messaging as the four primary enterprise integration styles.2
October 2018: Manning published Chris Richardson's Microservices Patterns, codifying the Transactional Outbox pattern for coupling local database updates with event publication.3
Protected data and external actions (1981)
The underlying problem goes back to Jim Gray's 1981 paper on transaction concepts, which divided actions into unprotected work that needs no recovery, protected work (like database writes) that can and must be undone or redone, and real actions (like an ATM dispensing cash) that cannot be undone once performed and must therefore wait until commit, tagged with sequence numbers so duplicate executions can be ignored.1
When a message queue participates in the same transaction manager as the database, enqueuing is a protected action too. Most modern message brokers, however, sit outside the business database's local transaction scope, so calling db.commit() and broker.publish() back to back cannot make them atomic; rather than pulling in a cross-system two-phase commit, the outbox pattern records the intent to send inside the local protected boundary first.14
Enterprise integration and transactional messaging (2003)
In enterprise messaging, Gregor Hohpe's 2003 presentation highlighted how delivery timing and system coupling shape integration choices, and Hohpe and Bobby Woolf's Transactional Client pattern wrapped the client's messaging session in a transaction so outgoing messages enter the channel only on commit and consumed messages leave the channel only on commit.25
That pattern governs the messaging session itself. When a service also mutates its own relational database, the messaging session and the database connection have separate transaction boundaries unless coordinated, and the outbox bridges them by anchoring the pending message inside the database commit.
Service databases and event publication as a pattern (2018)
Chris Richardson codified that bridge for microservices in his October 2018 book Microservices Patterns (and his online pattern catalog, which also lists the alias Application events): write the domain update and the event row in one local transaction, then let an asynchronous relay ship committed events to the broker after the request returns.43
Notice the exact scope of that commit: it binds only the producer's domain row and outbox row. The consumer commits its own database changes in a separate transaction later, after the broker delivers the message.
Table polling and log-based delivery
How does the relay find committed events? Richardson distinguishes two main mechanisms: polling queries the outbox table for unsent rows, whereas transaction-log tailing reads the database's write-ahead log or binlog to capture inserts without extra SELECT load.4
Debezium's Outbox Event Router is a widely used log-tailing implementation built on change data capture (CDC). Out of the box, it maps the outbox row's id to a message header, aggregateid to the message key, aggregatetype to the target topic name, and payload to the message value, while extra columns such as event type can be routed into headers or the message envelope via table.fields.additional.placement.6
Polling lives and dies by poll interval, index design, and row-claiming locks across worker instances; log tailing trades those for WAL retention policies, connector offsets, and CDC pipeline operations. Both require a durable checkpoint so an interrupted relay knows where to resume.
Duplicate delivery and idempotent receivers
What happens if the relay publishes an event to the broker and crashes an instant before marking the outbox row sent or advancing its offset? On restart it will send the same event a second time, which is why AWS's pattern guidance stresses that an outbox provides at-least-once delivery and relies on idempotency at the receiver.7
A consumer achieves that idempotency by checking the incoming event's unique ID and recording that ID in the same local database transaction as its business update, ensuring that redeliveries become no-ops within the retention window of the processed-ID table.
That local deduplication table only protects state committed in the consumer's own database. If the consumer also charges a card or sends an email through an external provider, that provider call needs its own idempotency key contract.
Ordering, retention, and related patterns
Order matters too. Whenever two events on the same entity, such as OrderCreated and OrderCancelled, produce different outcomes if swapped, parallel consumers preserve per-entity sequence by routing events for the same aggregate ID through a consistent message key and broker partition.46
Lifecycle cleanup is a balancing act too: delete outbox rows before delivery is confirmed and you lose messages; keep every delivered row forever and table bloat degrades the producer database, so deletion or partition-drop policies must track broker acknowledgements and recovery windows.
Finally, keep neighboring patterns distinct. An outbox reliably publishes state changes, a saga coordinates multi-service workflows and compensating actions, and event sourcing makes the immutable event log itself the primary source of truth, a difference decided by which record defines current business state, not merely by whether a table holds events.
An example of two storage boundaries
Look at the two local transaction boundaries in the diagram below. A direct relay marks sent after the receiver commits, whereas a brokered relay marks sent as soon as the broker acknowledges the publish; the PostgreSQL script that follows demonstrates the producer side by committing an order and its outbox row together.
CREATE TEMP TABLE orders (id integer PRIMARY KEY, item text NOT NULL);
CREATE TEMP TABLE outbox (
event_id text PRIMARY KEY,
order_id integer NOT NULL,
event_type text NOT NULL
);
BEGIN;
INSERT INTO orders VALUES (1, 'book');
INSERT INTO outbox VALUES ('event-1', 1, 'OrderPlaced');
COMMIT;
SELECT id, item FROM orders;
SELECT event_id, order_id, event_type FROM outbox;
Both SELECT queries return one row: the order and event-1, because BEGIN and COMMIT sealed them in a single transaction, while handle_event on the right side of the diagram runs in a later consumer transaction (and in production the outbox is a regular persistent table so external relay workers or CDC connectors can read committed rows).
See also
Footnotes
- Jim Gray, “The Transaction Concept: Virtues and Limitations,” VLDB, 1981. Paper. ↩
- Gregor Hohpe, “Enterprise Integration Patterns,” presentation, 2003. Author's slides. ↩
- Chris Richardson, Microservices Patterns, Manning, October 2018. Publisher's record. ↩
- Chris Richardson, “Transactional Outbox.” Author's pattern. ↩
- Gregor Hohpe and Bobby Woolf, “Transactional Client.” Authors' pattern. ↩
- Debezium, “Outbox Event Router.” Official documentation. ↩
- AWS Prescriptive Guidance, “Transactional outbox pattern.” Official documentation. ↩