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

How to understand the consistent hash algorithm

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "how to understand the consistent hash algorithm". In the daily operation, I believe many people have doubts about how to understand the 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 for you to answer the doubts about "how to understand the consistent hash algorithm". Next, please follow the editor to study!

Description of consistent Hash

Here is a simple description of the Hash algorithm, and then the consistent Hash algorithm is described.

The Hash algorithm maps a binary value of any length to a shorter fixed-length binary value, which is called a small binary value.

Hash value.

Consistent hash algorithm

The purpose of the design is to solve the hot problems in the Internet. The consistent hash algorithm corresponds to the CARP algorithm, and the former is a modification of the latter.

To put it simply, consistent Hash organizes the entire hash value space into a virtual ring, such as assuming that the value space of a hash function H is

0-2. 23-1. That is, it refers to a 32-bit unsigned whole class, and the whole Hash space ring is as follows:

In a nutshell, this is a ring, which is made up of each point, the hash space, organized in a time direction, 0 and 2 ^ 32-1 coincide at clock zero, and then, in order to determine the spatial location of each server, generally speaking, the host's Host name, or Ip address, is Hash addressed to the server every day.

For example, ‍ has three services in the space for Hash addressing. The distribution of its location in the space is as follows: ‍

Three machines, after Hash processing. You get an address that corresponds to the address on the Hsah ring as follows:

The first step is solved, how to save the machine to solve, then how to store the data?

Data is actually the same as a machine, that is, you need to use a Hash algorithm to determine which machine the data should access. First, use the same function H to Key the data.

To calculate the hash value h, determine our position on the ring according to the h value, and look down the clock along the ring from this position. The first server encountered is the server where it should be stored, and the data is stored on this server. For example, in the figure below, we have four pieces of data. After taking the value of Hash algorithm, the location in space is as follows:

An overall description of the consistent Hash map is as follows:

Igz

Analysis of Fault tolerance and scalability of consistent Hash

The traditional hash algorithm has the problems of fault tolerance and scalability, especially when adding servers and reducing servers, if you want to continue to work, then the algorithm must be modified. In this section, we will introduce the fault tolerance and scalability of the consistent Hash algorithm. If Server3 suddenly stops, the following occurs:

The machine Server3 disappeared in the cluster. Then the entire topology structure changes, as follows:

According to the consistent Hash algorithm, A still finds the three Server1,BCD data according to the sequential clock direction, which will be placed directly on the Server2. For the disappearance of the ServerB, what we can be sure is that the location of the BC is still machine 3, and only the node before machine 2 has been changed.

When the number of clusters is reduced, the algorithm of consistent Hash can reduce the data changes of clusters to a minimum.

In the next case, we try to add a new one to the current Hash ring to see how the hash ring changes.

Data 3 remains the same, while data 2 will be distributed over the newly added node Server4.

At this point, the study of "how to understand the 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.

Share To

Servers

Wechat

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

12
Report