Data Consistency/

Two Phase Commit

Lesson overview

Two Phase Commit

Two-phase commit protocol for distributed consistency.

Two Phase Commit

Payment Service

Order Service

Inventory Service

create order

Deduct inventory

Process payment

Phase 1 — Prepare Phase

Phase 2 - Commit Phase

Case 1: Normal

Case 2: If One Service Says NO in Prepare Phase

then coordination service will rollback if there is some problem

Case 3: If One Service commit's fail

what to do commit or rollback?

Case 4: If coordinator crash

PROS: Strong Consitency Atomic Distributed Transaction

Single Point OF Failure infinite lock situation

CONS: Single point of failure Blocking problem poor scalability Long lock Duration

Two-Phase Commit (2PC) is a distributed transaction protocol that ensures all participating systems either commit the transaction together or roll it back together, maintaining consistency across multiple services or databases. It works in two phases: Prepare Phase – The coordinator asks all participants if they are ready to commit the transaction. Commit Phase – If all participants respond YES, the coordinator instructs them to commit; otherwise, it instructs them to roll back.

Loading Two Phase Commit