Saga 编舞模式架构
Architecture
Saga 编舞架构图,订单、支付和库存服务通过领域事件协调而无需中央编排器,每个服务发布和订阅驱动事务前进或触发补偿的事件。该模板模拟去中心化的 Saga 方法,服务自主响应事件,减少单点故障但增加了跟踪 Saga 状态的复杂性。适合偏好服务自治而非集中控制的团队。
完整 FlowZap 代码
OrderService { # Order Service
n1: circle label:"Place Order"
n2: rectangle label:"Create Pending Order"
n3: rectangle label:"Emit OrderCreated"
n4: rectangle label:"Listen for Saga Events"
n5: diamond label:"Saga Outcome?"
n6: rectangle label:"Confirm Order"
n7: rectangle label:"Cancel Order"
n1.handle(right) -> n2.handle(left)
n2.handle(right) -> n3.handle(left) [label="Pending"]
n3.handle(bottom) -> EventBus.n8.handle(top) [label="Publish"]
n4.handle(right) -> n5.handle(left)
n5.handle(right) -> n6.handle(left) [label="Success"]
n5.handle(bottom) -> n7.handle(top) [label="Failure"]
}
EventBus { # Event Bus
n8: rectangle label:"OrderCreated Topic"
n9: rectangle label:"PaymentProcessed Topic"
n10: rectangle label:"InventoryReserved Topic"
n11: rectangle label:"PaymentFailed Topic"
n8.handle(bottom) -> PaymentService.n12.handle(top) [label="Subscribe"]
n9.handle(bottom) -> InventoryService.n15.handle(top) [label="Subscribe"]
n10.handle(top) -> OrderService.n4.handle(bottom) [label="Notify Success"]
n11.handle(top) -> OrderService.n4.handle(bottom) [label="Notify Failure"]
}
PaymentService { # Payment Service
n12: rectangle label:"Process Payment"
n13: diamond label:"Payment OK?"
n14: rectangle label:"Emit PaymentProcessed"
n18: rectangle label:"Emit PaymentFailed"
n12.handle(right) -> n13.handle(left)
n13.handle(right) -> n14.handle(left) [label="Success"]
n13.handle(bottom) -> n18.handle(top) [label="Declined"]
n14.handle(top) -> EventBus.n9.handle(bottom) [label="Publish"]
n18.handle(top) -> EventBus.n11.handle(bottom) [label="Publish"]
}
InventoryService { # Inventory Service
n15: rectangle label:"Reserve Inventory"
n16: rectangle label:"Emit InventoryReserved"
n17: rectangle label:"Compensate: Release Stock"
n15.handle(right) -> n16.handle(left) [label="Reserved"]
n16.handle(top) -> EventBus.n10.handle(bottom) [label="Publish"]
n17.handle(top) -> EventBus.n11.handle(bottom) [label="On PaymentFailed"]
}
为什么需要这个工作流?
Orchestration-based sagas create a central coordinator that becomes a single point of failure and a deployment bottleneck. Choreography distributes saga logic across participating services, where each service autonomously publishes and subscribes to events—enabling fully independent deployment and eliminating the coordinator dependency.
工作原理
- Step 1: The Order Service creates a pending order and publishes an OrderCreated event.
- Step 2: The Payment Service subscribes to OrderCreated, processes payment, and publishes PaymentProcessed or PaymentFailed.
- Step 3: The Inventory Service subscribes to PaymentProcessed and reserves stock.
- Step 4: On successful reservation, InventoryReserved is published, completing the saga.
- Step 5: If payment fails, PaymentFailed triggers compensation in the Inventory Service.
- Step 6: The Order Service listens for all outcome events to update the final order status.
替代方案
Orchestration provides better visibility and easier debugging. Process managers offer a middle ground between pure choreography and orchestration. This template shows the fully decentralized choreography approach.
Key Facts
| Template Name | Saga 编舞模式架构 |
| Category | Architecture |
| Steps | 6 workflow steps |
| Format | FlowZap Code (.fz file) |
相关模板
Saga 编排模式架构
Architecture
Saga 编排架构图,中央编排器协调跨订单、库存和支付服务的多步骤分布式事务,具有专用的补偿链用于失败时的回滚。该模板模拟基于编排的 Saga 模式,单个协调器管理事务生命周期并在任何步骤失败时触发补偿操作。对于实施不使用两阶段提交的可靠分布式事务的架构师至关重要。
Saga 旅行预订架构
Architecture
旅行预订 Saga 架构图,将航班、酒店和租车预订编排为单一分布式事务,任何预订步骤失败时自动补偿取消所有预订。该模板模拟经典的 Saga 用例,多个独立服务必须全部成功或全部回滚,确保旅客不会出现部分预订。非常适合用真实业务场景演示 Saga 模式。
事件驱动领域事件架构
Architecture
领域事件架构图,展示聚合根如何引发领域事件,这些事件既在进程内分发给本地处理器,也跨边界分发给其他限界上下文中的集成事件消费者。该模板模拟 DDD 事件模式,领域逻辑通过干净的事件分发器触发副作用,保持领域和基础设施关注点的分离。对于实施基于事件集成的领域驱动设计的团队至关重要。
无服务器 Step Functions 编排架构
Architecture
AWS Step Functions 编排架构图,展示状态机工作流,包括选择状态、并行处理、等待回调模式以及失败步骤的补偿回滚。该模板模拟无服务器工作流编排,复杂的多步骤流程定义为具有内置错误处理和重试逻辑的状态机。对于构建需要人工审批或长时间运行流程的可靠无服务器工作流的团队至关重要。
Saga 分布式事务架构
Architecture
分布式事务架构图,实现两阶段提交协议,事务协调器向参与服务发送准备和提交消息,任何投票失败时执行全局中止。该模板可视化经典的 2PC 协议,用于多个服务间需要强一致性的场景,展示准备、投票和提交/中止阶段。对于理解微服务架构中分布式共识权衡至关重要。