Two-Phase Commit(2PC) — Distributed Design Patterns

The Two-Phase Commit protocol is a distributed algorithm that ensures that a transaction is either committed or rolled back consistently across all nodes in a distributed system. The protocol involves two phases, as the name suggests.

Phase 1 — Prepare Phase: The coordinator node sends a prepare message to all participating nodes, asking them if they are ready to commit the transaction. Each participant acquires a “lock” on the resource/s and replies with either a Yes or No message, indicating whether they can commit.

Phase 2 — Commit Phase: The coordinator decides whether to commit or abort the transaction based on the responses received in the Prepare phase. If all participants have responded with a Yes message, the coordinator sends a commit message to all the participants. If any participant has responded with a No message, the coordinator sends an abort message to all the participants, and the transaction is rolled back.

2 Phase Commit

Advantages of Two-Phase Commit Protocol

  1. Consistency: 2PC guarantees that either all participating nodes commit the transaction or all of them roll it back, ensuring consistency across the distributed system.
  2. Atomicity: The 2PC protocol ensures that the transaction is an atomic operation, meaning it either…

--

--

Pratik Pandey - https://pratikpandey.substack.com

Senior Engineer with experience in designing and architecting large scale distributed systems.