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
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.