Outbox

Outbox transaccional

Escrito por Darjus Kazakevic · revisado el

Save the change and the message in one transaction; send the message later.

When you save a change and also publish a message about it, you write to two systems: the database and the broker. They cannot share one transaction. Commit and then crash before publishing, and the event is lost. Publish and then fail to commit, and you announced something that never happened. This is the dual-write problem.

The Transactional Outbox fixes it. You insert the message into an outbox table in the same transaction as the change. A separate relay reads the table and publishes to the broker, retrying until it works. Delivery becomes at-least-once, so consumers must handle duplicates.

En esencia — Insert the outgoing message into an outbox table in the same transaction as the state change, and let a relay publish it later, so neither is ever lost.

Cuándo recurrir a él

Recurre a él cuando

  • You must publish an event after a database write, without loss
  • A lost or phantom event would corrupt another service's data
  • Your broker cannot join your database transaction (almost always)

Reconsidéralo cuando

  • Nothing leaves the service: no message, no dual write
  • Losing an occasional message is fine, as with analytics pings
  • CDC on the business tables already gives consumers what they need

How it works

The dual-write problem
Writing to the database and publishing to a broker are two commits to two systems. A crash between them leaves the two out of sync: a lost event, or an event for a change that never committed.
The outbox table
A table in the same database as your data. Each row is one message: id, topic, payload, created time, published time. It is written in the same transaction as the change.
The relay
A process that reads unsent rows, publishes them, and marks them sent once the broker confirms. Polling runs a query every 100 ms to 1 s. Change data capture (CDC) tools such as Debezium read the database's write-ahead log instead.
At-least-once delivery
The relay can crash after publishing but before marking the row sent. On restart it publishes the row again. So consumers must skip duplicates by message id.

Applying it well

Never call the broker in the business transaction
The only write in the business transaction is to your own database. Publishing inside it brings back the dual-write problem and holds the transaction open while the broker answers.
Give every message a stable id
Create the id when you insert the row and send it with the message. Consumers use it to skip the duplicates the relay will create after a crash.
Claim batches with SKIP LOCKED
If more than one relay runs, claim rows with FOR UPDATE SKIP LOCKED. Each relay gets a different batch, and two relays never publish the same row at the same time.
Prune published rows
Delete or archive rows a few days after they are published. An outbox that only grows makes the relay query slower every week.

Errores habituales

  • Publishing to the broker inside the transaction anyway, which brings the dual write back.
  • Consumers that are not idempotent, so a relay restart charges a customer twice.
  • An outbox table that only grows, so the relay query gets slower every week.
  • Expecting order with several relays. Their batches publish in parallel, so two events for one order can swap. Run one relay, and use the order id as the message key.