Time：2021-10-25

# Graphical distributed consistency protocol Paxos

Paxos protocol / algorithm is an important protocol in distributed system. How important is it?

Mike burrows, author of Google chubby, said that there is only one consistency algorithm in the world, that is Paxos, and other algorithms are defective.

After understanding these two distributed protocols (Paxos / 2pc), learning other distributed protocols will become quite easy.

Learning Paxos algorithm has two parts: a) principle / proof of algorithm; b) Understanding / operation of algorithms.

Understanding the operation process of this algorithm can basically be used in engineering practice. And it’s much easier to understand this process.

On the Internet, I think Paxos belongs to this article:Paxos diagramandPaxos algorithm details, I’ll combine hereInstances on WikiFurther elaboration. Some Paxos basics are basically understandable through the two articles mentioned here and the content on the wiki.

## Algorithm content

Paxos in the original author’s Paxos made simple is relatively concise:

Phase 1

(a) A proposer selects a proposal number n and sends a prepare request with number n to a majority of acceptors.

(b) If an acceptor receives a prepare request with number n greater than that of any prepare request to which it has already responded, then it responds to the request with a promise not to accept any more proposals numbered less than n and with the highest-numbered pro-posal (if any) that it has accepted.

Phase 2

(a) If the proposer receives a response to its prepare requests (numbered n) from a majority of acceptors, then it sends an accept request to each of those acceptors for a proposal numbered n with a value v , where v is the value of the highest-numbered proposal among the responses, or is any value if the responses reported no proposals.

(b) If an acceptor receives an accept request for a proposal numbered n, it accepts the proposal unless it has already responded to a prepare request having a number greater than n.

borrowPaxos diagramThe flow chart can be summarized as follows:

## Examples and detailed explanations

There are three types of roles in PaxosProposerAcceptorandLearner, the main interaction process isProposerandAcceptorbetween.

ProposerAndAcceptorThere are mainly four types of message communication, as shown in the following figure:

These four types of messages correspond to two stages and four processes of Paxos algorithm:

• phase 1

• a) The proposer sends to more than half of the acceptors in the networkpreparenews
• b) The acceptor replies normallypromisenews
• phase 2

• a) When enough acceptors reply to the promise message, the proposer sendsacceptnews
• b) Normally, the acceptor repliesacceptednews

There are several operations that require special attention——preparepromiseacceptaccepted, onlyacceptedThe message is really accepted. Even if there is a larger K, the value will still be taken at this timeacceptedValue of.

Because other proposers may send the above requests for the same thing in the whole process, some special cases will be handled in each process, which is also done to reach agreement. If there is no other proposer to compete in the whole process, the result of this operation is no objection. But if there are other proposers, the situation is different.

withExamples on Paxos Chinese wikiFor example. In short, this example uses several members to propose taxes to determine the tax proportion of the bill finally passed.

The following figure basically only shows the interaction between the proposer and an acceptor. The time flag T2 is always after T1. Proposal number is abbreviated as n.

One of the situations is shown in the figure below:

A3 sent at T1acceptedGive A1 and then receive A5 at T2prepare, A1 only notifies A5 of the final result (tax rate 10%) at T3. There are two situations:

• If N5 sent by A5 is less than N1 sent by A1, A3 directly rejects A5
• If N5 sent by A5 is greater than N1 sent by A1, A3 replies promise, but carries A1 (N1, 10%)

This can correspond to Paxos flow chart for better understanding.Acceptor will record (maxn, acceptn, acceptv)

A5 after receiving promise, the subsequent process can proceed smoothly. However, when you send accept, because you receive (acceptn, acceptv), you will take the acceptv corresponding to the largest acceptn, that is, 10% of A1 in the example as the acceptv.

If no other recorded acceptv is found when receiving promise, its value can be determined by yourself.

In view of the above conflict between A1 and A5, both A1 and A5 will broadcast the accepted value of 10%.

In fact, for acceptors in the four processes, when replying to promise and accepted, special treatment may be caused due to the intervention of other proposers. So basically, you can understand the whole algorithm when you receive requests from other proposers at these two time points. For example, when replying to promise, you may reject because the N sent by the proposer is not large enough:

If promise has been sent to other proposers with larger n when sending the accepted message, the accept sent by the proposer will also be rejected, as shown in the figure:

This corresponds to phase 2 (b):

it accepts the proposal unless it has already responded to a prepare request having a number greater than n.

## summary

Leslie Lamport did not describe Paxos mathematically, but he explained it clearly in English. Understand the contents of the two phases of Paxos clearly, and the whole algorithm process is not complicated.

As for how to implement a globally unique and increasing proposer number mentioned in Paxos, please refer to the following:

How to generate a unique number? In Paxos made simple, it is mentioned that all proposers can be selected from disjoint data sets. For example, if the system has five proposers, each proposer can be assigned an identification J (0 ~ 4), and the number of each proposer’s resolution can be 5 * I + J (I can be used to indicate the number of proposals)