DisSysLab · Class 7

The Paxos Algorithm: Consensus

Serializing Transactions

Let's look at the specification of the consensus problem again.

The distributed consensus problem: Each agent in a system proposes a value; the problem is to develop an algorithm by which all agents agree on a single value chosen among the proposed values. The algorithm has the following requirements:

  1. Validity The value that the agents agree on must be a value proposed by at least one of the agents.
  2. Agreement All agents must agree on the same value.
  3. Termination Every agent must eventually decide: it computes a consensus value, and never changes it afterwards.

Specification of the Paxos Algorithm Paxos is a distributed consensus algorithm that guarantees validity and agreement, but not termination.

The Paxos algorithm was constructed by Leslie Lamport.

The FLP theorem says that distributed consensus is impossible in a system where messages are arbitrarily slow and an agent may halt. This class describes the Paxos algorithm in which agents may never decide on a consensus value. The algorithm does guarantee validity and agreement. If agents decide on a value then (consensus) all agents decide on the same value and (validity) that value is one of the proposed values.

The Fault Model

The Paxos fault model allows more faults than the FLP theorem assumes. Messages may get lost, duplicated, delayed for arbitrary durations, and delivered out of order. An arbitrary number of agents may halt (not just one) as FLP assumes. Agents may halt and then restart, and agents may operate at arbitrary speeds.

We begin by describing serializability of transactions in Paxos and then describe the Paxos algorithm.

Why serializability Serializability of transactions allow us to prove properties of a distributed algorithm, Paxos, by proving properties of a sequential for loop. Proving correctness is easier for sequential programs than for concurrent programs.

Serializability of transactions

Consider a system that executes transactions - at this point it doesn't matter what a transaction is. Each step in a computation is associated with a single transaction.

The transactions in a computation are serializable exactly when the events (vertices) of the dataflow graph of the computation can be partitioned into a sequence of sets: past_j, j= 0, 1, .., N … where N is the number of transactions, and past_0 is the empty set, and for all j < N:
  1. past_(j+1) consists of past_j and all the events of exactly one transaction.
  2. past(j) is a consistent cut of the dataflow graph.

The figure below shows a computation with two transactions blue and red. The color of each event identifies the transaction to which the event belongs. A dotted message line indicates a message that is sent but is lost. Lost messages play no part in a computation and are ignored when determining whether a computation is serializable.

The transactions in this computation are serializable because the events can be partitioned into sets past_0, past_1, past_2 where past_1 consists of all blue steps and past_2 consists of all blue and red steps.

Figure 1. Serializable transactions. Both computations have the same dataflow graph. Press Play: the red events slide to the right of the blue ones, and the top computation becomes the lower one. Each agent keeps its own steps in the same order; only the order of steps at different agents changes.

The computation in the top diagram has steps of the two transactions interspersed — some red steps occur before some blue steps and some blue steps occur before some red steps.

The lower diagram is obtained from the top diagram by permuting steps so that all blue steps occur before all red steps. The order of steps between different agents is permuted, but the order of steps at each agent is the same in both graphs. The curvilinear cuts of the top computation are vertical lines in the lower computation.

The next two figures show transactions that are not serializable. If past_1 consists of all blue steps and no red ones, then there is an edge from the future to the past. Similarly, the for the case where past_1 consists of all red steps and no blue ones.

Example 1: Transactions that are not serializable

Figure 2. Example 1: not serializable. Agent E takes a blue step, then a red one, then a blue one again. Press past = blue steps: the third step of E is in the past while the second is in the future, so the stop sign appears on E between them. Press past = red steps and the sign appears between E’s first two steps for the same reason. Whichever colour goes first, an edge of E’s own timeline runs from the future into the past.

Example 2: Transactions that are not serializable

Figure 3. Example 2: not serializable. Agent A runs a red step before a blue one, and agent B runs a blue step before a red one. Press past = blue steps: A’s blue step is in the past while its red step is in the future, so the stop sign appears between them. Press past = red steps and the sign moves to agent B, for the same reason. Neither order gives a cut.

Theorem

A sufficient condition for serializability of transactions is as follows.
  1. Associated with each transaction is a unique id where transaction ids are totally ordered.
  2. For each agent, the sequence of events at that agent are in nondecreasing order of transaction ids.

The proof that these conditions are sufficient for serializability is that the computation can be partitioned into a sequence of consistent cuts where each cut separates steps of transactions with smaller ids from steps of transactions with larger ids.

In figure 1, the blue transaction has a smaller id than the red transaction, and in the computation of each of the five agents: all blue events occur before all red events. In figure 2 and 3, there are some agents with computations in which red events occur before blue, and there are other agents with computations in which blue events occur before red.

A protocol for serializable transactions

Each transaction is associated with a transaction id which is a pair: (n, X) where n is a number and X is agent the agent that starts the transaction. Assume that agents are totally ordered; so transaction ids are totally ordered with (n, X) < (m, Y) if n < m or if n = m and X < Y.

Rule 1 An agent executes at most one transaction at a time. When an agent starts a new transaction it sets the id of the new transaction to be greater than the id of transactions that it has executed.

Figure 4. Transaction ids along one agent. A diagram showing a computation of a single agent. For agent ids: A < B < C. The agent first takes steps of transactions with id (10, A), then (10, B), then (15, A), then (20, C).

Rule 2 When an agent executing a transaction with id T sends a message it appends T to the message.

Rule 3 An agent executing a transaction with id T ignores a message with transaction id T' if T' < T; the message is treated as lost.

Example Assume transaction ordering: green -> blue -> red

Figure 5. A message from an earlier transaction. Each agent’s line is colored by the transaction it is executing. The green message arrives at agent A while A is executing the blue transaction. A ignores the message and continues executing the blue transaction; A treats the green message as lost. Press Accept the green message to see what rule 3 is for: the step that receives it would be a green step sitting between two blue ones, so agent A’s transaction ids would run blue, green, blue and the computation would no longer be serializable.

Rule 4 When an agent executing a transaction with id T receives a message in a transaction with id T' where T' > T, the agent stops executing the transaction with id T and starts executing the transaction with id T'.

Example Transaction ordering, green -> blue -> red

Figure 6. A message from a later transaction. The message from the red transaction arrives at agent A while A is still executing the green transaction. Press Apply rule: A starts executing the red transaction, and the step that receives the message is a red step.

Observation The four rules guarantee that transactions are serializable.

Proof: Transaction ids for successive events at an agent are nondecreasing, and messages sent in a transaction with id T are received in a transaction with id T. Define past as the set of events with transaction id less than or equal to T. Then all edges to past are from past.

Consequence of serializability

When transactions of a computation are serializable, the dataflow graph is one in which transactions are executed sequentially. So, the computation of each agent is the same as one obtained by executing the following loop where L is the list of transaction ids in increasing order.

for i in L: execute transaction with id i
Figure 7. What serializability gives us. The diagram shows a computation with five agents and four transactions — blue, red, green, magenta — with their events interspersed. Messages are not shown. Press Play. While the dataflow graph remains unchanged, the events slide into four consecutive blocks, one transaction at a time. Blocks are separated by consistent cuts.

Instead of studying computations in which events from different transactions may be interspersed we need only study computations in which transactions are executed sequentially. We can determine the state of each agent after each step in any computation by determining the state in the corresponding sequential computation.

Figure 8. The states of one agent. Figure 7 again, with the boundaries between transactions drawn as thin lines, and the states of agent A marked: s0 is A’s state before its first event, and s_k is its state after its k-th event. The marks are there before Play and after it. Press Play: the events slide into transaction order and each mark slides with them, staying between the same two events of A. A’s events are the same events in the same order before and after, so its states are the same states in the same order. Permuting the events of different agents changes nothing that A holds, and the same argument holds for B, C, D and E.

Next: the Paxos algorithm