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

Phase 1: Prepare Phase

How the coordinator asks all participants to prepare to commit and wait for votes.

Phase 1: Prepare Phase

What you'll learn: How the coordinator orchestrates the first phase of Two-Phase Commit by asking all participants to prepare for a commit and waiting for their votes.

The Prepare Request

In the first phase of 2PC, the coordinator (the node managing the transaction) sends a PREPARE message to every participant node involved in the distributed transaction. This message essentially asks: "Can you commit this transaction if I tell you to?"

Think of it like a wedding ceremony where the officiant (coordinator) asks, "Does anyone object to this union?" Before proceeding, they need to hear from everyone present (participants).

What Participants Do

Each participant receives the PREPARE request and must:

  1. Check if it can commit — verify locks are held, constraints are satisfied, and resources are available
  2. Write the transaction to a durable log (so it can complete even after a crash)
  3. Vote YES or NO and send that vote back to the coordinator
  4. Lock resources — if voting YES, the participant promises not to roll back unilaterally

A YES vote is a binding promise. The participant is saying: "I guarantee I can commit this transaction if you ask me to, even if I crash and restart."

The Coordinator Waits

The coordinator collects votes from all participants. It must wait for every single participant to respond. If even one participant votes NO or doesn't respond (timeout), the coordinator will abort the transaction.

This is the critical decision point: proceed to commit phase only if unanimous YES votes arrive.

Key Takeaway: The Prepare Phase is where participants make binding promises to commit, logging changes durably and voting YES or NO. The coordinator collects all votes before deciding whether to proceed with the commit.