Browse patterns / Service architecture / B054
transactional outbox

Transactional outbox

Store an event in an outbox table inside the database transaction, then publish it to a broker from a separate process.

Recorded evidence 14/14Compare →Source
01 / UNDERSTAND

Structure and behavior

Follow the arrows step by step.

Business data and pending events are persisted in one transaction. Consumers must be idempotent to handle relay republication.

Expected benefits

Events exist only after the matching data change commits, reducing loss and improving DB-to-event consistency.

Trade-offs

Requires an additional outbox table and relay, and polling queries have a cost.

Delivery to systems such as Kafka can be at least once, so messages may arrive more than once.

02 / APPLY

Operations and adoption guide

Synthesis, distinct from items backed by source excerpts. Validate before applying in production.

When to use

Use for ordering, payments, notifications, and audit logging when database updates and event publication must stay consistent. Reduce loss and handle possible duplicates with idempotent consumers.

Scenario

An online pet supplier processes 1 million orders per day and needs orders committed in the DB to match their OrderPlaced events. The order service saves the order and outbox record in one transaction; a relay publishes committed records to Kafka. Payment confirmations and shipping notifications must not be lost. Consumers deduplicate by order ID. Monitor publishing lag and unpublished backlog, and quarantine failed messages in a dead-letter queue.

Placement principles

  • Store orders and outbox records in the same DB and schema
  • Run the relay embedded in the application or in a separate worker or Deployment
  • Use Kafka or managed Pub/Sub as a broker
  • Coordinate multiple relay pods with SKIP LOCKED or a unique Owner
  • Make the oldest unsent row, publishing lag, DLQ, and retention policy observable.

On premises

Place the table and relay in existing on-premises infrastructure such as an RDB and Kafka. Operate transaction boundaries, backups and retention, polling load, and relay high availability. Coordinate multiple relays through SKIP LOCKED or leader selection and document recovery and dead-letter retention.

03 / VERIFY

Evidence by criterion (14 total)

Evidence availability at the time of research. Assess source accuracy and currency separately.

Definition and overviewEvidence available

Store an event in an outbox table inside the database transaction, then publish it to a broker from a separate process.

This pattern saves events in a data store that's typically in an outbox table in your database before it pushes them to a message broker.

learn.microsoft.com original ↗

Problem addressedEvidence available

Addresses dual writes and inconsistency when database updates and event publication cannot commit atomically together.

A dual write operation occurs when an application writes to two different systems

docs.aws.amazon.com original ↗

Key components and resourcesEvidence available

An outbox table, a business data store, a separate relay or worker, and a message broker.

a separate service or worker process queries the outbox table for unhandled entries, publishes them, and marks them as processed.

learn.microsoft.com original ↗

Data and asset placementEvidence available

Write the outbox record and business data such as an order in the same DB transaction so they commit or roll back together.

`Outbox.add()` inserts the message into an outbox table in the same database transaction as the order, so both commit or neither does.

docs.nestjs.com original ↗

Typical use casesEvidence available

Suitable for order notifications, payment confirmations, and audit logs that require reliable and consistent database changes and event publication.

You need to update a database and publish an event reliably

trickle-labs.github.io original ↗

Example scenarioEvidence available

An online shop order API records the order and OrderPlaced event together, and a relay publishes the event.

the order API saves the order and announces it with an `OrderPlaced` event.

docs.nestjs.com original ↗

When to avoid and caveatsEvidence available

Delivery to systems such as Kafka can be at least once, so messages may arrive more than once.

We guarantee every message in the outbox will eventually arrive in Kafka, but it may arrive more than once.

developer.confluent.io original ↗

On premises: development, deployment, and operationsEvidence available

A deployment and operations example runs the service on an on-premises Kubernetes cluster before migrating to the cloud.

Initially, services were operated in an on-premise Kubernetes cluster.

kinneko.de original ↗

Cloud: development, deployment, and operationsEvidence available

Cloud deployments can use MongoDB change streams and managed brokers such as Google Pub/Sub.

using MongoDB change streams and Google Pub Sub as message broker.

github.com original ↗

Kubernetes: development, deployment, and operationsEvidence available

Run the relay as a Kubernetes Deployment, with a unique Owner per pod, SKIP LOCKED coordination, readiness and liveness probes, and a long termination grace period.

Run the relay embedded or as a Deployment. Give every pod a unique `Owner`.

github.com original ↗

Incident responseEvidence available

Relay interruptions, poison payloads, and locks on the outbox table can halt publication. Monitor the oldest unpublished row and publishing lag.

The relay is a polling worker or a change data capture process, and either can stop.

operatex.dev original ↗

BenefitsEvidence available

Events exist only after the matching data change commits, reducing loss and improving DB-to-event consistency.

The event exists if and only if the data change committed.

shardcraft.dev original ↗

Drawbacks and trade-offsEvidence available

Requires an additional outbox table and relay, and polling queries have a cost.

An extra table and a relay

shardcraft.dev original ↗

Representative tools and technologiesEvidence available

Examples include MongoDB change streams, Google Pub/Sub, message brokers, an outbox table, and a relay.

using MongoDB change streams and Google Pub Sub as message broker

github.com original ↗

Additional references (21)