Saga 模式工作流
patterns
针对订单、支付和配送服务的 Saga 编排模式分布式事务工作流,在失败时自动触发补偿回滚。
完整 FlowZap 代码
Orchestrator { # Saga Orchestrator
n1: circle label:"Start"
n2: rectangle label:"Begin distributed transaction"
n3: rectangle label:"Coordinate saga steps"
n4: rectangle label:"Commit saga"
n5: rectangle label:"Trigger compensations"
n6: circle label:"End"
n1.handle(right) -> n2.handle(left)
n2.handle(bottom) -> OrderService.n7.handle(top) [label="Step 1"]
n3.handle(bottom) -> PaymentService.n11.handle(top) [label="Step 2"]
n4.handle(right) -> n6.handle(left)
n5.handle(bottom) -> PaymentService.n15.handle(top) [label="Rollback"]
}
OrderService { # Order Service
n7: rectangle label:"Create order record"
n8: diamond label:"Order created?"
n9: rectangle label:"Reserve inventory"
n10: rectangle label:"Cancel order"
n7.handle(right) -> n8.handle(left)
n8.handle(right) -> n9.handle(left) [label="Yes"]
n8.handle(bottom) -> n10.handle(top) [label="No"]
n9.handle(top) -> Orchestrator.n3.handle(bottom) [label="Success"]
n10.handle(top) -> Orchestrator.n5.handle(bottom) [label="Failed"]
}
PaymentService { # Payment Service
n11: rectangle label:"Process payment"
n12: diamond label:"Payment successful?"
n13: rectangle label:"Confirm transaction"
n14: rectangle label:"Report payment failure"
n15: rectangle label:"Refund payment"
n16: rectangle label:"Release inventory"
n11.handle(right) -> n12.handle(left)
n12.handle(right) -> n13.handle(left) [label="Yes"]
n12.handle(bottom) -> n14.handle(top) [label="No"]
n13.handle(bottom) -> ShippingService.n17.handle(top) [label="Step 3"]
n14.handle(top) -> Orchestrator.n5.handle(bottom) [label="Compensate"]
n15.handle(right) -> n16.handle(left)
n16.handle(top) -> OrderService.n10.handle(bottom) [label="Undo order"]
}
ShippingService { # Shipping Service
n17: rectangle label:"Schedule shipment"
n18: diamond label:"Shipment scheduled?"
n19: rectangle label:"Generate tracking number"
n20: rectangle label:"Report shipping failure"
n17.handle(right) -> n18.handle(left)
n18.handle(right) -> n19.handle(left) [label="Yes"]
n18.handle(bottom) -> n20.handle(top) [label="No"]
n19.handle(top) -> Orchestrator.n4.handle(bottom) [label="Complete"]
n20.handle(top) -> Orchestrator.n5.handle(bottom) [label="Compensate"]
}