# What is the transactional outbox pattern?

> The transactional outbox pattern writes an event to an outbox table in the same database transaction as the state change it describes. A separate relay process later reads the table and publishes the event. The event cannot be lost if the change committed, but it may be published more than once.

- Author: Zeeshan Adil (https://www.internalswithzee.com/about)
- Published: 2026-11-12
- Source: https://www.internalswithzee.com/concepts/transactional-outbox

Also called: outbox pattern.

## The problem it solves

Saving an order and announcing it on a message broker are two writes to two systems, and there is no transaction across both. If you save and then publish, a crash between them leaves an order nobody hears about. If you publish and then save, a crash leaves an announcement for an order that does not exist. One of the two has to be the source of truth.

## How it works

The database is the source of truth, so the announcement goes there too.

```
BEGIN
  UPDATE orders SET status = 'paid' WHERE id = 42
  INSERT INTO outbox (event_id, payload) VALUES ('e-9001', '...')
COMMIT          -- both rows exist, or neither does

relay: read unpublished outbox rows -> publish -> mark as published
```

The relay can be a polling loop or a change-data-capture reader. If the relay crashes after publishing but before marking the row, it publishes the row again on restart.

## What it does not guarantee

It gives at-least-once publishing: the event is not lost, but it can appear more than once. It does not give exactly-once publishing, and it does not order events across different aggregates unless you design for that. So the consumer has to deduplicate, usually by recording each event id it has processed in the same transaction as its own effect. Brokers have their own reliability settings (publisher confirms, durable queues, acknowledgements), and the outbox only covers the step from your database to the broker. Old published rows should be cleaned up so the table does not grow forever.

## Measured

This pattern is one part of my [one message per action challenge](/challenges/distributed-systems/one-message-per-action), which sends a text message through a fake provider that times out, duplicates webhooks and reorders them. There the send intent is an outbox row, written in the same transaction as the compare-and-set that proves the worker still owns the action, with a stable idempotency key used for every send.

In a local chaos run of 2,000 actions with 200 random SIGKILLs and a PostgreSQL crash-restart, the provider's own log showed exactly one effect for 2,000 of 2,000 actions: none sent twice, none lost. The outbox did not do that alone. The outbox kept the intent from being lost, and the idempotency key and a lookup before any resend kept it from running twice.

## Related

- Challenge to run: https://www.internalswithzee.com/challenges/distributed-systems/one-message-per-action
- Article: https://www.internalswithzee.com/blog/resilient-jobs-unreliable-apis
- Article: https://www.internalswithzee.com/blog/message-queue-patterns
- Related concept: https://www.internalswithzee.com/concepts/idempotency-key
- Related concept: https://www.internalswithzee.com/concepts/at-least-once-delivery

## Sources

- [microservices.io: Pattern: Transactional outbox](https://microservices.io/patterns/data/transactional-outbox.html)
- [RabbitMQ docs: Reliability Guide](https://www.rabbitmq.com/docs/reliability)

