2PC (Two-Phase Commit)
Two-Phase Commit (2PC) is a distributed transaction protocol that guarantees atomicity across multiple nodes. Either all nodes commit the transaction, or none of them do.
Two phases
Prepare (voting) — the coordinator sends Prepare to all participants. Each checks whether it can execute the transaction and replies Yes or No
Commit/Abort — if all replied Yes → coordinator sends Commit. If any replied No → Abort
Problems
Blocking — if the coordinator fails after Prepare but before Commit, participants are stuck in uncertainty
Performance — two network round-trips; holds locks between phases
Alternative
Saga Pattern — abandons 2PC in favour of compensating transactions and Eventual Consistency. More practical for microservices.