The consistency algorithm of distributed system------"Designing data-intensive Applications" Reading notes 13

Source: Internet
Author: User

Consistency Algorithm is one of the most important problems in distributed system. On the face of it, it seems simple, just let a few nodes agree in some way. In this article, we will take a complete comb distributed system in the consensus algorithm, to more profound understanding of the design of distributed systems.

1. Atomic commit and two-phase commit (2PC)

Atomic commits prevent the database from being in a semi-updated state, which is especially important for databases that need to meet multi-object transactions and maintain secondary indexes. Each sub-index is a data structure that is detached from the master data, so if you modify some of the data, you also need to make the corresponding changes in the secondary index. By Atomicity, the level two index is guaranteed to be consistent with the original data.

Atom Commit under Distributed system

Let's take a look at how atomic commits are implemented on a single node:
For transactions that execute on a single database node, when a client submits a transaction to the database, the database first adds the transaction information to the log on disk for submission. If the database crashes in the middle of the process, the transaction is resumed from the log when the node restarts. If the commit record is successfully written to disk before the crash, the transaction is considered committed and, if not, any writes to the transaction are rolled back. in a single node database, the atomic commit of a transaction depends on the sequence of persisted writes to the disk, which causes the commit of the transaction to be atomized.

What if more than one node is involved in a transaction? The situation becomes very complex:

    • Some nodes may detect constraint violations or conflicts, need to be aborted, and other nodes will be able to commit successfully.

    • Some submission requests can be lost on the network, eventually aborted due to timeouts, and other submission requests are passed.

    • Some nodes may crash before the commit record is fully written and rolled back, while others are successfully committed.

Therefore, a node must determine that the transaction cannot be committed until all other nodes are committed. We have to have an algorithm that allows distributed nodes to reach consensus.

Two-phase commit (2PC).

The two-phase commit agreement is the first distributed consensus protocol we're going to talk about. As shown

    • Through a coordinator, the coordinator node begins the 1th stage when the application is ready to commit. It sends a prepare request to each participating database node and asks if they can commit? The coordinator then tracks the response of the participant, if all participants can answer: Yes, indicates that they are ready to commit, then the Coordinator makes a submission request at phase 2nd, all participants commit at the same time, and if any participant answers: No, the coordinator sends a abort request to all nodes in phase 2nd.

Two-phase commit issues

once a network failure or participant failure occurs, the coordinator node can abort the transaction through a timeout mechanism. Two if the commit or abort transaction fails in phase two, the coordinator node can retry indefinitely until the failback is resumed. However, if the coordinator node crashes during this process, it will cause many headaches:

If the coordinator fails before sending a prepare request, the participant can safely abort the transaction. However, once the participant receives a preparation request and answers: Yes, it must wait for instructions from the coordinator node, whether the transaction is committed or aborted. Once the coordinator node crashes or a network failure occurs, the participant can wait indefinitely. as shown in the following:

Database 1 does not know whether a transaction is committed because the coordinator crashes before sending the commit request to database 1. If database 1 is halted unilaterally, its results are inconsistent with database 2. the only way is to wait for the coordinator to recover, which is why the coordinator must write the result of its commit or abort to the transaction log on the local disk before committing or aborting the transaction request to the participant. When the coordinator resumes, it determines the status of all suspicious transactions by reading its transaction log. Any transaction that does not commit a record in the coordinator log will be aborted.

2. Consistency of consensus

From the above we can understand that in a distributed system can use the two-phase commit protocol to achieve the transaction ( you can also use the two-phase commit protocol upgrade version of the three-phase commit protocol ). A distributed transaction solves a transactional implementation in a distributed database, but it also introduces a new problem, and the transaction Coordinator itself is a database in which the results of transactions are stored . If the coordinator is running on only one machine, it is easy to create a single point of failure. By default, many coordinator implementations are not highly available, only the basic copy backup feature. Many server-side applications, based on a stateless model, store persistent state in a database, with the advantage of being able to add and remove application servers at will. However, when the coordinator becomes part of the application server, it changes the nature of the deployment. Because the coordinator's log also becomes the content that requires persistent state storage. On the one hand, distributed transaction is difficult to achieve complete consistency guarantee. On the other hand, because of the existence of the coordinator node, the distributed transaction can greatly reduce the performance of the database, so many data systems abandon the support of distributed transaction.

Consensus

In fact, the difficult point of distributed transaction is to achieve consistency , it requires multiple nodes in the system to achieve consensus. For example, if several people try to book the last seat on the plane or the same theater seat at the same time, or try to register an account with the same user name, you can use the consistency algorithm to finalize which operation is successful and agree on a consensus among the nodes of the distributed system.

The usual form of consensus is as follows: one or more nodes can present a value, and the consensus algorithm determines one of the values. The core concept of consensus: everyone decides the same result, and once you decide, you can't change your mind. We can simplify this model and discard fault tolerance: You can specify a node to be a leader, and all the decisions are made by the leader node. However, if the leader node fails, the system is paralyzed. The coordinator in the two-phase commit protocol is a leader, and once the coordinator fails, the system is unable to work. A better consensus algorithm requires that the system still works even if some nodes fail. Of course, if all the nodes are crashing, and none of the nodes are running, then no algorithm can run the chicken blood, so the number of failures that the algorithm tolerates is limited: in fact, any consensus algorithm requires that at least the majority of the nodes are functioning properly to ensure consensus.

Consistency algorithm and full sequence broadcast

There are many consensus algorithm algorithms in distributed system such as: Paxos,raft,zab, etc. In this article, the full details of the different algorithms are not involved, and they are used to understand some of the advanced ideas. ( Follow us will be interested to explain these algorithms to you )

The consistency algorithm here conforms to the characteristics of the full-order broadcast, and the full-sequence broadcast needs to deliver the message exactly once to all nodes in the same order. In each round of negotiations, each node can present the next message to be sent, which is then agreed by the negotiation, and the next message is passed in the system. all nodes together decide to pass the same message in the same order, and the message is not duplicated, the message will not be destroyed, and it will not be generated in a vacuum. (This ignores Byzantine issues, and if you need to introduce Byzantine fault tolerance, you need to adopt a POW algorithm similar to the blockchain)

# # #epoch编号和Leader选举

Every round of news is negotiated, and it is inefficient behavior. So protocols like raft and Zab are optimized using the leader mechanism. So consensus agreements need to elect leader and ensure that leader is unique. How to ensure that the distributed node to the leader node election results of consensus? here, with the distributed timing mechanism, there is an epoch number for each of the legitimately elected leader to ensure that leader is unique. once the leader node fails, a new leader is started between the nodes, and the new leader generates a new epoch value, so the epoch value is monotonically increasing according to the leader election order. If two different leader are in conflict ( perhaps because the previous leader did not actually die ), then all nodes will agree to a higher epoch value.

Leader each decision that needs to be made, it must send the proposed value to the other node and wait for the collection of nodes to respond to the proposal. At this point the consensus needs to reach a quorum, which is the majority of nodes that comprise the cluster. A node will vote in favor of a proposal only if it does not know any other leader with a higher epoch value. The consensus is divided into two operational stages: one: Choose a leader, two: the proposal to vote leader. When leader receives a quorum for the voting result to agree to the proposal, it can be concluded that there is no leader with a higher epoch value, and then it is safe to submit the results. ( can be proved with contradiction ) This voting process appears similar to the two-phase commit. The biggest difference is the fault tolerance of the coordinator node to guarantee the high availability of the consensus algorithm.

3. The cost of consensus

The consensus algorithm is a major breakthrough in distributed systems: It brings a concrete solution to all other uncertain systems, and it is still fault-tolerant (as long as most nodes work correctly) and provides full-order broadcast characteristics, so they can also implement linearized atomic operations in a fault-tolerant manner.

There is no such thing as a free lunch, and there is a price to it.

    • The proposal would need to be voted on during the consultation process, and the process of voting was essentially a synchronous replication. We have explored the pros and cons of synchronous replication and asynchronous replication in the previous article. In practice, we typically configure the use of asynchronous replication. Some submitted data may be lost during failover, but for better performance, many people choose to accept this risk.

    • The stringent requirements for consensus require a majority of decisions. This means that you need at least three nodes to tolerate a failure (the remaining two constituent majority), or at least five nodes to tolerate two failures (the remaining three constituent majority).

    • Most consensus algorithms assume that a fixed set of nodes participate in the poll, which means that nodes in the cluster cannot be added or deleted dynamically.

    • How to detect failed nodes is also a problem. In an environment with highly variable network latencies, it is common for a node to mistakenly assume that the leader is invalid. While this error does not compromise the security of the system, frequent leader elections can lead to poor system performance because the system will eventually spend more time choosing a leader than doing any useful work.

Therefore, the design of a more robust algorithm for unreliable networks is still a problem that our computer workers need to challenge and study.

Summary:

Starting from the two-phase commit protocol, we comb the consistency algorithm under distributed system and analyze their merits and demerits. With these outsourced distributed coordination services as the base model, we can achieve more and more complex distributed services.

The consistency algorithm of distributed system------"Designing data-intensive Applications" Reading notes 13

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.