Two-stage processing
medium 30 minTwo-stage processing, known as two-phase commit (2PC), makes several databases commit or abort together.
Video lesson · Chapter 1 of 5
Three vaults must agree on one transfer
Theory
Read or listen first. About 2 min.
What it is
Two-phase commit is a protocol for one transaction that touches several machines. A coordinator runs it, and each database is a participant. In phase 1 the coordinator asks: can you commit? In phase 2 it announces the decision, and everybody follows it.
The rule is simple. If all participants vote yes, the decision is commit. If even one votes no, or does not answer in time, the decision is abort. A short version: first ask everyone whether they can commit, then tell everyone to commit or to abort.
Why it exists
If you just send "commit" to three databases, one may crash or refuse while the others have committed. Nobody can take that back. Asking first removes that risk, because a yes vote is a promise: I have saved everything I need, and I can commit even after a crash.
The promise has a price. Between the yes vote and the decision, a participant may not change its mind. It keeps its locks and waits. If the coordinator disappears in that moment, the participant is in doubt and blocked.
A short history
1978
Jim Gray's "Notes on Data Base Operating Systems" appears in Lecture Notes in Computer Science 60 and describes two-phase commit.
8 November 2005
PostgreSQL 8.1 is released and adds two-phase commit, so that a transaction can be prepared on several computers.
2006
Jim Gray and Leslie Lamport publish "Consensus on Transaction Commit", which uses consensus so that one failed coordinator cannot block the decision.
2012
Google's Spanner paper appears. Its documentation describes two-phase commit for writes that touch several splits.
Words to know
- Coordinator
- The process that collects votes and decides.
- Participant
- A database or service that takes part and votes.
- Prepare
- Save enough to be able to commit or abort later, even after a crash.
- Vote
- Yes is a promise; no is a veto.
- In doubt
- Voted yes, and does not know the decision yet.
- Write-ahead log
- Records that are on disk before the change counts.
- XA
- A technical standard for distributed transactions between a transaction manager and databases.
How it works
Step through the idea, or press Play.
Prepare. Ask everyone. Each saves its work, locks it and votes
Understand it
Start here
Ready? Pull!
A crew pulls heavy stones with a rope. If only two people pull, the rope burns their hands. So the captain always asks first, and calls "pull" only after hearing yes from everybody.
The last picture is the weakness of the whole protocol.
The idea
Parts and properties
One coordinator, three participants
Atomic
All or nothing over all participants.
Phase 1
Prepare and vote.
Phase 2
Commit or abort.
Coordinator
One place that decides.
Blocking
Participants may wait with locks held.
Logging
Votes and decisions survive a crash.
Phase 1
Prepare and vote
| Coordinator | Participant |
|---|---|
| Makes a transaction id | Takes the locks it needs |
| May log that it is preparing | Runs the work locally |
| Sends PREPARE to all | Writes undo and redo records and the prepare record to its log |
| Waits for all votes, with a timeout | Votes yes (ready) or no (cannot) |
Everyone says yes
Phase 2
Commit or abort
All voted yes
- Coordinator logs COMMIT
- It sends COMMIT to all
- Participants make the change permanent
- They free their locks
- They send ACK
Any no, or silence
- Coordinator decides ABORT
- It sends ABORT to all
- Participants roll back using their log
- They free their locks
- They send ACK
One says no
Timeline
| Moment | What happens |
|---|---|
| Start | The client begins the transaction |
| Prepare | The coordinator sends PREPARE |
| Vote | Participants vote yes or no |
| Decision | The coordinator decides commit or abort and logs it |
| Execute | Participants commit or roll back |
| Complete | All acknowledged; the coordinator may forget |
Trouble
The blocking case
How it gets stuck
- 1
PREPARE is sent
All participants receive it.
- 2
Everyone votes yes
Locks are held.
- 3
The coordinator logs COMMIT
The decision exists in one place only.
- 4
It crashes before telling anyone
No participant has heard the decision.
- 5
Participants wait
They cannot commit, because it may be abort. They cannot abort, because it may be commit.
They are stuck until the coordinator comes back or someone who knows the decision answers. That may be seconds or hours. Guessing breaks atomicity.
May I decide alone?
Four problems
| Problem | Why | Impact |
|---|---|---|
| Blocking | Locks are held while waiting for the decision | A coordinator crash after the votes blocks other work |
| Single point of failure | One coordinator decides | The transaction hangs until it recovers |
| Network partition | Some participants cannot be reached | Before the votes: abort. After them: wait |
| Performance | Two rounds and several disk flushes | More latency than a local commit, and locks held longer |
Recovery
What the logs are for
The coordinator restarts
| Last record | Action |
|---|---|
| No decision | Abort: nobody can have committed |
| COMMIT | Send COMMIT again until all acknowledge |
| ABORT | Send ABORT again until all acknowledge |
| Done | Nothing |
States of a participant
Numbers
What it costs
Messages
How long is a lock held?
Everyone must be up
Alternatives
Other ways to agree
| Way | Idea | Good | Bad |
|---|---|---|---|
| Three-phase commit | Adds a pre-commit phase | Does not block for some crashes; participants may time out | Not safe in a network partition, more complex |
| Saga | Local transactions with compensations | No distributed locks, available | Eventual consistency, undo code |
| Try, Confirm, Cancel | Reserve first, then confirm or cancel | Business-level, flexible | Needs idempotency, more code |
| Events | Publish and react | Loosely coupled, scales | Eventual consistency, harder to debug |
| 2PC over consensus | Each participant and the coordinator is a replicated group | One machine cannot block it | More messages, a special database |
What to use when
When to use it
Use cases
Good fit
- A transfer between two databases that must be atomic
- Stock and order rows in two databases of one team
- One transaction over several databases
- XA transactions between a database and a message broker that support it
Avoid it
- Systems that must stay available: blocking reduces it
- Many participants: one failure is likely
- Across distant data centres: latency stretches the locks
- Long transactions: locks are held too long
Do
- Keep the decision log durable
- Set a timeout for votes
- Make commit and abort idempotent
- Use a write-ahead log in every participant
- Monitor prepared transactions that are stuck
Do not
- Use it where availability comes first
- Include many participants
- Run it across far regions without need
- Forget that the coordinator can fail
- Hold locks while a person thinks
Summary
The whole topic in one minute.
Two-phase commit makes several participants commit or abort as one. Phase 1 collects votes, phase 2 spreads the decision. Commit needs every vote to be yes. One no, or one silence, means abort.
The protocol is safe, but it can block. A participant that voted yes keeps its locks until it learns the decision. Use it for short work over a few databases that are close together, replicate the coordinator's log, and watch for transactions stuck in doubt. For long processes over services, use a saga.
- Phase 1 asks and collects votes; phase 2 spreads the decision.
- Commit needs every yes. One no or one silence aborts.
- A yes vote is a promise that survives a crash.
- In doubt means blocked: wait for the decision, never guess.
- Short work, few participants, close together. Otherwise use a saga.