Zeeshan Adil
Book a call
← All concepts

CONCEPT

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.

Also called: outbox pattern. By Zeeshan Adil··Distributed Systems·Databases

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.

Code
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, 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.

RUN IT YOURSELF

Challenge · Distributed Systems ChallengesOne message per action through a flaky provider

GO DEEPER

When RabbitMQ Isn't Enough: Building Durable, Retryable JobsDatabase-backed jobs with atomic claiming, backoff, circuit breakers and an outbox, and what each piece does and does not guarantee.Why Exactly-Once Processing Is Hard: RabbitMQ, Retries, and IdempotencyWhere RabbitMQ duplicates come from and how an outbox, deduplication keys and idempotent consumers protect database effects and external calls.
What is an idempotency key?What is at-least-once delivery?

SOURCES

Get the next deep dive by email

New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.

You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.

Written by Zeeshan Adil

I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.