Skip to the guide

The outbox pattern and sagas

AdvancedConcept3 pages

Two problems appear once each service owns its database. How do you save a change and publish an event about it without losing one of the two? That's the outbox. And how do you run an action across services, like an order that reserves stock, charges a card and books a courier, with no transaction covering them all? That's a saga. Interviewers ask about both whenever orders or money are involved.

The out-tray and the trip you book yourself

A clerk files a signed contract and, in the same movement, drops the customer's letter into her out-tray. Both happen, or neither. The post room empties the tray later and posts whatever it finds; if it isn't sure a letter went, it posts it again.

Now book a trip yourself. Unlike the travel agent in Distributed transactions and two-phase commit, nobody holds all three: you book the flight, the hotel, then the car, each for real. If no car is available, you cancel the hotel and then the flight: each cancellation is a new action, and may cost a fee. So you book the refundable parts first and pay the non-refundable deposit last.