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
