分布式事务的 Saga 模式
学习如何使用 Saga 模式和补偿操作,在不采用分布式事务的情况下协调多个服务之间的数据变更。
分布式事务的 Saga 模式 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless AWS Lambda Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless AWS Lambda Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Distributed Data Problem
In event-driven systems each service owns its own data. A single business operation may span several services, but there is no shared transaction to roll them all back.
What is a Saga?
A saga breaks one big transaction into a sequence of local transactions. If a step fails, earlier steps are undone with compensating actions.
A Worked Example
Placing an order: reserve inventory, charge payment, create shipment. If payment fails, you must release the reserved inventory.
Compensating Actions
Every forward step needs a matching undo. Reserve has Release; Charge has Refund. Compensations make the system eventually consistent.
steps = [
('reserveInventory', 'releaseInventory'),
('chargePayment', 'refundPayment'),
('createShipment', 'cancelShipment')
]Choreography Style
In choreography, each service reacts to events and emits the next event. There is no central coordinator, but the flow is implicit and harder to follow.
Orchestration Style
In orchestration, a coordinator (often AWS Step Functions) explicitly drives each step and triggers compensations on failure. The flow is centralized and visible.
Step Functions Fits Sagas
Step Functions supports a Catch on each task to invoke a compensation, making it a natural home for orchestrated sagas.
{
"ChargePayment": {
"Type": "Task",
"Resource": "arn:...:chargePayment",
"Catch": [{"ErrorEquals": ["States.ALL"], "Next": "ReleaseInventory"}],
"Next": "CreateShipment"
}
}Idempotency is Essential
Saga steps and compensations may be retried, so each must be idempotent. Use a unique transaction id to detect and ignore duplicates.
Eventual Consistency
During a saga the system is temporarily inconsistent. Design the UI and downstream consumers to tolerate states like "payment pending".
Semantic Locks
To avoid conflicts mid-saga, mark records with a pending status (a semantic lock) so other operations know the data is in flight.
When to Use a Saga
Use sagas when a workflow truly spans services. If everything lives in one service or one database, a normal local transaction is simpler and safer.
Quick Check
Test your saga knowledge.
Recap
You learned the saga pattern: split a distributed operation into local steps with compensating undos, choose choreography or orchestration, and keep steps idempotent.
常见问题解答
「分布式事务的 Saga 模式」课时是免费的吗?
是的 — 「分布式事务的 Saga 模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。
「分布式事务的 Saga 模式」这节课中我会学到什么?
学习如何使用 Saga 模式和补偿操作,在不采用分布式事务的情况下协调多个服务之间的数据变更。 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Serverless AWS Lambda Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「分布式事务的 Saga 模式」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?
能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 构建事件驱动的微服务
- 与 Amazon EventBridge 集成
- 使用 Kinesis 进行实时处理
- 分布式事务的 Saga 模式