Video in progress
Design a fault-tolerant order placement flow involving order, warehouse, payment and delivery services.
The order is created in PENDING and the saga starts through the outbox. Every step is idempotent, has a compensation, and is watched with a timeout. Payment is the pivot step, and delivery is the last step, which is only retried.
📖 Answer
I build the flow with an orchestrator, because there are 4 services, compensations and timeouts.
1. order: Order(PENDING) + outbox(ReserveStock) — one transaction
2. inventory: reserves → StockReserved | StockRejected
3. payment: charges the money → PaymentDone | PaymentFailed ← pivot
4. order: APPROVED, outbox(ScheduleDelivery)
5. delivery: schedules (retry only, no compensation)
Failures at each step:
- Inventory rejected: the order becomes
REJECTED, with a clear reason for the customer. - Payment declined: the
ReleaseStockcompensation, and the order becomesREJECTED. - Payment didn't respond (the hardest): it is unknown whether the money was charged. You can't compensate. The saga moves to a
PAYMENT_UNKNOWNstate and the payment status is queried with the idempotency key. Only after a definite "not charged" answer is the stock released. - Delivery isn't working: the order is paid, we don't go back. We retry with backoff, and if it drags on, an alert and manual intervention.
Infrastructure requirements:
- every message carries
sagaId+stepIdas the idempotency key; - the saga table has a
deadlinecolumn; a scheduler finds expired ones and starts the timeout path; - the reservation in inventory also has a TTL (say 15 minutes), so even if the orchestrator disappears completely the stock isn't reserved forever.
The synchronous response to the user: 202 Accepted and the order ID. The UI gets the state through polling or push. If the business requires "instant confirmation", the orchestrator can wait 2–3 seconds for the result and then return with PENDING; in most cases the saga finishes in that time.
Before shipping this design to prod, I would kill a service at every step (a chaos test) and check the result: no order should be left "hanging".
Sign up to mark studied questions and save your progress.