欢迎使用 FlowZap,快速、清晰、掌控的绘图应用。

Saga 编排模式架构

Architecture

Saga 编排架构图,中央编排器协调跨订单、库存和支付服务的多步骤分布式事务,具有专用的补偿链用于失败时的回滚。该模板模拟基于编排的 Saga 模式,单个协调器管理事务生命周期并在任何步骤失败时触发补偿操作。对于实施不使用两阶段提交的可靠分布式事务的架构师至关重要。

完整 FlowZap 代码

Orchestrator { # Saga Orchestrator
n1: circle label:"Start Saga"
n2: rectangle label:"Create Saga Instance"
n3: rectangle label:"Execute Step 1: Create Order"
n4: rectangle label:"Execute Step 2: Reserve Inventory"
n5: rectangle label:"Execute Step 3: Process Payment"
n6: diamond label:"All Steps Succeeded?"
n7: rectangle label:"Mark Saga Complete"
n8: rectangle label:"Begin Compensation"
n9: circle label:"Saga Finished"
n1.handle(right) -> n2.handle(left)
n2.handle(right) -> n3.handle(left) [label="Initialize"]
n3.handle(right) -> n4.handle(left) [label="Step 1 OK"]
n4.handle(right) -> n5.handle(left) [label="Step 2 OK"]
n5.handle(right) -> n6.handle(left)
n6.handle(right) -> n7.handle(left) [label="Yes"]
n6.handle(bottom) -> n8.handle(top) [label="No"]
n7.handle(right) -> n9.handle(left)
n8.handle(bottom) -> Compensation.n16.handle(top) [label="Rollback"]
n3.handle(bottom) -> OrderService.n10.handle(top) [label="Command"]
n4.handle(bottom) -> InventoryService.n12.handle(top) [label="Command"]
n5.handle(bottom) -> PaymentService.n14.handle(top) [label="Command"]
}
OrderService { # Order Service
n10: rectangle label:"Create Order Record"
n11: rectangle label:"Return Order ID"
n10.handle(right) -> n11.handle(left) [label="Persisted"]
n11.handle(top) -> Orchestrator.n4.handle(bottom) [label="Success"]
}
InventoryService { # Inventory Service
n12: rectangle label:"Reserve Stock Items"
n13: rectangle label:"Return Reservation ID"
n12.handle(right) -> n13.handle(left) [label="Reserved"]
n13.handle(top) -> Orchestrator.n5.handle(bottom) [label="Success"]
}
PaymentService { # Payment Service
n14: rectangle label:"Charge Payment Method"
n15: rectangle label:"Return Transaction ID"
n14.handle(right) -> n15.handle(left) [label="Charged"]
n15.handle(top) -> Orchestrator.n6.handle(bottom) [label="Result"]
}
Compensation { # Compensation Steps
n16: rectangle label:"Reverse Payment"
n17: rectangle label:"Release Inventory"
n18: rectangle label:"Cancel Order"
n19: circle label:"Saga Rolled Back"
n16.handle(right) -> n17.handle(left) [label="Refund"]
n17.handle(right) -> n18.handle(left) [label="Unreserve"]
n18.handle(right) -> n19.handle(left) [label="Cancelled"]
}

为什么需要这个工作流?

Distributed transactions across microservices cannot use traditional ACID transactions because each service owns its own database. The saga orchestration pattern uses a central coordinator to manage the transaction lifecycle, executing steps sequentially and triggering compensating actions when any step fails.

工作原理

  1. Step 1: The saga orchestrator creates a new saga instance and begins executing steps.
  2. Step 2: Step 1: The Order Service creates an order record and returns the order ID.
  3. Step 3: Step 2: The Inventory Service reserves stock items for the order.
  4. Step 4: Step 3: The Payment Service charges the customer payment method.
  5. Step 5: If all steps succeed, the saga is marked complete.
  6. Step 6: If any step fails, the compensation chain reverses all previously completed steps in reverse order.

替代方案

Two-phase commit provides strong consistency but blocks resources and does not scale. Choreography-based sagas avoid the central coordinator but are harder to monitor. This template shows the orchestration approach with explicit compensation.

Key Facts

Template NameSaga 编排模式架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

Saga 旅行预订架构

Architecture

旅行预订 Saga 架构图,将航班、酒店和租车预订编排为单一分布式事务,任何预订步骤失败时自动补偿取消所有预订。该模板模拟经典的 Saga 用例,多个独立服务必须全部成功或全部回滚,确保旅客不会出现部分预订。非常适合用真实业务场景演示 Saga 模式。

Saga 编舞模式架构

Architecture

Saga 编舞架构图,订单、支付和库存服务通过领域事件协调而无需中央编排器,每个服务发布和订阅驱动事务前进或触发补偿的事件。该模板模拟去中心化的 Saga 方法,服务自主响应事件,减少单点故障但增加了跟踪 Saga 状态的复杂性。适合偏好服务自治而非集中控制的团队。

Saga 订单履行架构

Architecture

订单履行 Saga 架构图,包含四个顺序步骤:客户验证、库存预留、支付授权和发货创建,以及在失败时反转已完成步骤的补偿链。该模板将端到端订单生命周期建模为 Saga,展示每个服务如何参与事务以及补偿操作如何维护数据一致性。适合设计可靠订单处理管道的电商架构师。

Saga 模式工作流

patterns

针对订单、支付和配送服务的 Saga 编排模式分布式事务工作流,在失败时自动触发补偿回滚。

微服务 API 网关架构

Architecture

微服务 API 网关架构图,展示请求路由、JWT 身份验证、速率限制、服务发现以及跨分布式后端服务的响应聚合。该模板模拟微服务生态系统中所有客户端流量的入口点,在请求到达内部服务之前执行安全策略。适合设计具有集中式横切关注点的可扩展 API 基础设施的平台工程师。

微服务每服务独立数据库架构

Architecture

每服务独立数据库架构图,每个微服务拥有其专用数据存储,通过 Kafka 进行事件驱动同步以实现跨服务数据一致性。该模板展示了微服务数据隔离的核心原则,展示 PostgreSQL 和 MongoDB 如何在多语言持久化策略中共存。对于在保持最终一致性的同时强制服务自治的架构师至关重要。

返回所有模板