liveBook · Manning

Outbox pattern

As explained in Grokking Software Architecture

A pattern that stores business data and an outgoing message in the same local transaction, then publishes the message asynchronously.

An order is saved, but the network drops before the OrderPlaced event reaches the broker. Shipping and payment never hear about it. Publishing first is no safer: if the database write fails, the event announces an order that does not exist. This fragile two-step operation is a dual write. section 7.5.1

The transactional outbox pattern replaces the network write with a local one. The service saves the business data and an outgoing event in an Outbox table in the same database transaction. Both writes commit, or neither does. A separate background process reads the table and forwards messages to the broker asynchronously. section 12.6.1

Keeping a saga moving

An inventory service records an inventory deduction and its InventoryDeducted event together. If the service crashes before publishing, the committed event remains in the outbox for the background process to forward; billing does not depend on the original request making a network call successfully. section 12.6.1

The local transaction protects the data and the record of the message—not a simultaneous database-and-broker commit. The relay provides at-least-once delivery, so consumers must be ready for repeated events. An event identifier can help an idempotent consumer recognize an event occurrence. section 12.6.1 section 7.5.1

An outbox can also support application-level synchronization during a migration when database transaction logs are inaccessible. It avoids writing directly to both the legacy and new databases across an unreliable network; change data capture is another option when log access is available. section 12.5.2

How Grokking Software Architecture covers it

How deep it goes
Explains the atomic local write and background relay, while deferring the relay's internal mechanics.
Examples
Uses order placement and an inventory-to-billing saga to illustrate missed events and recovery.
What you can do after
Evaluate how an outbox avoids inconsistent local writes and supports at-least-once event delivery.
How it connects
Connects the pattern to sagas, dual writes, change data capture, and idempotent consumers.

Also explained in

Compare how 4 books explain outbox pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.