二相コミットプロトコル
分散トランザクションのための二相コミット(2PC)について、その保証、ブロッキングという弱点、そしてマイクロサービスで代わりにSagaが好まれる理由を理解します。
「二相コミットプロトコル」はCoddyKit上の無料Microservices Communication Patterns (Saga, Circuit Breaker)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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
よくある質問
「二相コミットプロトコル」レッスンは無料ですか?
はい。「二相コミットプロトコル」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Microservices Communication Patterns (Saga, Circuit Breaker)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Microservices Communication Patterns (Saga, Circuit Breaker)コースには全4レッスンが含まれています。
「二相コミットプロトコル」で何を学びますか?
分散トランザクションのための二相コミット(2PC)について、その保証、ブロッキングという弱点、そしてマイクロサービスで代わりにSagaが好まれる理由を理解します。 ブラウザで直接実行するハンズオンコードでMicroservices Communication Patterns (Saga, Circuit Breaker)を演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。