Transactional outbox & saga

Save an order and its message in one transaction, so a crash cannot leave an order with no message. Then run the order as a saga, and undo the finished steps when a later one fails.

Save an order and tell Payments

The Orders service saves order 17 for $40, and Payments must hear about it to charge the card. Pick how the order is saved and where a crash lands, then press Next step.

How the order is saved
Where a crash lands

With these settings: The order and its event committed together, so the crash lost nothing. Payments charged the $40 once.

Orders service, saves ordersOrders servicesaves ordersOrders database, orders, outbox, NOWOrders databaseorders, outboxorder + outbox rowNOWRelay, reads outboxRelayreads outboxPayments, charges cardsPaymentscharges cardsBroker, eventsBrokerevents
  • Orders service — saves orders — healthy
  • Orders database — orders, outbox — healthy — NOW — order + outbox row
  • Relay — reads outbox — healthy
  • Payments — charges cards — healthy
  • Broker — events — healthy
  • Local transaction
  • Outbox row
  • OrderPlaced event
Step 1 of 6

Step 1 of 6 · Orders database

Done: The order and an outbox row commit together

What happens
Orders saves order 17 for $40 and an OrderPlaced row, in one local transaction.
Why
One transaction means both rows commit, or neither does.
Next
The Orders service crashes

Event log

5 more to go. Press a line to go back to that step.

Order 17 saved
yes
Outbox row
not sent
Times the event was sent
0
Times $40 was charged
0

Step 1 of 6: Orders database. The order and an outbox row commit together. Orders saves order 17 for $40 and an OrderPlaced row, in one local transaction.

What this simplifies. This follows one order through whole steps. A real relay reads many rows at a time, and how often it checks the table is a setting. Payments keeps its list of handled event ids in its own database.

Step through one saga

One order needs three services, each with its own database. Pick which one fails, then press Next step. Back takes you to the step before.

Which step fails?

On: the order service has to ask for the refund twice.

With these settings: Cancelled and undone. The one finished step was compensated, newest first, so no service is left holding half an order.

CustomerCustomerOrder service, runs the saga, NOWOrder serviceruns the sagaORD-200: pendingNOWPayment service, its own databasePayment serviceits own databaseInventory service, its own databaseInventory serviceits own databaseShipping service, its own databaseShipping serviceits own database
  • Customer — healthy
  • Order service — runs the saga — healthy — NOW — ORD-200: pending
  • Payment service — its own database — healthy
  • Inventory service — its own database — healthy
  • Shipping service — its own database — healthy
  • Do a step
  • Failed or refused
  • Undo a step (compensation)
Step 1 of 6

Step 1 of 6 · Order service

Done: The customer places an order

What happens
The customer orders one jacket for $120. The order service saves the order as pending and starts the saga.
Why
Three other services each keep their own database. No single transaction can cover all of them, so the work is done as a chain of local steps.
What changes
Order ORD-200 is saved with the status pending.
Next
The payment service commits: charged $120

Event log

5 more to go. Press a line to go back to that step.

Order status
pending
Steps committed
0
Steps undone
0
Held from the customer
$0

Step 1 of 6: Order service. The customer places an order. The customer orders one jacket for $120. The order service saves the order as pending and starts the saga.

What this simplifies. This is one order with no other requests beside it, and a failure is a setting, not chance. Here the order service calls each service and waits. Real sagas often pass these steps as messages through a queue, and store the saga's own progress so it can carry on after a crash. A real compensation can also fail for good, and then a person has to step in.

Two-phase commit: the alternative to a saga

One order must charge $30 and reserve a shoe in two databases, all or nothing. Step through how two-phase commit does it, and what happens when the coordinator fails.

Scenario
Order service, workingOrder serviceworkingorder 42Payments DB, workingPayments DBworkingcharge $30, not finalInventory DB, workingInventory DBworkingreserve 1 shoe, not final
  • Order service — working — healthy — order 42
  • Payments DB — working — healthy — charge $30, not final
  • Inventory DB — working — healthy — reserve 1 shoe, not final

Step 1 of 7: Begin.

The order service is the coordinator: it runs one transaction that must charge $30 in Payments and reserve one shoe in Inventory.

Locks held. Payments: yes. Inventory: yes. Other orders that need a locked row must wait.