Paxos algorithm 1-algorithm Formation Theory

Source: Internet
Author: User

The difficulty in understanding paxos algorithms is as admirable as the algorithm's popularity. From my personal experience, the reason is not that the algorithm is too advanced to have insufficient IQ, lamport is too obscure to express this algorithm and lacks a complete application scenario. If the master can express the algorithm in another way, it may be easier to accept:

  • First, the application scenarios of algorithms are proposed to provide a case that most readers can understand.
  • Next, describe how the paxos algorithm solves this problem.
  • Again give the origin of the algorithm (that is, the analogy and algorithm process of the Greek city-state)

Lamport first proposed the origin of the algorithm. Without any assistance, many people have been stuck in the quandary and cannot continue to access the specific content of the Algorithm without any questions, it is hard to understand the essence of algorithms. In this article, we will redescribe the paxos Algorithm in another way.

All our descriptions assume that the reader is familiar with Lamport's paxos-simple article, so he will not explain the concepts.

In addition to Lamport several papers, paxos algorithm description is concise Chinese article: http://zh.wikipedia.org/zh-cn/Paxos%E7% AE %97%E6%B3%95

The translation of this article is in place, but there are still some ambiguity and incorrect understanding of the original text in key details, which may lead to readers more confused about the paxos algorithm, however, you can quickly get a rough idea of the paxos algorithm.

1. application scenarios

(1) Consistency in distributed architecture

The paxos algorithm mainly solves the consistency problem. For "consistency", there are different interpretations in different scenarios:

  • Nosql: consistency emphasizes "Reading New writes", that is, read and write consistency.
  • Database field: consistency emphasizes that "all data statuses are consistent". After a transaction, if the transaction succeeds, all table data is operated according to the SQL statement in the transaction, the addition, deletion, and modification cannot be modified. The deletion is not deleted. If the transaction fails, all data is in the initial state;
  • State Machine: the consistency in the state machine emphasizes that the State must be consistent after a series of commands are executed on each state machine with the same initial state, that is, the sequence consistency. This is the case for paxos algorithm consistency. We will discuss this scenario further.

(2) MQ

Assume that all system log information is written to an MQ server, then, each log command is asynchronously sent to multiple log servers through MQ to write files (the reason for writing multiple log servers is to back up the log files to prevent data loss ), then the data on all log servers must be consistent (the log Content and order are identical), because MQ itself has the sorting function, and as long as the Q data is entered, there will be order, it is equivalent to a globally unique number. No matter how many files are written into the data, the content of each file must be consistent as long as it is numbered, but an MQ server is obviously a single point, if the system goes down, the availability of the entire system is affected.

(3) Multiple MQ

To solve the MQ single point of failure, the preferred solution is to use multiple MQ servers, that is, using one MQ cluster, the client can access any MQ server, and different clients may access different MQ servers, the data content and sequence on different MQ servers may be inconsistent. If this problem is not solved, the content written to log server by each MQ server is inconsistent, which is obviously not the expected result.

(4) data update in nosql

Generally, nosql ensures its availability by means of Data Replication. However, when the client operates on multiple data, there may be one or several servers sent by many operations on the same data, possible execution: insert, update a, update B .... update N. This update operation must be performed on the server after an insert operation is performed multiple times in a row, if the update sequence received by each replication server is inconsistent due to thread pool, network, server resources, and other reasons, the replication data on the other side will become meaningless, in the financial sector, it may even cause serious consequences.

These inconsistencies are solved by the paxos algorithm. Of course, these problems are not only solved by paxos. These problems have also been solved before paxos is available, for example, MQ in dual-master mode is used to solve the single point of MQ and nosql Replication through master server. However, these solutions have some defects or are difficult to scale horizontally, or affect availability. Of course, in addition to the paxos algorithm, there are other algorithms also trying to solve such problems, such as viewstamped replication algorithm.

The common characteristics of the above scenarios are that the status of multiple servers is consistent, that is, consistency. Let's look at the introduction of Chinese wiki:

In a distributed database system, if the initial status of each node is the same, and each node executes the same operation sequence, they can finally get a consistent state. To ensure that each node executes the same command sequence, a "consistency algorithm" must be executed on each command to ensure that the commands seen by each node are consistent.

You may have a deeper understanding of this description.

2. How does paxos solve such problems?

Paxos tries to globally number the status of each server to solve this type of problem. If the number can be successful, all operations are performed in the serial order, and consistency is self-evident. When the server in the cluster receives some data, how do I perform serial numbers? Is to vote, let all servers vote, to see which server on which data should be ranked first, which is ranked second ..., as long as most servers agree to the number of rows for a certain data, the number of rows will be reached.

Obviously, in order to give each data unique number, only one data can be generated for each vote. Otherwise, the vote has no meaning. The paxos algorithm focuses on how to generate only one data in one vote. Furthermore, we call the voting data value. The core and essence of the paxos algorithm is to ensure that only one value is generated for each voting.

3. paxos Algorithm

We will supplement the concept of the original article:

  • Promise: acceptor promises to Proposer. If there is no larger proposal, it will accept the proposal it submitted
  • Accept: If the acceptor does not find a proposal with a higher number than the previous proposal, it approves the proposal.
  • Chosen: when the majority of the acceptor has an accept proposal, the proposal is finally selected, also known as the resolution.

That is to say, the acceptor has two actions for Proposer: Promise and accept.

The following explanations mainly focus on"Only a single value is chosen,

",

Let's look at the following condition P1,

P1: an acceptor must accept the first proposal that it has es.

At first glance, this condition is obvious, because there is no value before, the acceptor should naturally be the first proposal in accept, but think about it, I feel that the condition P1 is not strict, is it a simple description of the problem or a strict mathematical condition? These questions come down to two problems:

(1) What is this condition essentially?

(2) What should I do with the second proposal?

In subsequent algorithms, it is not related to whether an acceptor approves a value and whether it is the first one, but the proposal number. Does it mean P1 is not guaranteed? At first, I was puzzled. Later, after talking with my friends, I found that "accept" in P1 actually refers to the "Promise" of the acceptor to proposer ", that is, there is ambiguity between the language description and the algorithm step description. Therefore, I think the mathematical syntax should be used instead of the text language for the algorithm problem.

Therefore, P1 emphasizes that the first proposal should be promise, but the second is not mentioned yet. This is also one of the questions.

Obviously, the paxos algorithm cannot be ensured by P1 alone. Because the majority may not be formed, the following discussion should consider how to make up for the shortcomings of P1, to ensure the paxos algorithm, we hope that the future conditions should be described as follows:

  • How to solve the problem that P1 cannot form a majority
  • How to select the second proposal?

So the constraint P2 appears:
P2: if a proposal with value V is chosen, then every higher-numbered proposal that is chosen has value v.

P2 did not go down the path of P1, nor did it solve the two imperfections of P1. Instead, it discussed how to ensure that only one value can be selected from the other. P1 discusses how to choose. P2 discusses that once selected, the subsequent selection should remain unchanged, that is, P1 is still discussing the selection, and P2 has been selected, there is a fault in the middle, and there is no discussion about how to choose it.

In fact, we can see from the subsequent Lamport's continuous P2 enhancement that P2 contains p1 (via proposal number, no number before the first time, so select ), p2 really gives the specific process of selection. After analysis, we can see that P1 gives the first option and P2 gives all the options. The conditions are a bit repeated. Therefore, it is inaccurate to regard P1 and P2 as two independent conditions. Therefore,"If both P1 and P2 are guaranteed, constraint 2 is guaranteed.

", Has a certain impact on the subtle understanding.

It doesn't mean P1 is useless. In turn, P2 is an unknown problem, while P1 is a known part of this unknown problem. From the perspective of contract, P1 is a non-variant, any P2 enhancement cannot pass the header so that it cannot meet the unchanged P1 type. That is to say, P1 is the bottom line of P2 enhancement.

Is there any other non-variant that needs to be followed? Have these unknown non-variant patterns been damaged during P2 enhancement? These difficult problems involve the correctness of the paxos algorithm. The strict mathematical proof of MIT is beyond this article.

In addition, the Chinese wiki describes P2 as follows:"P2: Once a value is approved (chosen), the value subsequently approved (chosen) must be the same as this value

.", The original article uses higher-numbered

It can better describe the fact that proposal will be numbered in the future,

While the Chinese language uses"After

"

, Has completely lost this meaning.

We will temporarily press P1 to stop the table and observe P2 at a close distance. To ensure that a value is selected each time, P2 specifies that if a value has been selected, if other proposer submits the value, then, the approved value should be consistent with the previous one, that is, when a value is selected in fact, the subsequent proposer cannot submit different values to disrupt the previous results. This is a general description, but if this description can be implemented, the paxos algorithm can be ensured. Therefore, P2 is also called "safety property ".

The subsequent discussions are based on "if a proposal with value V is chosen", how to ensure that "then every higher-numbered proposal that is chosen has value v

", How to implement" a proposal with value V is chosen.

P2 is more about how to solve this problem at the ideological level, but the specific implementation work requires many detailed steps. Lamport implements P2 by gradually enhancing the conditions, mainly from the following aspects:

  • Requirements for the entire result (P2)
  • Request for acceptor (p2a

    )

  • Requirement on proposer (p2b

    )

  • Both acceptor and Proposer are required (P2c

    )

The reason why Lamport can divide the process so clearly is unknown, but from the article published by Lamport, he has a deep understanding of distributed systems and has continued for a long time, it has a lot to do with the foundation of the distributed architecture and the great efforts behind it. But for us, we don't know the process but the result, and we always feel that we don't know why.

Let's continue with the above ideas:

P2a

: If a proposal with value V is chosen, then every higher-numbered Proposal accepted by any acceptor has value v.

This condition is imposed on the acceptor. Obviously, if p2a

It is certain that P2 is satisfied, but p2a

The enhancement destroys the bottom line of P1 unchanged. For more information, see the original article.

It is meaningless, but it is upgraded from the proposer end.

P2b

: If a proposal with value V is chosen, then every higher-numbered proposal issued by any proposer has value v.

This condition is limiting proposer. If proposer can be restricted, the acceptor restrictions can certainly be met. At the same time, because the proposer must submit value V, P1 is guaranteed by the way (the first must be value V)

But p2b

It is difficult to implement because multiple proposer can submit proposal of any value, and cannot limit proposer to submit a value.

Equivalent Condition:

P2c

: For any V and N, if a proposal with value V and number N is issued, then there is a set S consisting of a majority of acceptors such that either

(A) No acceptor in S has accepted any proposal numbered less than N, or

(B) V is the value of the highest-numbered Proposal among all proposals numbered less than N accepted by the acceptors in S.

According to the original article, P2c

It contains p2b

, But by P2c

Derivation of p2b

Is the most difficult part to understand.

P2c

What to do, because p2b

Difficult to implement directly, P2c

The solution is to solve p2b.

The problem is to solve "If value V is selected, the proposal with a higher number already has value V", that is:

  • R: "for any V and N, if a proposal with value V and number N is issued

    "Is the result, and

  • C: "then there is a set S consisting...

    "Is the condition

It is to prove that if C is true, then the result R is true, and the expression in the original article is "if R is true, there is a condition r", which makes it easy to confuse the causal relationship, I once again lamented that the use of mathematical symbols to express such ambiguity would certainly be much reduced.

P2c

The solution is not to directly try to meet p2b's requirements.

To meet the requirements of p2b

If this sufficient condition can be met, then p2b

Is obvious. It should also be emphasized that Proposer can submit any value. How can you restrict the value V that must be submitted? In the original article, "for any V and N, if a proposal with value V and number N is issued

"Indicates" if a proposal number N submits value V,And value V can be accepted by the acceptor.

To be accepted, you cannot submit a value at will. It must be a restricted value. The premise discussed here is that value V is accepted. Then let's look at whether condition C is met and the result R is true.

(A) No acceptor in S has accepted any proposal numbered less than N

If this condition is true, n is the first proposal in S, which must be accepted according to P1, so the result R is true.

(B)

V is the value of the highest-numbered Proposal among all proposals numbered less than N accepted by the acceptors in S

This proof first assumes that the proposal numbered N has value x selected, and there must be a combination of C, where each acceptor accepts value X, each acceptor in set S accepts value v. Because S and C are majority, a public member U accepts both X and V, to ensure the uniqueness of the selection, x = v.

You may find that the proof is a little less rigorous. There are still many proposal values between "the maximum number less than N" and N, and those proposal also have some values. Will those values be not V?

This will use the mathematical induction in the original text, that is, the proposal of any number M has value V, then n = m + 1 is, according to the above, it also has value V, so backward recursion, any n> M has value v. The induction proof in the Chinese wiki does not need to be pushed to m... n-1, but the inverse proof of N can be obtained through mathematical induction.

That is to say, P2c

Yes p2b

To meet P2c

P2b

.

Let's take a closer look at P2c.

It is found that you only need to consult the acceptor before submitting a proposal by proposer to see if they have selected a value v, then select a new number and value for submission based on the acceptor's answer to meet the requirements of P2c

. By serial number, you can combine the conditions (a) and (B.

Actually, P2c

The idea to be presented is very simple: if the previous value V is selected, the value V will be submitted later; otherwise, the proposer will decide which value to submit. The specific practice is to consult in advance and decide in the matter, post-event submission, that is, it can be implemented through the message passing model. Lamport expresses the problem through conditions, sets, inductive proofs, and other forms. Without such a purpose, it will lead to difficulties in understanding. You may wonder if you can only select one value from start to end? In fact, the election here refers to an election, rather than the entire election cycle. You can run paxos multiple times and select only one value each time.

Meet P2c requirements

It also shows that if you want to submit a correct value V, you must restrict proposer and Acceptor at the same time. It cannot be solved only by limiting one party.

Review the recursive relationship between conditions P2c

=> P2b

=> P2a

=> P2, that is, P2c

Finally, P2 is guaranteed, that is, after a value V is selected, the larger proposal with the selected number has the value V, P2c

This not only guarantees the results of P2, but also raises the question of "How to Choose", that is, the above is carried out in stages, which fills in the gap between P1 and P2, there are also two incomplete problems in P1 that will be intuitively solved. The specific issue depends on the algorithm process section.

Incomplete P1 problems:

P2c

It also solves the problem of incomplete P1, because the value submitted by Proposer is restricted by the acceptor, so that two different values will not be submitted in one election, even if the submission is successful, one of them will be rejected due to the proposal number problem, thus ensuring a majority.

Another question about how to choose the second one is obvious.

It is proved again that P2 contains P1, which is just an unknown non-variant of P2.

Continue: paxos algorithm 2

Author: chen77716 posted on 15:11:00 Original article link Read: 939 comment: 0 view comment

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.