Il protocollo Two-Phase Commit
Comprenda il two-phase commit (2PC) per le transazioni distribuite, le sue garanzie, i suoi limiti dovuti al blocco e il motivo per cui i microservizi preferiscono invece le saga.
Il protocollo Two-Phase Commit è una lezione Microservices Communication Patterns (Saga, Circuit Breaker) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Microservices Communication Patterns (Saga, Circuit Breaker), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Microservices Communication Patterns (Saga, Circuit Breaker) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Domande Frequenti
La lezione «Il protocollo Two-Phase Commit» è gratuita?
Sì — il testo completo di «Il protocollo Two-Phase Commit» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Microservices Communication Patterns (Saga, Circuit Breaker), passa a CoddyKit PRO. Il corso Microservices Communication Patterns (Saga, Circuit Breaker) include 4 lezioni in totale.
Cosa imparerò in «Il protocollo Two-Phase Commit»?
Comprenda il two-phase commit (2PC) per le transazioni distribuite, le sue garanzie, i suoi limiti dovuti al blocco e il motivo per cui i microservizi preferiscono invece le saga. Eserciti Microservices Communication Patterns (Saga, Circuit Breaker) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Microservices Communication Patterns (Saga, Circuit Breaker)?
Non è richiesta alcuna esperienza precedente. Microservices Communication Patterns (Saga, Circuit Breaker) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Il protocollo Two-Phase Commit»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Microservices Communication Patterns (Saga, Circuit Breaker)?
Sì. Ogni lezione Microservices Communication Patterns (Saga, Circuit Breaker) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Principi ACID vs. BASE
- Comprendere la consistenza eventuale
- Gestione delle transazioni nei microservizi
- Il protocollo Two-Phase Commit