35 / 35 · Extensions
Consensus and distributed transactions
Compare agreement within a replica group with a commit decision across two participants, then follow each through a failure.
The shop needs two guarantees. Copies of a stock database must preserve the same sequence of committed operations. A checkout that uses a separate payment database must also reach one decision about the stock change and the payment. The first problem concerns copies of one state machine. The second concerns several participants with different work to perform.
Replicas agree on an order of commands#
Consensus lets cooperating nodes decide one value despite some failures. A replicated log uses repeated agreement to choose a sequence of commands. In a three-node Raft group, call the replicas A, B and C. A majority contains two nodes. Two majorities must overlap, since two disjoint pairs would require four nodes. Raft uses this intersection together with terms, log matching and a voting restriction that checks a candidate’s log. The intersection alone does not implement the protocol. Ongaro and Ousterhout, 2014, §§5.2–5.4
Suppose A leads in term 8 and persists a new reservation command in that term. B persists the entry. A has the entry on two replicas including itself. Raft’s current-term rule permits the leader to advance its commit index. It applies the committed entry before returning the operation’s result. If A later becomes unreachable, a future elected leader must contain the committed entry. A single isolated node cannot commit another command on its own. Counting replicas of an entry from an older term is insufficient for the same conclusion. Ongaro and Ousterhout, 2014, §§5.3–5.4
Participants have to be able to commit#
A checkout transaction may involve a stock participant and a payment participant. Each has different constraints. A majority of some group cannot vote a payment into existence if the payment participant cannot accept it. The commit rule requires every participant to be prepared.
In two-phase commit (2PC), the coordinator first asks each participant to prepare. A participant that answers yes durably records that it can later commit and retains the resources needed to do so. If all participants answer yes, the coordinator can record a commit decision durably and tell each participant to commit. A failure to prepare requires abort. The participants execute the final decision once they learn it. Gray and Lamport, 2006, §3
Preparation precedes the global decision
Suppose stock has voted yes and then loses contact with the coordinator. The coordinator might have recorded commit and reached the payment database, or it might have stopped before recording a decision. The prepared stock participant cannot safely choose abort from the timeout alone. It must learn the decision or use the protocol’s recovery rules. This is a blocking failure mode of classic 2PC. In PostgreSQL, PREPARE TRANSACTION persists a prepared transaction for an external transaction manager and the prepared transaction can continue holding locks. Gray and Lamport, 2006, §3 PostgreSQL 18, PREPARE TRANSACTION
Combine mechanisms at the correct boundary#
A consensus-backed decision service can survive some coordinator failures. It still needs a valid vote from every participant before committing. Conversely, 2PC by itself supplies no replicated log for an individual database. These guarantees compose when each protocol has a defined role and all participating writes follow it. An email sent by an external service is outside the database transaction unless that service provides an explicitly coordinated effect.
Next#
Return to Architecture and cost to apply these choices. A requirement should name which writes need one decision and how long the system can wait for it when a participant is unavailable.
References#
- Diego Ongaro and John Ousterhout. In Search of an Understandable Consensus Algorithm. USENIX ATC, 2014, extended version, §§5.2–5.4. Defines leader election, commit and the restrictions that preserve committed log entries.
- Jim Gray and Leslie Lamport. Consensus on Transaction Commit. ACM TODS, 31(1), 133–160, 2006. §3 describes classic 2PC and the paper compares it with a consensus-based commit protocol.
- PostgreSQL Global Development Group. PostgreSQL 18, PREPARE TRANSACTION, 2025. Describes the prepared state and its resources.