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

Saga 分布式事务架构

Architecture

分布式事务架构图,实现两阶段提交协议,事务协调器向参与服务发送准备和提交消息,任何投票失败时执行全局中止。该模板可视化经典的 2PC 协议,用于多个服务间需要强一致性的场景,展示准备、投票和提交/中止阶段。对于理解微服务架构中分布式共识权衡至关重要。

完整 FlowZap 代码

Coordinator { # Transaction Coordinator
n1: circle label:"Begin Distributed Transaction"
n2: rectangle label:"Assign Transaction ID"
n3: rectangle label:"Send Prepare to All"
n4: rectangle label:"Collect Votes"
n5: diamond label:"All Voted Commit?"
n6: rectangle label:"Send Global Commit"
n7: rectangle label:"Send Global Abort"
n8: circle label:"Transaction Complete"
n1.handle(right) -> n2.handle(left)
n2.handle(right) -> n3.handle(left) [label="TxID"]
n3.handle(bottom) -> ServiceA.n9.handle(top) [label="Prepare"]
n3.handle(bottom) -> ServiceB.n12.handle(top) [label="Prepare"]
n3.handle(bottom) -> ServiceC.n15.handle(top) [label="Prepare"]
n4.handle(right) -> n5.handle(left)
n5.handle(right) -> n6.handle(left) [label="Yes"]
n5.handle(bottom) -> n7.handle(top) [label="No"]
n6.handle(right) -> n8.handle(left)
n7.handle(right) -> n8.handle(left) [label="Rolled Back"]
n6.handle(bottom) -> ServiceA.n11.handle(top) [label="Commit"]
n6.handle(bottom) -> ServiceB.n14.handle(top) [label="Commit"]
n6.handle(bottom) -> ServiceC.n17.handle(top) [label="Commit"]
}
ServiceA { # Booking Service
n9: rectangle label:"Lock Resources"
n10: rectangle label:"Vote Commit"
n11: rectangle label:"Apply Changes"
n9.handle(right) -> n10.handle(left) [label="Prepared"]
n10.handle(top) -> Coordinator.n4.handle(bottom) [label="Vote"]
}
ServiceB { # Payment Service
n12: rectangle label:"Hold Funds"
n13: rectangle label:"Vote Commit"
n14: rectangle label:"Capture Payment"
n12.handle(right) -> n13.handle(left) [label="Prepared"]
n13.handle(top) -> Coordinator.n4.handle(bottom) [label="Vote"]
}
ServiceC { # Notification Service
n15: rectangle label:"Queue Notification"
n16: rectangle label:"Vote Commit"
n17: rectangle label:"Send Notification"
n15.handle(right) -> n16.handle(left) [label="Prepared"]
n16.handle(top) -> Coordinator.n4.handle(bottom) [label="Vote"]
}

为什么需要这个工作流?

When strong consistency is absolutely required across multiple services (e.g., financial transactions), eventual consistency is not acceptable. The two-phase commit protocol ensures all participants either commit or abort together, providing ACID-like guarantees across distributed services at the cost of availability during the prepare phase.

工作原理

  1. Step 1: The transaction coordinator assigns a unique transaction ID and sends prepare messages to all participants.
  2. Step 2: Each participant locks its resources and votes commit or abort.
  3. Step 3: The coordinator collects all votes and makes a global decision.
  4. Step 4: If all participants voted commit, a global commit message is sent.
  5. Step 5: If any participant voted abort, a global abort message is sent to all.
  6. Step 6: Participants apply or rollback changes based on the global decision.

替代方案

Saga patterns provide better availability but only eventual consistency. Consensus protocols like Raft/Paxos are used for state machine replication. This template shows the classic 2PC protocol for understanding distributed consensus trade-offs.

Key Facts

Template NameSaga 分布式事务架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

Saga 编排模式架构

Architecture

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

Saga 编舞模式架构

Architecture

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

Saga 旅行预订架构

Architecture

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

Saga 订单履行架构

Architecture

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

微服务 API 网关架构

Architecture

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

微服务服务网格架构

Architecture

服务网格架构图,展示 Istio 或 Linkerd 边车代理处理 mTLS 加密、流量策略、熔断器和跨微服务的分布式追踪。该模板可视化服务网格如何将网络关注点从应用代码中抽象出来,实现服务间的零信任通信。对于采用服务网格基础设施以提升可观测性和安全性的团队至关重要。

返回所有模板