In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What this article shares with you is about what the CAP principle in the micro-service architecture is. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
What is the principle of distributed CAP, what is partition fault tolerance, what is the CAP difference between zookeeper and eureka, and how do you do it as an architect of the company, those distributed systems are designed as CP, AP, why, and so on.
What exactly does distributed system CAP mean?
C (Consistency): consistency, that is, data consistency, especially in distributed systems.
A (Availability): availability, that is, the high availability of services, especially the high availability of services in a distributed system. The paralysis of a service does not affect the normal operation of the whole distributed system.
P (Partition Tolerance): partition fault tolerance (also known as partition tolerance), that is, network failure, especially when a network failure occurs between services in a distributed system, and the whole distributed system still maintains availability and consistency.
In a word, CAP: in a distributed system, the network fails, the service is paralyzed, and the data of the whole system is still consistent.
The above statement may not be easy to understand, give a popular example of what is the principle of distributed CAP.
Describe the case in vernacular:
As shown in the picture above, Xiao Zhang is going to buy several books related to micro-services at JD.com Mall. the total order amount is 200 yuan, and he happens to have a coupon of 200 yuan less than 100 yuan.
In this case, the CAP of the distributed system is as follows:
Data consistency reflects:
Use 100 yuan coupon deduction, 100 yuan actually paid, coupon used up.
Failed to use the 100 yuan coupon, actually paid 200 yuan, the coupon is still there.
System availability reflects:
When Xiao Zhang places an order, the order service or PLUS member service fails, which should not affect Xiao Zhang to place an order at this time.
The fault tolerance of system partition is reflected:
When Xiao Zhang places an order, the network between the order service and the PLUS member service does not affect Xiao Zhang to place an order.
What is partition fault tolerance?
The most difficult concept in the CAP theorem is P, partition fault tolerance, which can be understood by drawing a diagram.
In the case of network outage, the two servers become independent networks and cannot communicate with each other, which is called zoning. In the case of partitions, distributed systems satisfy the fault tolerance of partitions if they want to ensure data consistency and availability.
The difficulty of implementing CAP technology is solved one by one below
At present, most Internet enterprises are micro-service architecture, that is, distributed systems.
Now an e-commerce micro-service architecture, assuming that there is a network failure (P), the service is down (A), the data of the whole system is still consistent. This cannot be done.
Relatively achievable solutions:
The practice of the industry is to choose the second of the three CAP, that is,
CP
AP
CA (Please think? Described later)
When there is a network failure between services:
Question:
How to ensure the high availability of order service and PLUS membership service?
How to place an order and deduct 100 yuan coupons at the same time?
Solutions for distributed systems:
CAP sacrifice consistency (AP): ensure high availability, that is, ensure that the order service can be accessed normally, and PLUS member services can be accessed normally, at the expense of data consistency.
Xiao Zhang went to JD.com Mall to place an order (but did not deduct 100 yuan discount coupons). In this case, after Xiao Zhang's order was submitted successfully, he went to check that the 100 yuan coupons were still there, but he did not deduct the success, but the actual payment was 100 yuan. Very puzzled (heart secretly delighted).
What should I do? How to solve this problem?
The general practice is that when the network returns to normal, the order service retries to request PLUS membership service, and then deducts the 100 yuan coupon.
CAP sacrifices availability (CP) to ensure data consistency.
When Xiao Zhang went to JD.com Mall to place an order, he prompted: "the network is abnormal, please try again later."
What should I do? How to solve this problem?
Xiao Zhang can only place an order successfully when the network returns to normal.
CAP sacrifices partition fault tolerance (CA): no P partitions, that is, network failures are not allowed, which is impossible.
Therefore, there is no CA in the distributed system. Even the traditional single system can not do CA, because the single system will have a single fault.
Through the case of Xiao Zhang placing an order in JD.com Mall, my friends should have figured it out.
Here is an analysis of the CAP principle of zookeeper & eureka, two service registries commonly used in microservice architectures.
1) illustrating the CAP principle of zookeeper
Note: the underlying principle and implementation of zookeeper are not described here.
Zookeeper as a micro-service registry is based on the principle of CP, that is, it ensures the consistency of data at the expense of availability. As shown in the following figure:
The principle of data synchronization of zookeeper:
The Client1 client registers with zk Server1, and the zk Server1 synchronization information is sent to the leader node where zk Server2,zk Server2 is the registry, which is responsible for synchronizing the message broadcast to other follower zk nodes. In order to ensure the consistency of the data, the Client1 client can receive the message of successful registration only when the whole registry information is synchronized.
As shown in the figure above, when the registry leader is restarted or a network failure occurs, the entire zk cluster re-elects the leader node. During the election period, the Client client cannot register, that is, the zookeeper service is not available, so the availability of the system is sacrificed.
As shown in the above figure, the system can not resume registration until the leader node is elected by the whole system, so zookeeper sacrifices the availability of the system in order to ensure data consistency.
A fatal disadvantage of CP is that in large distributed systems, the network is very complex, the failure frequency of leader nodes is very high, and it is easy to cause avalanches. So this is the reason why many large distributed systems do not choose zookeeper as the registry.
2) illustrating the CAP principle of Eureka
As shown in the following figure:
Eureka is the principle of AP, which ensures the availability of the system, but sacrifices the consistency of the system.
The principle of data synchronization of eureka:
The first step is to register the Client1 client with the eureka Server1 service
In the second step, eureka Server1 directly tells Client1 that the registration is successful.
In the third step, eureka Server1 synchronizes the registration information of Client1 to Server2. In order to ensure the availability of services, eureka Server is synchronized asynchronously.
Through the above case description and graphic interpretation, I believe you have a certain understanding of the principle of CAP in the micro-service (distributed system) architecture. For example, you know what is CAP principle, what is partition fault tolerance, and what is the CAP difference between zookeeper and eureka as a registry. At the same time, I hope it will be helpful to your company's system architecture design in the future, whether the system design follows CP or AP.
These are the principles of CAP in the micro-service architecture, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.