Services

Resources

Company

Transactional outbox pattern

The transactional outbox pattern fixes the dual write problem: a service that writes to its database and then publishes to a broker such as Kafka can crash or time out between the two, leaving them out of sync. With an outbox, the service writes the business row and an event row to an outbox table in the same transaction, so both commit or neither does.

How it works

A separate relay reads the outbox and publishes each event. The relay can poll the table, or use change data capture with a tool like Debezium, which tails the database log and publishes new outbox rows as they commit.

For example, without an outbox, an order commits but the publish to Kafka times out, so the warehouse never hears about it. With an outbox, the orders row and the OrderPlaced outbox row commit together. If the relay fails to publish, it retries from where it stopped. Delivery is at least once, so consumers must be idempotent and skip event ids they have already handled.

Why it matters in production

The outbox trades a little latency and an extra table for correctness. Watch relay lag, clean up published rows, keep event schemas versioned, and make every consumer handle duplicates, because retries will produce them.

See also

On the One2N blog

References

  1. Chris Richardson: Transactional outbox pattern

  2. Debezium: Outbox event router

Comic about outbox pattern: an order service saves order 1042, then publishing OrderPlaced times out, so the warehouse never hears of it and a paid order never ships; after the fix, one transaction writes the order row and outbox event 7781 together, a relay such as Debezium tails the outbox and retries failed publishes, and event 7781 can arrive twice, so the warehouse skips ids it has already processed.