Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Consistency of redis what are hash and hash slots?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "redis consistency hash and hash slot is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "redis consistency hash and hash slot is what" it!

If we now have x cache devices, we can key%x when deciding which cache device to put the data on, but if capacity expansion or node loss occurs, you will need key% (x ±y) so that you will encounter a large number of data migration problems, which can be avoided by consistent hash and hash slots.

Consistent hash principle

An ordinary hash is a balance for the number of servers, and a consistent hash is a balance for a specific number (2 ^ 32). It will not change because of the number of servers. First, we take the ip or other unique identification of the server to get a value, which is the position of the server on the hash ring, and then hash the object to be put into the server to get a value. Find the corresponding server on the hash exchange. If the value is located in a location that does not have a server, see if the server knows to find a server that can be stored in the next location.

1. Annular space

According to the commonly used hash algorithm, the corresponding key is hashed into a space with 2 to the power of 32 nodes, that is, the digital space of 0 ~ (32)-1. We can think of this thing as a man who bites his tail and forms a closed loop.

2. Hash the server to the ring

Now that we have the ring, we need to put the server on the ring. We can take the hash according to the unique identification such as the server's IP address, and then put it on the ring.

3. Data storage and acquisition

When we need to put a data on the server, we first need to calculate the hash value of the data and then take the remainder. If the residual value has a corresponding server on the ring, then put it directly in it. If not, then look back.

So in the end, data1 is in redis1, and data2 is in redis2. When we get the data, we perform the same process, calculate the hash value of the key, and then get the stored server according to the same rules.

4. Delete and add servers

If one redis node is dead now, the data in the other nodes is still there, and the data in the original node will be reassigned to the next node.

If you add a new server RedisNeo in the environment, and map the RedisNeo to the ring through the hash algorithm, then the data with the previous hash values between Redis2 and RedisNeo will be migrated to the RedisNeo (RedisNeo next to Redis2 in the following figure), and other objects will keep the original storage location by clockwise migration rules. Through the analysis of the addition and deletion of nodes, the consistent hash algorithm not only maintains the monotonicity, but also minimizes the data migration, which is very suitable for the distributed cluster and avoids a large number of data migration. reduces the pressure on the server.

So after redisNeo joined, data3 went to redisNeo.

5. Balance

So far, consistent hash can be considered complete, but there is one problem that needs to be solved, and that is balance. As we can see from the following figure, when there are few server nodes, there will be a problem, that is, a large amount of data will inevitably be concentrated on one node, for example, you only have two nodes, one in 1 and the other in 10, then it is obvious that the pressure on 1 node is infinite, because only those with hash values between [2jue 10] will reach 10 nodes, and the rest will go up to 1 node. In order to solve this data skew problem, the consistent hashing algorithm introduces a virtual node mechanism, that is, multiple hashes are calculated for each service node, and one service node is placed at each result location, which is called a virtual node. The specific approach can first determine the number of virtual nodes associated with each physical node, and then add a number after the ip or hostname, while the data location algorithm remains the same, only one more step of the mapping from the virtual node to the actual node.

Hash slot

The hash slot is used in the redis cluster cluster scheme. The redis cluster cluster does not use the consistent hash scheme, but uses the hash slot in the data slice to store and read data. Redis cluster uses data slicing hash slot for data storage and data reading. Redis cluster has a total of 2 ^ 14 (16384) slots. All master nodes will have a slot, such as 01000, and the number of slots can be migrated. The slave node of the master node does not allocate slots and only has read permissions. But note that in the code, redis cluster performs read and write operations on the master node, not the slave node you think, but the master node. The first time a new redis cluster was built, 16384 slots were evenly distributed by master nodes.

Compared with the consistent hash, you need to manually allocate hash slots during capacity expansion and reduction, and hand over its slave node and hash slot to other master nodes when deleting master nodes. Hash slots are determined which slot belongs to according to the value of CRC-16 (key) 384.

Thank you for your reading, the above is the content of "what is the consistency of redis hash and hash slot". After the study of this article, I believe you have a deeper understanding of what the consistency of redis hash and hash slot is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report