Course contentsShow
← System Design
Lesson 568 of 1,91916. Distributed TransactionsFree lesson

Two-Phase Commit (2PC) Overview

Introduction to 2PC as a protocol for coordinating atomic commits across distributed resources.

Two-Phase Commit (2PC) Overview

You'll learn: How 2PC coordinates atomic commits across multiple distributed nodes to maintain ACID guarantees.

The Core Problem

In a distributed transaction, you need multiple independent databases or services to either all commit their changes or all abort—no partial updates allowed. But how do you get everyone to agree when they can't communicate instantly and might fail mid-process?

How Two-Phase Commit Works

2PC solves this with a coordinator that orchestrates two distinct phases:

Phase 1: Prepare (Voting Phase)

The coordinator asks all participants: "Can you commit this transaction?" Each participant:

  • Checks if it can complete its part
  • Locks the necessary resources
  • Writes changes to a temporary log
  • Responds "yes" (prepared) or "no" (abort)

Phase 2: Commit (Decision Phase)

Based on the votes:

  • If everyone said "yes": Coordinator tells all participants to commit permanently
  • If anyone said "no": Coordinator tells everyone to abort

Think of it like planning a group vacation: First, you ask everyone "Can you come?" (prepare). Only if everyone confirms do you book the flights (commit). If even one person can't make it, nobody books anything (abort).

The Guarantee

2PC ensures atomicity—either all participants commit or none do. No participant commits until the coordinator confirms unanimous agreement.

The Cost

The protocol requires multiple network round-trips and participants must hold locks during the entire process, blocking other operations and creating potential for delays.

Key Takeaway: Two-Phase Commit uses a coordinator and two-round voting protocol to achieve atomic commits across distributed resources, trading performance for strong consistency guarantees.