A service often has to do two things together: save a change to its database and tell other services about it. "Save the order, then publish OrderPlaced to Kafka" looks simple, but the two steps can't share a transaction. If the service crashes between them, the order exists and nobody downstream hears about it. If it publishes first and the database write then fails, other services act on an order that doesn't exist. This is the dual-write problem.
The transactional outbox pattern solves it by writing the event into an outbox table in the same database transaction as the business change. A separate process, the message relay, reads the outbox and publishes to the broker. The event goes out if and only if the transaction commits, with no two-phase commit across the database and the broker.
How it works
- Begin a transaction.
- Insert or update the business rows.
- Insert one outbox row per event: an event ID, the aggregate type and ID, the event type and a JSON payload.
- Commit.
- The relay publishes outbox rows to the message queue or event stream and marks them as sent, or deletes them.
Chris Richardson's microservices.io catalogue describes two ways to build the relay:
- Polling publisher. A worker queries unsent rows every second or so, in ID order. Simple and works on any database, at the cost of some load and latency.
- Transaction log tailing. A change data capture tool reads the database's write-ahead log or binlog. Debezium ships an outbox event router for Kafka Connect. By default it routes each row to a topic named after its aggregate type (
outbox.event.order) and uses the aggregate ID as the message key, so all events for one order land on the same partition in order.
Example: order placed in a PostgreSQL service
BEGIN;
INSERT INTO orders (id, customer_id, total_paise, status)
VALUES ('ord_81', 'cus_12', 249900, 'PLACED');
INSERT INTO outbox (id, aggregatetype, aggregateid, type, payload)
VALUES (gen_random_uuid(), 'order', 'ord_81', 'OrderPlaced',
'{"orderId":"ord_81","customerId":"cus_12","totalPaise":249900}');
COMMIT;
If the commit fails, neither row exists and no event is sent. If it succeeds, Debezium picks up the outbox insert from the write-ahead log and publishes it. Inventory, notifications and analytics consume OrderPlaced without the order service knowing they exist.
What the pattern doesn't give you
- Exactly-once delivery. The relay can publish a message and crash before recording that it did, so it publishes again after a restart. Consumers must be idempotent: store each event ID and skip repeats. The idempotency page explains how.
- Global ordering. You get order per aggregate when messages are keyed by aggregate ID. Don't assume order across different aggregates.
- A small table. Delete or archive sent rows on a schedule, or the outbox grows without limit.
Outbox vs saga, CDC and event sourcing
People often search "outbox pattern vs saga", but the two solve different problems. A saga coordinates a business process across services with compensating steps. Each saga step usually relies on an outbox to publish its event reliably.
Plain CDC on business tables also avoids dual writes, but it turns your internal schema into the event contract. An outbox lets you publish deliberate, versioned events instead. Event sourcing is the alternative where the event log itself is the source of truth, so there is no second write to keep in step.
The inbox pattern is the mirror image on the consumer side. The receiver records each incoming message ID in an inbox table in the same transaction as its own change, which makes processing idempotent.
Our Kafka vs RabbitMQ vs SQS comparison covers the broker side, and monolith vs microservices for startups explains when you need this at all.