LESSON 06 · Commit and coordination

Pick 2PC, consensus, or a service

Two-phase commit needs every participant to agree on one transaction, consensus needs only a majority, and a coordination service spares you from building it.

SYSTEM VIEW

The agreement path

Diagram view
Yes votes—
Seat 14—
Decision—

Watch a coordinator commit one purchase across two databases, then replay it with a no vote, a crash, and a coordinator that agrees by majority.

  1. A purchase debits $40 in Wallet DB and claims seat 14 in Seats DB: both commit, or neither. In two-phase commit, the coordinator asks each to prepare: store the change durably and promise to commit. Each holds its change and votes yes.

    Prepare
    store durably, promise
    Not the same as
    two-phase locking

    What changed

    • Wallet DB holds −$40
    • Seats DB holds seat 14
    • 2 yes votes
  2. Every vote is yes, so the coordinator durably records commit, and only then tells both. Each commits and releases its hold: the wallet is debited $40 and seat 14 is sold, both changes or neither.

    What changed

    • Commit logged
    • Wallet debited $40
    • Seat 14 sold
  3. Replay it with only $25 in the wallet. Seats DB holds seat 14 and votes yes, but Wallet DB can’t debit $40, so it votes no. The coordinator records abort and tells both, and Seats DB releases seat 14: neither change happens.

    Wallet
    $25, needs $40
    One no
    everyone aborts

    What changed

    • Wallet DB votes no
    • Abort logged
    • Seats DB’s hold on seat 14
  4. Replay it: both vote yes, then the coordinator crashes before sending its decision. Having promised, neither database may decide alone, since the coordinator might have chosen either way. Both wait in doubt, and seat 14 stays locked until the coordinator recovers. One unreachable database also prevents a commit.

    Coordinator down
    both wait
    One database unreachable
    no commit either

    What changed

    • Both vote yes
    • Coordinator crashes
    • Seat 14 locked, in doubt
  5. Consensus differs: a majority decides, so it carries on through a minority of failures. Spanner runs 2PC across Paxos groups. Replay it with the coordinator as a group of three: it stores commit on 2 of 3, its leader crashes, and another member sends the decision.

    2PC needs
    every participant
    Consensus needs
    a majority
    Decision stored on
    2 of 3

    What changed

    • Coordinator: 3 replicas
    • Commit stored on 2 of 3
    • The group’s leader
    • Seat 14 sold
  6. Don’t write your own consensus: etcd and ZooKeeper package it. Seats DB takes its primary’s lease from etcd, stored on a majority like every write, so keep etcd to leaders, locks and configuration, not business data or heavy traffic. A Postgres leases table can be enough.

    Use for
    leader, lock, config
    Not for
    business data, heavy traffic
    Already on Postgres
    a leases table

    What changed

    • etcd
    • Lease for Seats DB’s primary
    • Small data only

Space plays · ← → step · Home resets

THE IDEA

How it works

A purchase that debits a wallet in one database and claims a seat in another needs both to commit or neither. Two-phase commit (2PC) does this; it is unrelated to two-phase locking. A coordinator asks each participant to prepare, meaning to store the changes durably and promise it can commit. If all vote yes, the coordinator records the decision and tells them to commit. One no, and everyone aborts.

The weak point is the wait. Suppose both databases vote yes and the coordinator crashes before sending the decision. Neither may decide alone, because the coordinator might have decided either way, so seat 14 stays locked until it recovers. One unreachable participant also prevents a commit. Consensus differs: a majority decides, so it continues through a minority of failures. Spanner combines them, running 2PC across Paxos groups so that a coordinator’s decision survives its crash.

Coordination services package consensus for you. etcd and ZooKeeper offer small, strongly consistent stores with watches, leases or sessions, and ordering numbers usable as fencing tokens. Kubernetes keeps its cluster state in etcd. Use one to elect a leader, hold a lock, or publish shard ownership and configuration, not for business data or heavy traffic, since every write passes through a majority. Do not write your own consensus, because its bugs surface only under rare combinations of faults.

You may not need a service. If one Postgres primary is already a hard dependency, a leases table with a token column can be enough, and you accept that it is down when the primary is.

DECISION POINT

Checkout writes an order to Postgres and must publish an event to Kafka. A teammate suggests two-phase commit across both so they always agree. What do you do instead?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • 2PC needs every participant and can block if its coordinator fails; consensus needs only a majority.
  • Use etcd or ZooKeeper for small coordination data, not business records or heavy traffic.
  • Prefer a tested service, or your existing database, to writing your own consensus.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

A coordinator crashes after both shards vote yes. Forty minutes later, seat rows on one shard are still locked. Why can that shard not release them on its own?

Choose an answer. You’ll see the explanation once you get it right.

0 of 3 checkpoints answered correctly to complete this lesson.