What if your system freezes waiting for one slow part to say yes or no?
Why Two-phase commit (and why to avoid it) in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a small shop with two cash registers. When a customer buys something, both registers must record the sale. You try to coordinate by calling each register to confirm the sale before finalizing it.
This manual coordination is slow and confusing. If one register confirms but the other doesn't, you don't know what to do. Sometimes sales get lost or recorded twice, causing mistakes and unhappy customers.
Two-phase commit is like a strict manager who first asks all registers if they are ready to record the sale, then tells them all to finalize it together. This keeps data consistent but can slow everything down and cause delays if one register is slow or broken.
call register1 to save sale call register2 to save sale if both success then confirm else rollback
start transaction prepare register1 prepare register2 if all prepared then commit else abort
It ensures all parts agree on a change or none do, keeping data perfectly in sync across systems.
In a bank, transferring money between accounts in different systems needs two-phase commit to avoid losing or creating money by mistake.
Manual coordination causes delays and errors.
Two-phase commit guarantees consistency but can slow systems and cause blocking.
Understanding its limits helps design better, faster systems.
Practice
two-phase commit protocol in microservices?Solution
Step 1: Understand the role of two-phase commit
Two-phase commit is designed to make sure all parts of a distributed transaction agree to commit or abort together.Step 2: Identify the main goal in microservices
Its main goal is to keep data consistent across multiple services by coordinating their commit decisions.Final Answer:
To ensure all services agree on a transaction before committing -> Option DQuick Check:
Two-phase commit = agreement before commit [OK]
- Thinking it speeds up communication
- Believing services act independently
- Assuming it retries failed requests automatically
Solution
Step 1: Recall the two phases names and order
The first phase is the prepare phase where the coordinator asks all services if they can commit.Step 2: Understand the commit phase
If all agree, the coordinator sends a commit command to finalize the transaction.Final Answer:
Prepare phase where coordinator asks, Commit phase where services finalize -> Option BQuick Check:
Prepare then commit = correct phase order [OK]
- Mixing up the order of prepare and commit phases
- Confusing abort with prepare phase
- Thinking services finalize before coordinator asks
Solution
Step 1: Analyze failure during prepare phase
If any service fails to respond or votes no during prepare, the coordinator must abort to keep consistency.Step 2: Understand coordinator's action
The coordinator sends abort commands to all services to rollback any partial changes.Final Answer:
The coordinator aborts the transaction and tells all services to rollback -> Option CQuick Check:
Failure in prepare = abort transaction [OK]
- Assuming commit happens despite failure
- Thinking coordinator retries forever
- Ignoring failure and proceeding anyway
Solution
Step 1: Identify cause of delays and hangs
In two-phase commit, the coordinator waits for all services to respond during prepare phase.Step 2: Understand impact of crashed services
If a service crashes, the coordinator may wait indefinitely, causing delays and system hangs.Final Answer:
The coordinator is waiting indefinitely for responses from crashed services -> Option AQuick Check:
Waiting on crashed service = system hang [OK]
- Thinking services commit too fast causes hangs
- Believing skipping prepare phase causes delays
- Assuming missing logs cause system hangs
Solution
Step 1: Understand drawbacks of two-phase commit
Two-phase commit blocks resources while waiting, reducing system availability and scalability.Step 2: Recognize why modern systems avoid it
Modern microservices prefer eventual consistency and non-blocking patterns to improve performance and fault tolerance.Final Answer:
Because it causes blocking, reduces availability, and hurts scalability -> Option AQuick Check:
Blocking and low availability = avoid two-phase commit [OK]
- Thinking it does not guarantee consistency
- Believing it requires no coordination
- Assuming it is too simple
