In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is Java consistent Hash algorithm". In daily operation, I believe many people have doubts about what is Java consistent Hash algorithm. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what is Java consistent Hash algorithm"! Next, please follow the editor to study!
Data fragmentation
Let's look at an example first.
We often use Redis to cache and put some data on it to reduce the pressure on the data.
When the amount of data is small and the access pressure is not great, it can usually be done with a Redis. In order to be highly available, it is enough to have a master and slave.
When the amount of data becomes larger and the concurrency increases, it is difficult to put all the cached data on one machine. After all, the resources of a machine are limited. Usually, we build a cluster environment to put the data in each Redis as evenly as possible. For example, there are 4 Redis in our cluster.
So how to put the data in these four Redis as evenly as possible? The simplest is the modular algorithm:
Hash (key)% NMagi N is the number of Redis, where N = 4
It looks very good, because with this method, we can store the data in 4 Redis on average, and when there is a new request, we can also locate which Redis the data will be in, so that the cached data can be accurately queried.
02 problems encountered in data slicing
But four Redis is not enough. We need to add four more Redis.
Then the remainder algorithm will become: hash (key)% 8
As you can imagine, most of the current cache locations will be wrong, and in extreme cases, it will cause cache avalanches.
03 consistent Hash algorithm
The consistent Hash algorithm can solve this problem very well, and its general process is as follows:
Take 0 as the starting point, 2 ^ 32-1 as the end point, draw a straight line, and then coincide with the starting point and the end point, the straight line into a circle, the direction is clockwise from small to large. The first point on the right of 0 is 1, then 2, and so on.
Modulo 2 ^ 32 after hash the IP or other keywords of the three servers, which is bound to fall somewhere on this circle, marked Node1, Node2, Node3.
Then do the same with the data key, which is bound to fall somewhere on the circle; then walk clockwise and you can find a Node, which is the server that the key is going to store.
If you add a server or delete a server, only part of the data will be affected.
However, if there are too few nodes or uneven distribution, it is easy to tilt the data, that is, most of the data will be concentrated on a certain server.
In order to solve the problem of data skew, the consistent Hash algorithm proposes [virtual node], which calculates multiple hashes for each service node and puts them in different positions on the circle.
Of course, we can also find that the consistent Hash algorithm only solves the problem of most data.
At this point, the study of "what is the Java consistent Hash algorithm" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.