Paxos algorithm detailed illustration

Source: Internet
Author: User
Tags zookeeper

1. Application of Paxos algorithm
Paxos algorithm and Variant algorithm are widely used in distributed systems.
Variants based on the Paxos algorithm are: ZAB, Raft
For example:
? The Zab protocol in Zookeeper is also a variant of the Paxos algorithm. The zookeeper provides data consistency through the ZAB protocol for data consistency.
? The strong consistency of metadata in each node is implemented by Paxos algorithm in Nutanix.
2, what is the Paxos algorithm
The problem with the Paxos algorithm is how to agree on a value in a distributed system where a message might be delayed, lost, duplicated, and that the consistency of the resolution will not be compromised regardless of any of the above exceptions.
This "value" may be a data, or a log, etc., depending on the application environment, this "value" is also different.

A typical scenario is that in a distributed database system, if the initial state of each node is consistent, each node executes the same sequence of operations, then they can finally get a consistent state. To ensure that each node executes the same sequence of commands, a "consistency algorithm" is executed on each instruction to ensure that the instructions seen by each node are consistent.
3, the principle of Paxos algorithm
For example, the company agreed on the location of the annual meeting and everyone could make recommendations. In a real world we can discuss in a conference room or discuss it in a group (based on memory sharing), but in a messaging-based distributed environment, everyone can only pass text messages to others. How to identify an annual meeting place in this delayed and lost environment;

The Paxos algorithm solves this problem:
1. Everyone can make suggestions, agree to suggestions, accept suggestions
2, the minority obey the majority. This recommendation can be determined as long as the proposal is approved by the majority.
The following discussion is determined as follows:
1, only the proposed proposal can be approved by everyone.
2, finally can only determine a proposal
3. If a person thinks that everyone agrees with a suggestion, then the proposal must really be approved by everyone.

Algorithm inference:
Situation one: What if there is only one person to make a suggestion?
If there is only one suggestion to be put forward then everyone must agree with this proposal, because if you disagree with this proposal you will not be able to identify a venue for the annual meeting.
So it concludes that:
P1: Everyone must agree with the first suggestion he received
Based on this conclusion, the following problems can occur:

Zhang San to Harry texting said: I suggest to Shanghai to hold the annual meeting!
Harry to John Doe texting said: I suggest to go to Guangzhou to hold the annual meeting!
John Doe to Zhang San texting said: I suggest to Beijing to hold the annual meeting!

According to P1: Everyone must agree with the first advice he receives, so the information Zhang San, John Doe and Harry eventually obtains is inconsistent.

So again, a proposal must be approved by most people to take effect.
Then a person can agree to multiple suggestions at the same time, and if one can agree to multiple proposals at the same time, the eventual Byzantine general problem may result in inconsistent results. (For example: Zhang San agreed to Beijing and agreed to the holding of Guangzhou, then John Doe will get 2 votes a vote of their own, one vote Zhang San. He will think he has the majority support, so he decided to go to Beijing eventually, the same Harry will also receive 2 votes, also think that everyone finally decided to hold in Guangzhou);

So to avoid this problem, a person will not have this problem if they agree to the same content in multiple proposals (the company's address).

The final negotiation result is that there are 2 votes in the same place, so that the final venue can be confirmed!
Then it leads to the conclusion that:
P2: Once a proposal has been agreed upon, the proposed company's annual meeting address must be in accordance with the agreed recommendations.
The question comes out: how to determine what is "before", What is "after"
Therefore, a number must be assigned to the proposal to establish a full-order relationship between the proposals.


Situation Two:
When the Zhang San, John Doe, Wang 53 people decided to finally hold the annual meeting in Zhengzhou. Zhao Liu, Sun 72 people because the cell phone has no power, did not receive notice, when they 2 people Zhao Liu to the Sun seven send SMS proposal to Hainan held, this proposal is sun Seven after the first received the proposal, according to P1 principle, he must agree with his first proposal received, so Sun Qi agreed to the annual meeting in Hainan. But this will lead to Sun VII and Zhang San, John Doe, Harry they determine the venue is inconsistent.



To avoid these problems. Specific instructions for the P2:
P2A: Once a proposal has been agreed upon, then the person who then agrees to the proposed company's annual meeting must have the same address.
In other words, the first proposal that Sun Qi agreed upon after the boot must be "held in Zhengzhou" will not appear inconsistent information. However, Sun Seven must accept the first proposal (P1 principle) after booting, and cannot interfere with the proposed content (the address of the company's annual meeting). So the best way to get Zhao Liu's proposal in some way is to have the same address as Zhang San and John Doe agreed (to Zhengzhou). So the first proposal that Sun Seven agreed on was "to be held in Zhengzhou"

We re-modified the P2A:
P2B: Once a proposal has been agreed upon, the person who followed it again suggests that the proposed company's annual meeting address must be the same as the address previously addressed by others.

How to make the new Zhao Liu proposed content must be the same as the Zhang San, John Doe, Harry discussion (to be held in Zhengzhou)?

We continue to make enhancements to P2B:
P2C: If there is a proposal with a number n that has V (the proposed content), then there is a majority, or none of them agree to any proposal with a number less than n, or they have agreed that the proposal with the largest number in the proposal with a number less than N has v.

To meet the requirements of the P2C, the proponents first have to communicate with the majority and obtain the proposal of their last consent before proposing. Then according to the feedback information to determine the content of this proposal, the formation of a proposal to start voting!

So the whole voting resolution is divided into two stages:
1. Preparation Stage
1. The proposed person chooses a number n and sends the preparation information to the majority.
2. If the recipient receives the preparation message, if the proposed number is greater than all the preparation information it has replied to. The recipient then responded to the proposal with the proposal that he had accepted last time and promised not to revert to the proposal less than N.
2. Consent phase
1. When a proposer receives a message from the majority, it enters the consent phase. It wants to send a request to the person who has given it information again to agree to the proposal. Contains the number n and the proposed content as determined by P2C (if there is no feedback on the proposed content they have accepted, then the proposed content is free to decide)
2. Without violating the promise made to others, the request is immediately accepted upon receipt of the proposal.

To illustrate:
Hypothesis: Only User1, User2, User3 Three people decide to be equal to a few!
1. Preparation Stage

1, User1 proposal number 1 and sent to User2 and User3.
Because User2 and User3 are based on P2C, they have not accepted a proposal that is smaller than the number 1. So they can accept the proposal and give User1 no longer accepting proposals less than number 1. Then User1 received a majority reply, will enter the 2nd stage. (If the received response does not form the majority, then it will go back to Phase 1)

2, User2 proposal number 2, and sent to User2 and User3.
Because User1 received the proposal for the first time and, according to P2C, it did not agree to a proposal less than 2, so it could accept the proposal. User3 has received the User1 number 1 proposal, but User2 's proposal number 2>1 so User3 can agree to the USER2 proposal, and feedback no longer accept less than 3 of the proposal. User2 also received a reply from the majority, which will proceed to phase 2nd.

3, User3 proposal number 3, and sent to User1 and User2.
Since User1 received the proposal >user2 number 2 for User3 number 3, it accepted the proposal of User3.
Since User2 received the proposal >user1 number 1 for User3 number 3, it accepted the proposal of User3.
To some User3 also received the majority reply, will carry on the 2nd stage.

Phase 2:
1, User1 sends the proposal with number 1, the proposal content is: 1+1=1; concurrent to User2 and User3.
Since User2 has stated that it will no longer accept proposals less than 3, it rejects the User1 proposal.
As User3 has stated that it will no longer accept proposals less than 2, it also rejects User1 's proposal.
The User1 offer was rejected by the majority, which again entered Stage 1.

2, User2 sends the proposal with number 2, the proposal content is: 1+1=2; concurrent send to User1 and User3
Since User1 has stated that it will no longer accept proposals less than 3, it rejects the user2 proposal.
Since User3 had stated that it would no longer accept proposals less than 2, the proposal was numbered =2 so User3 agreed to User2 's proposal.
But User2 did not get the majority's consent, so the same goes for stage 1.

3, User3 send the number! [] (http://i2.51cto.com/images/blog/201803/13/becfe18975159bd17b6a2d918b7d39d8.png?x-oss-process=image/ watermark,size_16,text_qduxq1rp5y2a5a6i,color_ffffff,t_100,g_se,x_10,y_10,shadow_90,type_zmfuz3pozw5nagvpdgk=) For the 3 proposal, the proposed content is: 1+1=3; and sent to User1 and User2;
As the User1 statement no longer accepted the proposal of less than 3, it agreed with User3 's proposal.
As the User2 statement no longer accepted the proposal of less than 3, it agreed with User3 's proposal.

At this point the final User3 can get the majority's consent.


Paxos algorithm diagram:

In the implementation environment, a proposal will be adopted to select a leader. All subsequent proposals can only be made by leader.


4. After-school thinking
Why are zookeeper nodes typically configured as 3, 5, 7 odd-numbered nodes?

Paxos algorithm detailed illustration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.