# Transactional outbox

## What it is

A transactional outbox is a durable record that tells another process what to
publish after a database change. The application writes the change and the
outbox entry within one database transaction. The entries commit together. If
the database rolls back, a relay has no committed entry to publish. The relay
may poll a table or watch changes through the database's change stream. The
destination can be a broker, an integration service or another component with a
documented delivery contract.

Consider a service that records that a parcel has been received at a depot. The
same transaction updates the parcel record and inserts an event with a stable
identifier, a parcel reference, an event type and a version. The HTTP request
can finish once the transaction commits. A separate publisher sends the event
and records progress. The service can acknowledge the committed update
immediately while transport and downstream processing follow on their own
schedule.

The producer decides which facts belong in the event and preserves them under
the same commit boundary as the change. The publisher is responsible for
retrying transmission. The receiver needs a way to recognise an event already
applied. If the publisher sends successfully and stops before recording that
success, the same event can be sent again after it returns. The event identifier
must survive this repeat.

## Why it matters

Without a shared commit boundary, a producer must choose an order for two
independently fallible operations. A database write can succeed while
publication fails. Sending first can let another service act on a change the
database later rejects. Closing either window with retries alone requires
knowing which step happened. An outbox gives the publisher a durable answer
about committed intent, including when the producer restarts or the broker is
unavailable for a while.

A customer can observe the effects of the delay. The depot screen might
immediately show the parcel at the facility, while an email notification arrives
later. Define which result the request acknowledges and which ones arrive
asynchronously. If a subsequent business step must wait for the broker consumer
to finish, model that pending state explicitly and decide what response the user
can see while it is pending.

The database now holds a buffer that must be operated. Count unpublished records
and track the age of the oldest one. A growing count can be normal for a short
burst, while a stable count with one ageing entry can indicate an event that
never makes progress. Observe relay attempts and consumer failures separately.
When somebody investigates a delayed shipment, they should be able to
distinguish a committed parcel update from a published event and a completed
notification.

## What it looks like in practice

Start with an integration that can accept duplicate deliveries safely. In an
isolated test, update a parcel and write its event. Stop the publisher before it
reads the committed entry and confirm that the entry is present after restart.
Next stop it after a send and before saving progress. The receiver should handle
the repeat using the existing event ID. Check the actual side effect, such as a
notification record, and count the acknowledgements at the broker separately.

Test a database rollback separately. Ask the producer to perform an operation
that fails a constraint and verify that neither the proposed row change nor the
event is visible afterward. Then commit two changes for one parcel while a
different parcel's events are also arriving. If consumers depend on per-parcel
order, give them a sequence or version with a meaningful scope and test delivery
out of order. A timestamp alone may order items differently from the application
changes they describe.

Our
[case study about moving slow work onto a River job queue](/portfolio/moved-slow-work-onto-a-river-job-queue/)
describes keeping jobs and the rows they touch in PostgreSQL so they can commit
together. That shared commit boundary is relevant here. The parcel example adds
a separate relay and a receiver to demonstrate publication to another system.
The case outcome is about in-database background work and an inspectable
history. The additional delivery test applies when the durable record is a
request for external publication.

## Where it goes wrong

An event tied only to a reference can describe the wrong version if the relay
looks up the current state long after the original update. Store the data or
immutable version that the consumer needs to act on. Decide which fields are
useful, who can read them and how long the event should be retained. Copying an
entire application record can put sensitive fields into an additional store and
make later schema changes harder to manage.

Repeated delivery is a normal recovery path. The consumer needs to decide how
long to retain its processing record and keep that decision consistent with the
maximum redelivery or replay window. For effects controlled by the receiving
database, recording the event identity and the side effect in the same receiver
transaction avoids another gap. External effects need their own idempotency
mechanism or a reconciliation process based on a stable reference.

An outbox that grows without a limit can eventually threaten the database that
serves everyday application traffic. Define when published entries can be
archived or removed and which processing state the cleanup uses. Monitor free
space, publishing delay and the rate at which new events are generated. Keep a
procedure for finding and dealing with an event the relay cannot send. In a
drill, prove that later work can still proceed under the required ordering
policy and that a repaired event can be replayed safely.

## Further reading

- [AWS transactional outbox guidance](https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/transactional-outbox.html)
  explains the failure window between separate database and messaging writes.
- [Microsoft's Cosmos DB outbox architecture](https://learn.microsoft.com/en-us/azure/architecture/databases/guide/transactional-outbox-cosmos)
  shows how a change feed can drive the relay.
- [PostgreSQL transactions](https://www.postgresql.org/docs/18/tutorial-transactions.html)
  describes the commit boundary that an outbox can share with application data.

<https://engineer.company/glossary/transactional-outbox/>
