Try
↑ ↓ move↵ openesc close

Interview prep / Sagas

Senior · Sagas · System design interview question

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

Learn it by running it

The sagas and compensation lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms