Bird
Raised Fist0
DBMS Theoryknowledge~20 mins

Distributed transactions and 2PC in DBMS Theory - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Distributed Transactions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the Two-Phase Commit Protocol

What is the main purpose of the Two-Phase Commit (2PC) protocol in distributed transactions?

ATo speed up transaction processing by allowing partial commits in distributed systems.
BTo ensure all participating databases either commit or abort a transaction together, maintaining consistency.
CTo allow transactions to commit independently on each node without coordination.
DTo replicate data asynchronously across distributed databases.
Attempts:
2 left
💡 Hint

Think about how distributed systems keep data consistent when multiple databases are involved.

📋 Factual
intermediate
2:00remaining
Phases of the Two-Phase Commit Protocol

Which two phases correctly describe the Two-Phase Commit protocol?

AStart phase and End phase
BLock phase and Unlock phase
CVote phase and Execute phase
DPrepare phase and Commit phase
Attempts:
2 left
💡 Hint

One phase asks participants if they are ready, the other finalizes the decision.

🔍 Analysis
advanced
2:00remaining
Handling Coordinator Failure in 2PC

In a Two-Phase Commit protocol, what happens if the coordinator fails after sending the prepare message but before sending the commit or abort decision?

AParticipants remain blocked, waiting indefinitely for the coordinator's decision.
BParticipants automatically abort the transaction after a timeout.
CParticipants commit the transaction immediately after prepare phase.
DParticipants elect a new coordinator and proceed without delay.
Attempts:
2 left
💡 Hint

Consider what participants do when they don't receive the final decision after voting.

Comparison
advanced
2:00remaining
Comparing 2PC and 3PC Protocols

Which statement correctly distinguishes the Three-Phase Commit (3PC) protocol from the Two-Phase Commit (2PC) protocol?

A3PC adds an extra phase to avoid blocking participants during coordinator failure, unlike 2PC.
B3PC is faster than 2PC because it skips the prepare phase.
C2PC allows participants to commit independently, while 3PC requires strict coordination.
D2PC uses asynchronous messaging, whereas 3PC uses synchronous messaging only.
Attempts:
2 left
💡 Hint

Think about how protocols handle failures and participant blocking.

Reasoning
expert
2:00remaining
Impact of Network Partition on 2PC

During a distributed transaction using 2PC, a network partition occurs isolating some participants from the coordinator. What is the most likely outcome for the isolated participants?

AThey automatically abort the transaction after detecting the partition.
BThey elect a new coordinator and proceed with the transaction.
CThey remain in a prepared state, unable to commit or abort until the partition resolves.
DThey commit the transaction independently to avoid delays.
Attempts:
2 left
💡 Hint

Consider what participants do when they cannot communicate with the coordinator after voting.

Practice

(1/5)
1. What is the main purpose of the Two-Phase Commit (2PC) protocol in distributed transactions?
easy
A. To ensure all participating systems agree to commit or abort a transaction
B. To speed up transaction processing by skipping checks
C. To allow partial commits in case of failures
D. To encrypt data during transaction processing

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To ensure all participating systems agree to commit or abort a transaction -> Option A
  4. Quick Check:

    2PC ensures agreement = To ensure all participating systems agree to commit or abort a transaction [OK]
Hint: 2PC means all or nothing commit agreement [OK]
Common Mistakes:
  • Thinking 2PC speeds up transactions
  • Believing partial commits are allowed
  • Confusing 2PC with encryption
2. Which of the following is the correct sequence of phases in the Two-Phase Commit protocol?
easy
A. Commit phase followed by Prepare phase
B. Commit phase only
C. Abort phase followed by Prepare phase
D. Prepare phase followed by Commit phase

Solution

  1. Step 1: Recall the 2PC phases

    The protocol first asks participants to prepare (vote), then commits if all agree.
  2. Step 2: Match phases to options

    Prepare phase followed by Commit phase correctly lists Prepare phase first, then Commit phase.
  3. Final Answer:

    Prepare phase followed by Commit phase -> Option D
  4. Quick Check:

    2PC phases = Prepare then Commit [OK]
Hint: Prepare before commit in 2PC sequence [OK]
Common Mistakes:
  • Reversing the order of phases
  • Ignoring the Prepare phase
  • Thinking Commit happens alone
3. Consider a distributed transaction using 2PC with three participants: P1, P2, and P3. If P1 and P2 vote to commit but P3 votes to abort during the Prepare phase, what will be the final outcome?
medium
A. Only P1 and P2 commit, P3 aborts
B. All participants abort the transaction
C. All participants commit the transaction
D. Transaction is left in uncertain state

Solution

  1. Step 1: Understand voting in 2PC Prepare phase

    All participants must vote to commit for the transaction to proceed; any abort vote causes abort.
  2. Step 2: Apply voting results

    Since P3 votes to abort, the coordinator instructs all to abort to keep data consistent.
  3. Final Answer:

    All participants abort the transaction -> Option B
  4. Quick Check:

    Any abort vote = all abort [OK]
Hint: One abort vote cancels entire transaction [OK]
Common Mistakes:
  • Assuming partial commits are allowed
  • Thinking transaction stays uncertain
  • Ignoring abort votes
4. A distributed transaction using 2PC is stuck indefinitely in the Commit phase. What is the most likely cause of this problem?
medium
A. A participant failed to send its vote during the Prepare phase
B. All participants voted to abort during Prepare phase
C. The coordinator crashed after sending Commit messages but before receiving acknowledgments
D. The transaction was never started

Solution

  1. Step 1: Identify causes of blocking in Commit phase

    If the coordinator crashes after sending Commit but before acknowledgments, participants wait indefinitely.
  2. 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.
  3. Final Answer:

    The coordinator crashed after sending Commit messages but before receiving acknowledgments -> Option C
  4. Quick Check:

    Coordinator crash during Commit causes blocking [OK]
Hint: Coordinator crash after commit message causes blocking [OK]
Common Mistakes:
  • Confusing Prepare phase failures with Commit phase blocking
  • Assuming abort votes cause commit blocking
  • Ignoring coordinator role
5. In a distributed system using 2PC, how can the protocol be improved to avoid the blocking problem caused by coordinator failure during the Commit phase?
hard
A. Use a Three-Phase Commit protocol that adds a pre-commit phase
B. Skip the Prepare phase to speed up commits
C. Allow participants to commit independently without coordinator
D. Increase the timeout for participant responses

Solution

  1. Step 1: Understand blocking in 2PC

    2PC can block if coordinator fails after sending commit but before acknowledgments.
  2. Step 2: Identify protocol improvements

    Three-Phase Commit adds a pre-commit phase to reduce blocking by ensuring participants can safely decide without coordinator.
  3. Final Answer:

    Use a Three-Phase Commit protocol that adds a pre-commit phase -> Option A
  4. Quick Check:

    3PC adds pre-commit to avoid blocking [OK]
Hint: 3PC adds pre-commit phase to prevent blocking [OK]
Common Mistakes:
  • Skipping Prepare phase breaks consistency
  • Allowing independent commits causes inconsistency
  • Just increasing timeout doesn't fix blocking