Design a saga for placing an order: reserve stock, charge the card, create the shipment. Where does it get hard?
Sketch your own answer first, then compare it with the one below.
A strong answer
Order the steps by how hard they are to undo. Reserve stock first, since a reservation is cheap to release. Charge the card next. Create the shipment last, because a shipment is the hardest to take back.
Use an orchestrator that persists its state. Give each step and each compensation an idempotency key. Mark the order pending, as a semantic lock, so the user and other code see that it may change. Set a deadline per step and a retry limit, then park the saga for a person.
The hard parts: a charge that succeeded but whose reply was lost, so the saga must look up the payment before compensating. A reservation that expires while the charge is slow. A compensation that fails for good. Name each, and say what you would monitor, such as the count of parked sagas and their age.
Sources
- Chris Richardson (2018). Pattern: Saga.
- Hector Garcia-Molina, Kenneth Salem (1987). Sagas.
- Pat Helland (2007). Life beyond Distributed Transactions: an Apostate's Opinion.