Distributed Systems Design Pattern: Two-Phase Commit (2PC) for Transaction Consistency [Banking Multi-Account Transfers Use?Case]
Shanoj Kumar V
VP - Senior Technology Architecture Manager @ Citi | LLMs, AI Agents & RAG | Cloud & Big Data | Author
The Two-Phase Commit (2PC) protocol is a fundamental distributed systems design pattern that ensures atomicity in transactions across multiple nodes. It enables consistent updates in distributed databases, even in the presence of node failures, by coordinating between participants using a coordinator node.
In this article, we’ll explore how 2PC works, its application in banking systems, and its practical trade-offs, focusing on the use case of multi-account money transfers.
The Problem:?
In distributed databases, transactions involving multiple nodes can face challenges in ensuring consistency. For example:
Example Problem?Scenario
In a banking system, transferring $1,000 from Account A (Node 1) to Account B (Node 2) requires both accounts to remain consistent. If Node 1 successfully debits Account A but Node 2 fails to credit Account B, the system ends up with inconsistent account balances, violating atomicity.
Two-Phase Commit Protocol: How It?Works
The Two-Phase Commit Protocol addresses these issues by ensuring that all participating nodes either commit or abort a transaction together. It achieves this in two distinct phases:
Phase 1:?Prepare
Phase 2: Commit or?Abort
Problem Context
Let’s revisit the banking use case:
领英推荐
Prepare Phase:
Commit Phase:
Fault Recovery in Two-Phase Commit
What happens when failures occur?
Practical Considerations & Trade-Offs
Advantages:
Challenges:
The Two-Phase Commit Protocol is a robust solution for achieving transactional consistency in distributed systems. It ensures atomicity and consistency, making it ideal for critical applications like banking, where even minor inconsistencies can have significant consequences.
By coordinating between participant nodes and enforcing consensus, 2PC eliminates the risk of partial updates, providing a foundation for reliable distributed transactions.
#BeIndispensable #DistributedSystems