DisSysLab

The Paxos Algorithm

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.

Specification

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.

The algorithm

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.

Proposers and acceptors

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.

Figure 1. The network. The proposers and acceptors of figure 2. Each agent has one outbox (filled) and one inbox (open). Every proposer’s outbox is connected to every acceptor’s inbox, and every acceptor’s outbox is connected to every proposer’s inbox: thirty connections in all. No proposer is connected to a proposer and no acceptor to an acceptor. Press p0, p1 or p2 to see one proposer’s connections on their own.

An outline of the algorithm

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.

  1. Select transaction id and proposer: Select a transaction id T that is larger than the ids of all previous transactions. Select a proposer p that starts executing a new transaction with id T.
  2. Proposer reads acceptor variables: p reads the local variable r.v of acceptor r in a set R of acceptors. R is the set of acceptors whose replies reach p.
  3. Proposer writes acceptor variables: If the size of R is at least M, p computes a value d from the values of acceptor local variables, r.v, that p read in this transaction. p assigns d to local variable, q.v, of acceptor q, for all q in a set W of acceptors. W is the set of acceptors that p’s writes reach.

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]
Transaction
This step
Consensus
Figure 2. Five transactions, one step at a time. Five acceptors a0 … a4 and three proposers p0, p1, p2 proposing green, blue and red. A majority is three. Each transaction takes three steps: the proposer reads the acceptors in R, computes f, and writes the acceptors in W. The first two transactions read fewer than three acceptors and write nothing. The third writes green to a0. The fourth reads a majority that includes a0, so f returns green although p1 proposes blue, and it writes green to a majority — from that point the theorem below applies with s* = green and t* = 30. The fifth reads a majority that includes one of those acceptors, so p2 writes green too, though it proposes red. Notice a4: no transaction ever writes it, so it holds (null, 0) to the end. An acceptor need never be assigned a value.

Properties of the algorithm

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.

Theorem: Candidate value assigned to a majority in a transaction remains unchanged

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'.

Learners

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.

Transaction
This step
Learners
Figure 3. What the learners hear. Figure 2 again, with two learners below the acceptors. Whenever an acceptor is assigned a value it sends the pair it now holds to both learners, and some of those messages are lost — a lost one is dashed and crossed out. A learner learns a value when it has heard the same pair from a majority of acceptors, and it never changes it afterwards. Both learners start out holding null. At T = 30 all three messages reach L0, so L0 learns green; one of them is lost on the way to L1, which hears only two and stays null. At T = 41 three messages reach L1, and L1 learns green too — the same value, later.

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.

Determining consensus on sequences of proposals

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

  1. Proposer 0's proposal is the consensus. Balance is $100
  2. Then: Proposer 1's proposal is the consensus. Balance is $5

A problem arises if the withdrawal is made before the deposit.

  1. Proposer 1's proposal is the consensus. An overdraft penalty of $5 is charged, so the balance is -$100.
  2. Then: Proposer 0's proposal is the consensus. Balance is $0.

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.

Code

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.