---
title: "Transactional Outbox"
description: "Commit an order and its event together; recover publication without repeating the effect."
source: https://vizlearn.app/cs/distributed/transactional-outbox
access: premium (interactive player requires Lifetime; this doc is free)
---

# Transactional Outbox

Transactional Outbox stores a business change and its outgoing event in the same database
transaction. An independent relay publishes committed events later. This preserves the intent to
send across an application crash, but the relay can publish twice. An idempotent consumer must
prevent those duplicate deliveries from repeating the business effect.

Suppose an order is saved, then the application crashes before announcing it. The order exists;
the reservation service knows nothing about it. Moving the announcement before the commit only
opens the opposite hole: a reservation for an order that rolled back. The useful move is to
store the announcement as data beside the order.

## How Transactional Outbox Works

The source database has an Orders table and an Outbox table. The application begins one local
transaction, writes order `O1`, writes event `E1: OrderPlaced(O1, units=1)`, then commits. Until
commit, both rows are drafts. After commit, both are durable. A rollback preserves neither.
The message broker is outside this transaction.

The relay reads committed `PENDING` rows. It publishes an event, receives confirmation from the
broker, then records `SENT` in the source database. Reading a row does not remove it. Sending a
packet does not mark it sent. Even receiving confirmation leaves a small interval before that
later database write.

If the relay crashes in that interval, recovery finds `E1` still pending. It publishes `E1`
again. The new attempt has a different attempt label, but the event ID must stay the same.
Otherwise the consumer could mistake a retry for new work.

The consumer uses its own local transaction. A unique processed-event record and the reservation
increment commit together. If `E1` is already recorded, the consumer skips the increment. A
separate check followed by an unprotected write is not equivalent: concurrent handlers or a
crash between commits can defeat it.

## Transactional Outbox Step by Step

The default selects Transactional outbox, Deduplicate atomically, and Crash after publish.
Reserved units start at `0` and the consumer's processed-ID set is empty.

1. Open the source transaction. Stage `O1`, then stage `E1` in the Outbox table. Neither draft
   is available to the relay.
2. Commit both rows together. The source now holds a durable order and a durable `PENDING`
   event. The broker has accepted nothing and the consumer has reserved nothing.
3. The relay reads `E1` and publishes attempt `p1`. The broker accepts it, then returns
   `CONFIRM p1`. The outbox row is still `PENDING`.
4. The relay crashes before recording `SENT`. Its memory disappears. The source rows and the
   broker's accepted publication survive.
5. The broker delivers `E1` from `p1`. The consumer checks its processed IDs within a transaction,
   finds no `E1`, and commits the reservation and `E1` together. Reserved units become `1`.
6. The relay restarts and reads the same pending row. It publishes `E1` as attempt `p2`, receives
   confirmation, and successfully records `SENT`.
7. The broker delivers `E1` again. The consumer finds the committed ID and skips the second
   increment. Two publications and two deliveries have produced one reservation.

The source bracket connects the two rows that become durable together. The separate consumer
bracket connects the ID and reservation. Between those brackets, `E1` travels twice; no single
transaction encloses the whole diagram.

## What Does Transactional Outbox Guarantee?

| Boundary | What it establishes | What it does not establish |
| --- | --- | --- |
| Source commit | Business state and publishing intent become durable together | Immediate broker acceptance |
| Broker acceptance | This publication reached the broker | Consumer completion or unique delivery |
| Recording SENT | The relay durably recorded a confirmed publication | A completed reservation |
| Guarded consumer commit | This event's local effect and processed ID commit together | Exactly-once external API calls |

With recovery, continued retries, and an eventually available broker, pending events can be
published after a crash. A permanent outage does not disappear because an outbox table exists.
The pattern trades a lost-send window for retryable work that may be duplicated; it does not
create a distributed transaction between the source, broker, and consumer.

Compare **Dual-write gap** with **Recover the send intent**. Both crash the application after
the source commit. The first keeps the order but loses the only send intent, which was in
memory. The second retains the outbox row, so an independent relay completes publication.
Then compare the default with **Duplicate reservation**: the publication schedule stays the
same, but removing the consumer guard changes reserved units from `1` to `2`.

## Edge Cases

- **Rollback before commit:** both source drafts disappear; nothing is published.
- **Application crash after commit:** outbox work survives independently of the application.
  Unprotected dual write has no durable send record in this model.
- **Publisher crash after confirmation:** outbox retries can duplicate `E1`. In the dual-write
  variant, the already accepted event still reaches the consumer once; no automatic retry is
  scheduled without an outbox.
- **No failure:** both publisher variants deliver once. The extra durable record matters when
  the execution is interrupted.

## Common Mistakes

- Committing the order and outbox row separately. The interval between commits reintroduces the
  missing-event failure.
- Marking `SENT` before publication. A crash can leave unpublished work invisible to the
  pending-row scan.
- Generating a fresh event ID on every retry. The consumer then sees different logical events.
- Recording the processed ID separately from the reservation. ID first can suppress missing
  work; effect first can repeat completed work.
- Treating `SENT` as proof of consumer completion, or applying the local database guarantee to
  an unrelated external side effect.

## A Note on Simplification

This model contains one order, one event, one polling relay, and one consumer. It schedules at
most one failure, then supplies successful recovery. Each accepted publication is delivered
once; additional broker retries and consumer ACKs are omitted. The Message Queues & Redelivery
lesson covers that separate failure window.

Concurrent relay claims, locking, CDC, replication, cleanup, retention, backoff, poison events,
consumer crashes, schema evolution, request idempotency, and multi-event ordering are outside
this trace. Ordering must be designed explicitly in a real relay. The reservation is a database
increment, not an external service call. Animation time does not measure network latency or a
polling interval.
