Das Two-Phase-Commit-Protokoll
Verstehen Sie Two-Phase Commit (2PC) für verteilte Transaktionen, seine Garantien und blockierenden Schwächen sowie die Gründe, warum Microservices stattdessen Sagas bevorzugen.
Das Two-Phase-Commit-Protokoll ist eine kostenlose Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Microservices Communication Patterns (Saga, Circuit Breaker)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Häufig gestellte Fragen
Ist die Lektion „Das Two-Phase-Commit-Protokoll“ kostenlos?
Ja — der vollständige Text von „Das Two-Phase-Commit-Protokoll“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Microservices Communication Patterns (Saga, Circuit Breaker)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Das Two-Phase-Commit-Protokoll“?
Verstehen Sie Two-Phase Commit (2PC) für verteilte Transaktionen, seine Garantien und blockierenden Schwächen sowie die Gründe, warum Microservices stattdessen Sagas bevorzugen. Du übst Microservices Communication Patterns (Saga, Circuit Breaker) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Microservices Communication Patterns (Saga, Circuit Breaker) zu starten?
Keine Vorkenntnisse erforderlich. Microservices Communication Patterns (Saga, Circuit Breaker) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Das Two-Phase-Commit-Protokoll“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion Code schreiben und ausführen?
Ja. Jede Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- ACID- vs. BASE-Prinzipien
- Eventual Consistency verstehen
- Transaktionsverwaltung in Microservices
- Das Two-Phase-Commit-Protokoll