← Lessons

quiz vs the machine

Gold1450

System Design

Two Phase Commit for Payments

A coordinator that votes then commits across stores to make a payment atomic.

5 min read · core · beat Gold to climb

The goal

When a payment touches two stores, such as a wallet and a ledger, two phase commit makes the change atomic: both commit or neither does.

The two phases

  • Prepare: a coordinator asks each participant to do the work and lock resources, then vote yes or no.
  • Commit: if all voted yes, the coordinator tells everyone to commit; if any voted no, it tells everyone to abort.

The cost

  • Participants hold locks between prepare and commit, hurting throughput.
  • If the coordinator crashes after prepare, participants are blocked waiting for the decision.

When to use it

  • Use it when both stores share an environment and strong consistency matters more than availability.
  • For cross service payments that span networks, prefer a saga instead.

Key idea

Two phase commit gives atomic payments across stores through a vote then commit protocol, at the cost of locks and blocking if the coordinator fails.

Check yourself

Answer to earn rating on the learn ladder.

1. What happens in the prepare phase?

2. What is the main weakness of two phase commit?