In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to apply CAP". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is CAP?
CAP Theorem, also known as Brewer Theorem, proposes that for a distributed system, it cannot satisfy the following three points at the same time:
Consisteny (consistency)
Availability (availability)
Partition tolerance (Partition Fault tolerance)
In other words, CAP Theorem states that any distributed system can only satisfy two of these three terms at the same time.
As shown in the figure above, if at most two items are satisfied at the same time, then we can have three combinations: CA, CP, and AP. Before we talk about these three combinations, let's take a look at the meanings of Consisteny (consistency), Availability (usability), and Partition tolerance (partition fault tolerance).
Suppose a system currently has two nodes An and B, the two nodes can be read and written by Actor respectively, and the data between the two nodes will be synchronized automatically.
Consisteny (consistency)
The requirement for consistency means that for any client (Actor above), the latest data can be obtained with each read operation. That is, when a client writes new data to node A, the data obtained by other clients reading from node B must also be up-to-date and consistent with the data of node A.
Availability (availability)
The requirement of usability means that each request can get an expected response within a reasonable period of time (there is no guarantee that the results obtained are up-to-date).
According to the figure above, as long as the client initiates a request to node An or node B, as long as the two nodes receive the request, they must respond to the client, but there is no need to guarantee whether the value of the response is correct.
Partition tolerance (Partition Fault tolerance)
Partition fault tolerance means that when there are problems in the network between nodes, the system can still provide services normally.
After talking about the meanings and requirements of C, A, P, let's move on to see how they are combined.
Second, how to apply CAP?
Let's go back to this picture:
Although we know that there are three combinations of CA, CP and AP, it is impossible for the network to be 100% reliable under the structure of a distributed system. Since the network is not guaranteed to be absolutely reliable, P (partition fault tolerance) is a must. The reasons are as follows:
If you choose the CA combination, discard P (partition fault tolerance). Or take the An and B nodes in the top figure as an example, when a network failure occurs between nodes, in order to ensure C (consistency), then the system must be locked and no write operations are allowed, otherwise there will be data inconsistencies between nodes. But locking the system means that when a write request comes in, the system is not available, which violates the A (usability) principle.
Therefore, it is theoretically impossible to have CA combination in distributed systems, so we can only choose the combined architecture of CP and AP.
Let's take a closer look at the characteristics of the CP architecture and the AP architecture:
CP architecture
CP architecture is the combination of Consisteny (consistency) and Partition tolerance (partition fault tolerance).
As shown in the figure above, node An and node B could not communicate with each other before due to network problems. When a client (Actor above) makes a write request to node A (ready to write Message 2), node A will not receive the write operation, resulting in write failure, which ensures the data consistency between node An and node B, that is, Consisteny (consistency).
Then, if another client (another Actor in the figure above) makes a read request to node B, the request B returns the information saved before the network failure (Message 1), and this information is consistent with node A, is the last successful written information of the whole system, and can provide services normally, that is, Partition tolerance (partition fault tolerance) is guaranteed.
The above situation guarantees the CP architecture, but abandons the Availability (availability) solution.
AP architecture
AP architecture is a combination of Availability (availability) and Partition tolerance (partition fault tolerance).
Due to network problems, node An and node B could not communicate with each other before. When a client (Actor above) makes a write request to node A (ready to write Message 2), node An allows writing, and the request operation is successful. But at this time, because An and B nodes could not communicate before, the data of B node is still old (Message 1). When a client initiates a read request to node B, the data read is old data, which is inconsistent with the data read in node A. However, because the system can provide services as usual, it meets the Availability (availability) requirements.
Therefore, in this case, the AP architecture is guaranteed, but it abandons Consisteny (consistency).
Third, CAP points for attention?
After understanding the CAP theorem, for developers, when we build services, we need to make a trade-off according to the business characteristics, which points can be chosen by the current system and which should be guaranteed.
Even in the same system, the data of different modules may apply different CAP schemas. For example, in an e-commerce system, the data belonging to the user module (account secrets, wallet balance, etc.) require high consistency, so the CP architecture can be adopted. For some commodity information data, the consistency requirements are not so high, but in order to take care of the user experience, so the usability requirements are higher, then the data of this module can use AP architecture.
In addition, although we mentioned in the second section above that we can only choose CP and AP, we cannot choose CA. But the prerequisite for this statement is that the system has a network failure. However, the probability of network failure is very small in the whole life cycle of the system, so when we design, we should not only consider the scheme under the network problem, but also consider the scheme under the normal condition of the network, that is, under the normal condition of the network, CA can be realized, and we also need to ensure the CA architecture in most of the time.
Moreover, even if we can only choose the second of the three according to the CAP theorem, it does not mean that we only need to protect two of them, and give up the third point completely. We should also take some preventive measures or redundancy plans for the third point that cannot be guaranteed, so as to make the system more perfect and sound.
That's all for the content of "how to use CAP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.