Microservices Transactional Outbox

A service often has to save data and announce the change to others. An order service stores a new order and publishes an OrderPlaced event. The database and the message broker are separate systems, so one can succeed while the other fails. The transactional outbox pattern closes that gap and keeps data and events in step.

Layman Explanation

A clerk writes a sale into the ledger and plans to phone the warehouse afterward. A power cut hits between the two steps, and the warehouse never hears about the sale. A smarter clerk writes both the sale and a "call the warehouse" note in the same ledger page. A second helper reads the notes and makes the calls. The ledger page is the outbox.

The Dual Write Problem

Order service:
  1. Save order in database      --> success
  2. Publish OrderPlaced event   --> broker down: FAILS

Result: order exists, nobody knows about it.

Reverse order of steps:
  1. Publish event               --> success
  2. Save order                  --> database fails

Result: others react to an order that never existed.

Neither order of steps is safe, because no transaction spans both systems.

How the Outbox Works

The service writes the business data and the event into the same database inside one local transaction. A separate process later reads the outbox table and sends events to the broker.

+---------------- Order service ----------------+
|  BEGIN TRANSACTION                            |
|    INSERT INTO orders (...)                   |
|    INSERT INTO outbox (event: OrderPlaced)    |
|  COMMIT                                       |
+-----------------------+-----------------------+
                        |
              Relay reads new outbox rows
                        |
                        v
                  Message broker --> subscribers

The commit succeeds for both rows or fails for both. An event exists exactly when its order exists.

The Outbox Table

ColumnPurpose
idUnique event identifier
aggregate_idOrder or entity the event belongs to
typeEvent name, such as OrderPlaced
payloadEvent data in JSON
created_atTime of creation
published_atTime of delivery, empty until sent

Two Ways to Relay Events

Polling Publisher

A background job queries for rows with an empty published_at, sends them to the broker, and marks them sent. Easy to build, with a small delay between polls.

Change Data Capture

A tool such as Debezium reads the database transaction log and streams new outbox rows to the broker. Latency stays low and the application code stays simple.

Duplicates and Ordering

A relay may crash after sending an event and before marking it sent. The next run sends the event again. Consumers must treat events idempotently, using the event id to skip repeats. Sending rows in creation order per aggregate preserves the sequence for each order.

Cleaning Up

The outbox grows with every event. A scheduled job deletes rows that were published more than a few days ago. Keeping a short history helps with debugging.

Related Pattern: Inbox

The receiving side stores processed message ids in an inbox table inside the same transaction as its own changes. Outbox on the sender plus inbox on the receiver produces reliable messaging with effectively exactly-once results.

Key Takeaways

The transactional outbox writes data and events in one local transaction, then relays events asynchronously. It removes the dual write problem and pairs naturally with idempotent consumers.