What if your money transfer got lost halfway because systems didn't talk to each other?
Why Distributed transactions and 2PC in DBMS Theory? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are managing money transfers between two different bank accounts located in separate systems. You try to update both accounts manually, one after the other, without any coordination.
This manual approach is risky because if one update succeeds and the other fails, the money could disappear or be duplicated. It's slow and error-prone since there's no guarantee both systems agree on the changes.
Distributed transactions with Two-Phase Commit (2PC) solve this by coordinating all systems to agree before making changes permanent. This ensures either all updates happen together or none do, keeping data consistent.
update accountA set balance = balance - 100; update accountB set balance = balance + 100;
begin transaction; prepare transaction; commit transaction if all agree; rollback if any fail;
It enables reliable and consistent updates across multiple systems, even when they are separate and independent.
When you transfer money between banks, distributed transactions ensure your money leaves one account and appears in the other without loss or duplication.
Manual updates across systems can cause errors and data loss.
Distributed transactions coordinate all systems to agree before committing.
Two-Phase Commit ensures all-or-nothing updates for consistency.
Practice
Solution
Step 1: Understand the role of 2PC in distributed systems
2PC coordinates multiple systems to either all commit or all abort a transaction, ensuring consistency.Step 2: Analyze the options
Only To ensure all participating systems agree to commit or abort a transaction correctly describes 2PC's goal of agreement before finalizing changes.Final Answer:
To ensure all participating systems agree to commit or abort a transaction -> Option AQuick Check:
2PC ensures agreement = To ensure all participating systems agree to commit or abort a transaction [OK]
- Thinking 2PC speeds up transactions
- Believing partial commits are allowed
- Confusing 2PC with encryption
Solution
Step 1: Recall the 2PC phases
The protocol first asks participants to prepare (vote), then commits if all agree.Step 2: Match phases to options
Prepare phase followed by Commit phase correctly lists Prepare phase first, then Commit phase.Final Answer:
Prepare phase followed by Commit phase -> Option DQuick Check:
2PC phases = Prepare then Commit [OK]
- Reversing the order of phases
- Ignoring the Prepare phase
- Thinking Commit happens alone
Solution
Step 1: Understand voting in 2PC Prepare phase
All participants must vote to commit for the transaction to proceed; any abort vote causes abort.Step 2: Apply voting results
Since P3 votes to abort, the coordinator instructs all to abort to keep data consistent.Final Answer:
All participants abort the transaction -> Option BQuick Check:
Any abort vote = all abort [OK]
- Assuming partial commits are allowed
- Thinking transaction stays uncertain
- Ignoring abort votes
Solution
Step 1: Identify causes of blocking in Commit phase
If the coordinator crashes after sending Commit but before acknowledgments, participants wait indefinitely.Step 2: Analyze options
The coordinator crashed after sending Commit messages but before receiving acknowledgments matches this scenario; other options relate to earlier phases or no transaction.Final Answer:
The coordinator crashed after sending Commit messages but before receiving acknowledgments -> Option CQuick Check:
Coordinator crash during Commit causes blocking [OK]
- Confusing Prepare phase failures with Commit phase blocking
- Assuming abort votes cause commit blocking
- Ignoring coordinator role
Solution
Step 1: Understand blocking in 2PC
2PC can block if coordinator fails after sending commit but before acknowledgments.Step 2: Identify protocol improvements
Three-Phase Commit adds a pre-commit phase to reduce blocking by ensuring participants can safely decide without coordinator.Final Answer:
Use a Three-Phase Commit protocol that adds a pre-commit phase -> Option AQuick Check:
3PC adds pre-commit to avoid blocking [OK]
- Skipping Prepare phase breaks consistency
- Allowing independent commits causes inconsistency
- Just increasing timeout doesn't fix blocking
