1Caller gọi run(data) cho ORD-1001, 450.000đ. Orchestrator duyệt List<SagaStep> đã được Spring sắp theo @Order (Go: NewOrderSaga khai báo []Step theo đúng thứ tự).The caller invokes run(data) for ORD-1001, 450,000 VND. The orchestrator walks List<SagaStep>, sorted by Spring using @Order (Go: NewOrderSaga declares the []Step in order).
2Bước CREATE_ORDER: sagaLog.started, rồi Do gọi Orders.CreatePending tạo đơn PENDING, sagaLog.completed và push bước vào Deque done. PENDING báo cho mọi người đơn chưa chốt.Step CREATE_ORDER: sagaLog.started, then Do calls Orders.CreatePending to create a PENDING order, sagaLog.completed, and the step is pushed onto the done Deque. PENDING tells everyone the order is not final.
3ChargePaymentStep.execute gọi payments.charge(orderId, customerId, total) và lưu d.setPaymentId(pay_77). Mã này cần cho compensate sau đó.ChargePaymentStep.execute calls payments.charge(orderId, customerId, total) and stores d.setPaymentId(pay_77). compensate will need this reference later.
4ReserveStockStep.execute gọi inventory.reserve và nhận lỗi hết hàng (lỗi đã rõ, không phải timeout). Orchestrator ghi log.warn và chuyển sang compensate; CREATE_SHIPMENT không bao giờ chạy.ReserveStockStep.execute calls inventory.reserve and gets an out-of-stock error (a known failure, not a timeout). The orchestrator logs a warning and switches to compensate; CREATE_SHIPMENT never runs.
5compensate pop bước gần nhất trong Deque: ChargePaymentStep.compensate gọi payments.refund(pay_77, "saga rollback ORD-1001"), rồi sagaLog.compensated. Payment phải chống trùng để retry không hoàn hai lần.compensate pops the most recent step from the Deque: ChargePaymentStep.compensate calls payments.refund(pay_77, "saga rollback ORD-1001"), then sagaLog.compensated. Payment must deduplicate so a retry never refunds twice.
6Pop tiếp CREATE_ORDER: Undo gọi Orders.Cancel(ORD-1001). ReserveStockStep không cần release vì nó chưa được push (kho chưa giữ hàng), và CREATE_SHIPMENT chưa chạy.Next pop is CREATE_ORDER: Undo calls Orders.Cancel(ORD-1001). ReserveStockStep needs no release because it was never pushed (nothing reserved), and CREATE_SHIPMENT never ran.
7run trả SagaResult.failed("RESERVE_STOCK", "out of stock"); Go trả errors.Join của lỗi saga và lỗi bù trừ (nếu có). Nếu một compensate lỗi, sagaLog.compensationFailed giữ lại để retry hay xử lý tay.run returns SagaResult.failed("RESERVE_STOCK", "out of stock"); Go returns errors.Join of the saga error and any compensation errors. If a compensate fails, sagaLog.compensationFailed parks it for retry or manual review.
8Biến thể choreography không có orchestrator: PaymentEventHandler nghe OrderCreated, trừ tiền rồi outbox.publish(PaymentCompleted). Khi Inventory phát StockReservationFailed, handler gọi refundByOrder (idempotent) và phát PaymentRefunded.The choreography variant has no orchestrator: PaymentEventHandler hears OrderCreated, charges, then outbox.publish(PaymentCompleted). When Inventory emits StockReservationFailed, the handler calls refundByOrder (idempotent) and publishes PaymentRefunded.