Distributed Systems: coherence protocols

It is the process and the data stored on the consistency model is essentially agreed, by the consistency of the model, we can understand the reasoning and data replication issues need to be considered and the basic assumptions in distributed systems. So, specifically consistency models have some of it? This article will explain the main ideas and methods consistent protocol.

What is coherence protocol

Coherence protocol describes the actual realization of a particular model of consistency. Consistency model like interface and protocol consistency is like a concrete implementation of the interface. The consistency constraint consistency model provides data replication distributed system, in order to achieve consistency constraint model, by the need to ensure coherence protocol.

Whether to allow data coherency protocol in accordance with differences can be divided into two types:

  • Single master agreement (data not allow differences) : The entire distributed system is like a single system, all write operations by the master node processing and synchronization to other copies. E.g. standby synchronization, 2PC, Paxos belong to such agreements.
  • Multi-Master Agreement (allow data divergence) : all write operations can be initiated by different nodes, and synchronized to other replicas. For example Gossip, POW.

It can be found, their core difference lies in whether to allow multiple nodes to initiate write operations, allowing only a single master agreement by the master node initiates a write operation, so it can ensure the orderly operation, consistency stronger. The multi-master protocol allows multiple nodes to initiate write operations, so it can not ensure the orderly operation can only achieve weak consistency.

It is worth noting that there are many coherency protocol classification, mainly to see from which angle to categorize, classify another common way is divided according to the synchronous / asynchronous replication, do not discuss here. The following single and multi-master master agreement protocol do are some common analysis, limited space, not go into the details of the agreement.

Single Master Agreement

Common single master agreement that will be in charge of the master node with a write operation that could ensure global write order consistency, it has another name, called sequencers, very image.

Copy standby

Standby replication can be said to be the most commonly used data replication method is the most basic way, many other protocols are based on its variants. Standby replication requires that all write operations are performed on the primary node, and then sent to log actions other replicas. It can be found since the standby replication is delayed, so that it is the final consistency achieved.

Main implementation backup copy of: the master node processed returns the result to the client immediately after the write operation, the log asynchronous write operations synchronized to other replicas. The advantage is the high performance client does not need to wait for data synchronization, the disadvantage is that if the master node to synchronize the data before the copy of the data is missing, then the data is permanently lost. MySQL the standby synchronization is a typical asynchronous replication.

Two-phase commit

Two-phase commit (2PC) is a relational database used to maintain the consistency of distributed transaction protocol, which also belongs to the synchronous replication protocol, only returned to the client after that is the result of data synchronization is complete. 2PC ensure that all nodes can be found only returned to the client after the same data to achieve sequential consistency.

2PC copy data divided into two steps:

  1. Voting phase : the master node sends data to all copies, each copy must respond committed or rolled back, if submitting a copy vote, then it will put the data staging area, waiting for final submission.
  2. Commit phase : the master node receives a response other copies, if copies are thought to be submitted, then it sends an acknowledgment to submit them to submit copies of all updated data will be moved from the area of permanent staging area. As long as there is a copy of the return on the whole rollback rollback.

2PC can find typical CA system, in order to ensure the consistency and availability, network partition occurs once or 2PC node is not a write operation is rejected, into the system with read-only. As the situation has led to the blocking of nodes in a cluster 2PC prone, so the scene is not commonly used in data replication, generally used for distributed transaction (Note: The actual application process will be a lot of optimization).

Partition tolerance coherence protocol

Coherence protocol partition tolerance like all single master agreement, it is only one master node is responsible for writing (provide sequential consistency), but it differs with 2PC is that it only needs to ensure that most nodes (usually more than half) agree on the result can be returned to the client, which can improve performance, but also can tolerate network partition (a few nodes partition does not cause the entire system can not run). Tolerant consensus algorithm partitions to ensure data consistency most nodes before returning client, also achieved sequential consistency.

Here's a simple example to illustrate the core idea of ​​such an algorithm. Suppose now that there is a distributed file system, its files are copied to the three servers, we stipulate: To update a file, the client must have access to at least two servers (most), get updated after they agreed to perform , and each file will have a version number to identify; to read the file, the client must also access at least two servers get the version number of the file, if all of the same version number, then the version must be the latest version because if the previous update operating requirements must have the consent of the majority of the server to update files.

These are the core ideas we know Paxos, ZAB, Raft coherence protocols such as partition tolerance: consistency does not necessarily ensure that all nodes are consistent, as long as the majority of the nodes updated for the entire distributed system for data also consistency. The above is just a brief explanation, the real algorithm is more complex, there will not start.

The coherence protocol partition tolerance Paxos typical CP system, to ensure consistency and partition tolerance, in case of a network partition, the majority of the write enable node, consistency by the consistency of the whole system is most nodes , will also make a few nodes fail to stop the service (can not read and write), to give up the availability of the overall system, that client access to a few nodes.

It is noted that, according to the theory CAP, suppose we have three nodes A, B, C, C when the network is partitioned, has come query request, this time C and because they can not communicate with other nodes, it can not respond to the query C , and not have the availability. But in the realization of the project, this problem can be bypassed when the Client Access C can not get a response, it had access to A, B, or actually part of availability for the entire system, not to say that the CP The system must have lost availability. Reference detailed analysis of distributed systems: Past and Present theory CAP

Multi-Master Agreement

Compared to single-master protocol in order to achieve sequential consistency, the plurality of nodes allowed concurrent write, multi-master protocol contrary, only guarantee eventual consistency allows multiple nodes to concurrently write, can significantly improve system performance. Since the eventual consistency are multi-master protocol is typically provided, so commonly used in less demanding of data consistency scene.

Gossip protocol is a typical multi-master protocol, many distributed systems use it for data replication, e.g. bit coins, as well a to the center of the chain, the data of all the nodes are synchronized using a Gossip protocol. In addition, the protocol also Gossip some distributed database as is used for synchronization of the distributed fault detection Dynamo state, when a failing node leaves the cluster, the other nodes can be detected quickly.

From the core idea of the name can be seen on Gossip Protocol, Gossip is gossip gossip mean, think about the scene spread gossip among people in our daily lives, a gossip once a person knows, through person to person in school, basically the whole school of people will eventually know. Therefore, the core idea is that Gossip protocol: each node can send messages to other nodes, the message was received node randomly select another node to send a message, node receives a message to do the same thing.

Multi-master protocol allows multiple nodes to run concurrent write, there will definitely be a case of concurrent data write data lead to conflict, so this type of agreement will need to address the problem of concurrent writes. Single master agreement written by the master node control to ensure the situation does not appear concurrent writes, since all writes will eventually be sorted by the master node, in a sense, to use the system for a single master agreement is actually written string OK, so its performance is a bottleneck. The multi-node multi-master protocol allows concurrent write, put out of the write performance, but it is actually the data merging operation is delayed, a single-master protocol proceeds when writing data combined thus read data If the time when data conflicts arise, it is the need for data consolidation, to ensure global consistency.

We mentioned bitcoin using Gossip protocol for data replication, then the problem came, not to say that multi-master protocol performance will be higher yet, why Bitcoin performance so bad? To actually here separately, because the Bitcoin is decentralized, but its payment function needs to ensure global data consistency, so it took a very clever consensus algorithm POW: All nodes do a math problem , the first to calculate the answer on who has the right to write the transaction chain, then use the Gossip protocol and spread it answers transaction, other nodes to verify that it's correct answer data will be saved.

That you may have a question here: POW as a multi-master protocol performance Why so low? Any agreement has its applicable scenarios. In this scenario the bit credits, for which data consistency is strong demand, in theory, a single master protocol is the best choice. But Bitcoin as a decentralized digital currency is not going to use a single master agreement, otherwise they become the center of the system. So Bitcoin can only select multiple master agreement, will be the Bitcoin whole chain operated by POW agreement approximation serialized , so as to reduce the probability of double flowers appear (concurrent write when a Bitcoin are consumed several times), fish and can not have both, since stronger consistency, you can only sacrifice performance in exchange.

Because multi-master protocol allows the data differences, then we need to have a strategy to resolve the conflict to ensure that the final data consistency. If you want to strictly distinguish between words, Bitcoin actually applied two coherency protocol:

  • The POW : The decision to record the right node, functions similar to the single master agreement sequencer. Note also the POW multi-master protocol, despite the low probability, but there may be multiple nodes simultaneously calculate the answer, along with a block (concurrent write) the situation, then we called Bitcoin appeared bifurcation, that there has been conflicting data .
  • Gossip : a block for the transaction will be synchronized to all nodes worldwide. Since the POW situation will be complicated by writing, when a node receives multiple nodes simultaneously write requests, it is necessary to solve the problem of data collision. Bitcoin resolve data conflicts embodiment is that when the bifurcation appears, select the longest piece of chain as a main chain, on the other bifurcated fair chains are rolled back, waiting for repackaging the block.

to sum up

This paper will be distributed into two coherence protocols from the perspective of whether to allow data differences: single and multi-master master agreement protocol. In which a single master agreement with a master node will be responsible for the write operation that could ensure global write order consistency, but therefore also part of the expense of performance. The multi-master protocol allows a write operation can be initiated by different nodes, and synchronized to other replicas can only guarantee eventual consistency, but also enhance the performance of the system and therefore concurrent writes. High data consistency requirements of the scene such as a distributed database, mainly uses single-master protocol, for example fault detection data consistency less demanding, mainly use multi-master protocol to improve performance, of course, there are exceptions, such as credits to go bit use of the center binding Gossip POW and data replication.

It is worth noting that a single master mentioned in the text, multi-master protocol is just my personal way to a classification of the distributed coherency protocol, to help us better understand. Readers can look at references, look at the different authors of distributed protocols is how to classify it, so there will be more in-depth understanding of the distributed coherency protocol.

Reference material

Guess you like

Origin yq.aliyun.com/articles/703728