两阶段提交协议
了解用于分布式事务的两阶段提交(2PC)、它提供的保证及阻塞方面的弱点,以及微服务为何更倾向于使用 Saga。
两阶段提交协议 是 CoddyKit 上的免费 Microservices Communication Patterns (Saga, Circuit Breaker) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Microservices Communication Patterns (Saga, Circuit Breaker) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Microservices Communication Patterns (Saga, Circuit Breaker) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Need for Atomic Distributed Writes
When one business action spans two databases, you want all-or-nothing: both commit or both roll back. A single local transaction cannot span separate systems, so we need a coordination protocol.
Enter Two-Phase Commit
Two-phase commit (2PC) is a classic protocol where a coordinator drives multiple participants to a unanimous decision: commit or abort.
Phase 1: Prepare
The coordinator asks every participant: can you commit? Each does the work, locks the resources, writes to its log, and replies yes (prepared) or no.
Phase 2: Commit or Abort
If all said yes, the coordinator tells everyone to commit. If anyone said no, it tells everyone to abort. The decision is final and recorded.
The Guarantee
2PC provides atomicity across participants: there is no state where one committed and another aborted, as long as the protocol completes.
The Blocking Problem
Here is the fatal flaw: if the coordinator crashes after phase 1, participants are stuck holding locks, unable to decide. They block indefinitely until the coordinator recovers.
Locks Kill Throughput
Resources stay locked for the whole protocol round-trip. Across services and networks this dramatically reduces concurrency and availability, which is unacceptable for high-scale microservices.
The CAP Tension
2PC favors consistency over availability. A network partition during commit can freeze the whole transaction, violating the availability that distributed systems usually prioritize.
XA Transactions
The XA standard implements 2PC across resource managers (databases, JMS brokers). It works, but it tightly couples services and inherits all of 2PC's blocking issues.
Why Microservices Avoid 2PC
Microservices prize loose coupling and availability. They generally reject 2PC in favor of the saga pattern: a sequence of local transactions with compensating actions, accepting eventual consistency instead of distributed locks.
When 2PC Still Makes Sense
2PC is not always wrong. Within a single trust boundary, low-volume, short transactions over reliable infrastructure (e.g. one DB plus one message broker) can use it safely.
Quick Check
Test your 2PC understanding.
Recap
You learned 2PC:
- 2PC coordinates an all-or-nothing commit across participants
- Phase 1 prepares (locks and votes); phase 2 commits or aborts
- It guarantees atomicity but blocks on coordinator failure
- Locks and the CAP trade-off hurt availability at scale
- Microservices prefer sagas; 2PC suits small, reliable, single-boundary cases
常见问题解答
「两阶段提交协议」课时是免费的吗?
是的 — 「两阶段提交协议」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Microservices Communication Patterns (Saga, Circuit Breaker) 课程的其余内容,请升级到 CoddyKit PRO。 Microservices Communication Patterns (Saga, Circuit Breaker) 课程共包含 4 节课。
「两阶段提交协议」这节课中我会学到什么?
了解用于分布式事务的两阶段提交(2PC)、它提供的保证及阻塞方面的弱点,以及微服务为何更倾向于使用 Saga。 你通过在浏览器中直接运行的动手代码来练习 Microservices Communication Patterns (Saga, Circuit Breaker),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Microservices Communication Patterns (Saga, Circuit Breaker) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Microservices Communication Patterns (Saga, Circuit Breaker) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「两阶段提交协议」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Microservices Communication Patterns (Saga, Circuit Breaker) 课中编写并运行代码吗?
能。每节 Microservices Communication Patterns (Saga, Circuit Breaker) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。