Transactional outbox
Store an event in an outbox table inside the database transaction, then publish it to a broker from a separate process.
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.
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.
Cloud
Use managed database transactions and messaging with change streams, CDC, or a polling relay. Use managed IAM, dead-lettering, retention, autoscaling, and observability. Do not assume exactly-once delivery; make consumers idempotent.
Kubernetes
Deploy the relay as a Deployment or a sidecar, and coordinate it with per-pod Owner identifiers and SKIP LOCKED. Set maxUnavailable to 0, allow a long termination grace period, check relay readiness and process liveness, and track DB and broker connections, resource limits, HPA, and backlog metrics.
Incident response
Typical incidents include a crashed or stopped relay, poison payloads, table lock contention, broker connection failures, and duplicate publication. Inspect the oldest unpublished row and lag, classify problems as transient or permanent, retry transient failures with backoff, and quarantine permanent failures in a dead-letter queue for later replay. Confirm consumer idempotency and that backlog drains.
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.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 systemsKey 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.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.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 reliablyExample 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.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.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.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.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`.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.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.Drawbacks and trade-offsEvidence available
Requires an additional outbox table and relay, and polling queries have a cost.
An extra table and a relayRepresentative 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 brokerAdditional references (21)
- Transactional outbox pattern - AWS Prescriptive Guidance ↗
- Pattern: Transactional outbox ↗
- Implement the Transactional Outbox Pattern by Using Azure Cosmos DB - Azure Architecture Center | Microsoft Learn ↗
- Transactional Outbox | Arcflow ↗
- The Transactional Outbox Pattern ↗
- Transactional outbox | NestJS - A progressive Node.js framework ↗
- Transactional outbox - pg_tide Documentation ↗
- aws-samples/transactional-outbox-pattern ↗
- Medium ↗
- xeno097/transactional-outbox-pattern-with-mongodb ↗
- docs/operations.md ↗
- ssa1004/gpu-job-orchestrator ↗
- KinNeko - Knowledge ↗
- hitesh22rana/chronoverse ↗
- Lessons from using the outbox pattern at scale ↗
- Transactional Outbox: Retry, Backoff and Monitoring ↗
- Transactional Outbox Pattern Problems That Start on Day Two ↗
- The transactional outbox: a write and a message as one step | ShardCraft ↗
- Part 2: Dual-Write and Retries - the Transactional Outbox, Backoff, Circuit Breakers | Hiep Lam ↗
- Transactional Outbox and Relay | CDC: The Missing Manual ↗
- Revisiting the Outbox Pattern ↗