The Paxos algorithm is a distributed consensus algorithm created by Leslie Lamport. The FLP theorem (see Distributed Consensus) says no distributed consensus algorithm in an asynchronous system can guarantee validity, agreement and termination if a single agent may halt. Paxos guarantees validity and agreement, but not termination.
A set of agents called proposers propose values for a consensus. A set of agents called learners determine the consensus value. Associated with each proposer p is a constant p.VALUE, and each learner L has a local variable L.value.
(L.value = null) OR
(exists proposer p where: L.value = p.VALUE).(L.value = null) OR
(L′.value = null) OR
(L.value = L′.value).The Paxos Algorithm: Serializing Transactions described a model of transactions in a computation and gave sufficient conditions for transactions to be serializable. Serializability allows us to convert a system in which messages may be lost, duplicated and delivered out of order, and in which agents may halt, into a sequential program.
We describe the Paxos algorithm in two parts. The first part deals with a network of two layers of agents: proposers that propose values to be considered for consensus, and acceptors that receive proposals and carry out computations that lead to a consensus.
Proposers send messages to, and receive messages from, acceptors. Acceptors send messages to, and receive messages from, proposers. Proposers do not communicate directly with each other. Likewise acceptors do not communicate directly with each other. In our model each proposer and each acceptor has a single outbox and a single inbox. (Later we will add a second outbox for acceptors, but that outbox plays no part in the first part of the algorithm.)
Associated with each proposer p is a local constant p.VALUE
which is the value that p proposes as a candidate for consensus.
Each acceptor q has a local variable q.v = (q.v.s, q.v.t): q.v.s is a
candidate consensus value, and q.v.t is the id of the transaction that
last assigned it. Initially q.v = (null, 0).
Let N be the number of acceptors, and let M acceptors be a majority.
M = floor(N/2) + 1.
The algorithm executes transactions in increasing order of transaction id. A transaction consists of the following steps in which T, p, R, and W are determined nondeterministically.
The transaction R and W are nondeterministic because messages can be lost: any acceptor’s reply, and any write, may fail to arrive.
if len(R) >= M: d = f(p, r.v for r in R) for q in W: q.v.s, q.v.t = d, T
def f(p, list_of_rv): if all(rv.s == null for rv in list_of_rv): # no acceptor holds a candidate: propose p's own value return p.VALUE else: # return the candidate value that was assigned most recently among # the acceptors that were read: the rv.s with the largest rv.t t_max = max(rv.t for rv in list_of_rv) # every rv with rv.t == t_max was written by the same transaction, # so they all hold the same value: any one of them will do r_s_list = [rv.s for rv in list_of_rv if rv.t == t_max] return r_s_list[0]
Acceptors only accept values proposed by proposers: For all acceptors q: (q.v.s = null) OR (exists proposer p: q.v.s = p.VALUE) Why: f returns either p.VALUE or a value that an acceptor already holds.
Monotone nondecreasing acceptor timestamps: If in a transaction q.v.t = t* then in all subsequent transactions: q.v.t >= t*. Why: transactions are executed in increasing order of id, and a write in the transaction with id T sets q.v.t = T.
If the set W* of acceptors assigned values in a transaction with id t* is a
majority, and s* is the candidate value assigned in the transaction,
then after the transaction with id t*, and after every later transaction,
for all q: (q.v.s = s*) OR (q.v.t < t*).
Proof by induction Base case — The condition holds after the transaction with id t*: the acceptors in W* hold (s*, t*), and every other acceptor was last assigned in an earlier transaction, so its q.v.t < t*.
Induction step: Assume that the condition holds after a transaction with id t >= t* and show that it holds after the next transaction with id t' > t.
If len(R) < M then acceptor values are not modified in the transaction with id t', and so the condition continues to hold.
If len(R) >= M then there is at least one element common to R and W*, because two majorities must have an element in common. Let r be an acceptor in R and in W*. Then r.v.t >= t*, because of monotone nondecreasing timestamps. So t_max >= r.v.t >= t*.
For all q: (q.v.s = s*) OR (q.v.t < t*). Therefore, if q.v.t = t_max then q.v.s = s*. And the value returned by function f is s*.
So every acceptor in W is assigned (s*, t'), and every other acceptor is unchanged. The condition holds after the transaction with id t'.
The acceptors know collectively when a majority of them reach a consensus. How can a learner know if a majority has reached consensus?
When an acceptor q changes its value it sends a message containing q.v to all learners. If L.value is null and L has received the same pair q.v from at least M different acceptors, L sets L.value = q.v.s.
Messages to learners can be lost, so a learner may never learn a value: L1 in figure 3 learns only because a later transaction writes the same value again. This is why Paxos does not guarantee termination.
The proof of correctness is as follows.
Validity: An acceptor q assigns only proposer values to q.v.s, and so L.value is only assigned proposer values.
Agreement: If L learns (s, t), then M acceptors reported (s, t), so the transaction with id t assigned s to a majority of acceptors. If another learner L′ learns (s′, t′) with t <= t′, then the transaction with id t′ also assigned s′ to a majority, and by the theorem s′ = s.
Stability: L sets L.value only while L.value is null, so it changes L.value at most once.
The Paxos algorithm that we described enables learners to determine a single consensus value. In many problems, consensus has to be reached on a sequence of values. Determining a consensus on an account ledger illustrates the problem.
Consider the following sequence of deposits and withdrawals from an account which has $0 initially. Proposer 0 proposes "Deposit $100 in the account." Proposer 1 proposes "Withdraw $95", presumably expecting the deposit to be made first. So the sequence of operations on the consensus ledger could be
A problem arises if the withdrawal is made before the deposit.
We want a consensus on the sequence of operations on the ledger.
The algorithm can be extended from determining consensus on a single value to a sequence of values as follows. Learners send sequences that they learn to proposers. A proposer's proposal extends the sequence that the proposer knows is in the consensus followed by a single additional proposed value. A proposer that reads q.v.s = sequence_0 and u.v.s = sequence_1 in the same transaction, treats q.v.s as null if the length of sequence_0 is less than that of sequence_1. We omit the proof that this rule preserves the theorem. A simpler alternative, used in practice, runs a separate instance of Paxos for each position in the ledger.
Deriving the code for proposers and acceptors from the sequential
algorithm is straightforward; see
the paxos office.
The office differs from figure 1 in one detail: an acceptor’s reply
goes only to the proposer that asked, so in the office each acceptor has
one outbox per proposer, and one more for the learners. Its
office.md explains why.