Two-Phase Commit (Design Pattern of Distributed Systems)

The Two-Phase Commit (2PC) is a distributed consensus protocol used to ensure consistency across a distributed system when performing a transaction that spans multiple nodes. It operates in two phases: the Prepare Phase and the Commit Phase. Here’s a detailed explanation with examples:


1. Overview of the Two Phases

Prepare Phase (Voting Phase):

  1. The Coordinator (a central node managing the transaction) sends a Prepare message to all Participants (nodes involved in the transaction).
  2. Each participant performs any checks needed to ensure it can commit the transaction (e.g., acquiring locks or ensuring sufficient resources).
  3. Participants respond with either "Yes" (can commit) or "No" (cannot commit).

Commit Phase:

  1. If all participants respond with "Yes", the coordinator sends a Commit message to finalize the transaction.
  2. If any participant responds with "No", the coordinator sends an Abort message, rolling back any changes.
  3. Participants execute the corresponding action (commit or rollback) and confirm back to the coordinator.


Example: Distributed Bank Transfer

Imagine a scenario where a customer wants to transfer money between two bank accounts managed by different servers:

Prepare Phase:

  • Coordinator: Starts the transaction and sends a Prepare message to the two banks involved.
  • Bank A: Checks if sufficient funds exist in the source account and locks the amount. If successful, responds "Yes".
  • Bank B: Checks if the destination account is active and ready to receive funds. If successful, responds "Yes".

Commit Phase:

If both banks respond "Yes", the coordinator sends a Commit message:

  • Bank A: Deducts the amount and releases the lock.
  • Bank B: Credits the amount to the destination account.

If either bank responds "No", the coordinator sends an Abort message:

  • Bank A: Releases the lock without deducting funds.
  • Bank B: Takes no action.

This ensures either the entire transaction succeeds or it fails without partial updates, preserving consistency.


Benefits of 2PC

  • Guarantees atomicity (all-or-nothing) in distributed transactions.
  • Prevents data inconsistency in case of partial failures.


Drawbacks

  1. Blocking: If the coordinator crashes during the process, participants remain blocked, waiting for the coordinator's decision.
  2. Overhead: Multiple communication steps increase latency.
  3. Single Point of Failure: The coordinator is a bottleneck and a risk.


Variants

To address these issues, systems may use alternatives or enhancements such as:

  • Three-Phase Commit (3PC): Adds a pre-commit phase to minimize blocking.
  • Paxos/Raft: Distributed consensus algorithms that avoid the coordinator bottleneck.

Real-World Applications

  • Databases: Distributed databases like MySQL Cluster.
  • Microservices: Coordinating transactions across multiple services (e.g., in a distributed e-commerce system).

要查看或添加评论,请登录

Muhammad Bilal的更多文章

社区洞察

其他会员也浏览了