Detailed consistent hashing

First, the use Redis cluster

When we use Redis, in order to ensure Redis high availability, improving read and write performance Redis, the easiest way to copy we will call the shots from the composition in the form of Master-Master or Master-Slave, or set up Redis cluster of data separate read and write, similar to a database separate from the primary copy and read. As follows:
Here Insert Picture Description
Also similar to the database, a single table when data needs to be greater than 500W of sub-library sub-table, when a large amount of data when (standard may be different, depending on Redis server capacity) We also can Redis a similar operation is sub-library sub-table.

Suppose that we have a social networking site, you need to use resources Redis store pictures, stored in the format as key-value pairs, key is the name of the picture, value for the image server where the file path, we need to find the file server where the file based on the file name path on the amount of data there are about 2000W, the library is divided according to rules we agreed, random allocation rule is that we can deploy eight cache servers, each containing about 500W of data, and the master-slave replication diagram below :
Here Insert Picture Description
as the rules are random, all of us are likely in any one data set Redis, such as the figure we find a user name is stored as "a.png" picture, because the rule is random, we are not sure specifically on which a Redis server, so we need to 1,2,3,4,4 queries to be able to query (that is, through all the Redis server), this is clearly not the result we want, there is find out about the junior partner may think, random rules not, you can use similar database Library sub-table rule: According to Hash value, modulo, by category, in accordance with common rules certain field values, etc. can come out! Well, according to our theme, we use the Hash way.

Second, the use Redis cluster Hash is

One can imagine that if we use the Hash way, each picture when performing sub-library can be targeted to specific servers, diagram is as follows:
Here Insert Picture Description
the figure above, suppose we are looking for is "a.png", because of 4 servers (excluded from the library), so the formula is hash(a.png) % 4 = 2, to be seen positioning the No. 2 server, so it will not go through all servers, greatly enhance the performance!

Third, the use of Hash issue

Although the above-mentioned manner to enhance the performance, we no longer need to traverse the entire Redis server! However, when the cache using the Hash algorithm, there will be some shortcomings, mainly reflected in the change when the number of servers, all cache location must be changed!

Imagine if four cache server cache can not meet our demand, then how should we do it? Very simple, a few more than the increase cache servers not on the list! Assumptions: We have added a cache server, the number of cache servers becomes 5 sets of four. So the original hash(a.png) % 4 = 2equation becomes hash(a.png) % 5 = ?, the result can be imagined, this is certainly not brought results 2 is that when changes in the number of servers, all cache location must be changed! In other words, when the number of servers is changed, all caches within a certain time is invalid, when the application can not obtain data from the cache when it requests data to the back-end database (remember on one of the " Cache avalanche " ?)!

Similarly, assuming that four cache suddenly have a cache server fails, can not be cached, then we will need to remove the faulty machine, but if you removed a cache server, the cache becomes the number of servers from four 3 sets, will also appear above problems!

So, we should think of ways to prevent this from happening, but because of the above-mentioned Hash algorithm itself, when the cache using modulo law, this situation can not be avoided, in order to solve these problems, Hash algorithm consistency (consistency Hash algorithm) was born!

Fourth, the mystery of consistency Hash Algorithm

Consistency Hash algorithm is to use modulo, just, modulo method just described is performed modulo the number of servers, and consistency pair 2 Hash algorithm , modulo, what does that mean? Briefly, the consistency of the entire hash algorithm Hash value space into a virtual ring, such as the assumed value of the hash function H is a spatial 0-2 32-1 (i.e., a hash value is a 32-bit unsigned shaping), the entire hash ring as follows:
Here Insert Picture Description
the whole space clockwise tissue, represents a positive point above the first annular 0,0 point on the right represents 1, and so on, 2,3,4,5 , 6 ...... until 2^32-1, that is 0the first point of the representative point on the left side 2^32-1, 0and 2^32-1superposed in the direction of zero, we made this 2^32point is called Hash composed annular ring.

The next step for each server using a hash Hash, or may be specifically selected IP hostname of the server as a hash key, so that each machine can determine its position on the hash ring, where it is assumed that the above four after the server using the IP address in the hash space position loop as follows:
Here Insert Picture Description
Next, using the following algorithm to access the appropriate server positioning data: key data using the same hash function hash value is calculated, and determines the position of the data on the ring , clockwise from the position of the ring along the "walking", which is to be targeted to the server of the first server encountered!

For example, we have Object A, Object B, Object C , Object D four data objects, after hashing, the space position on the ring is as follows:
Here Insert Picture Description
The Hash algorithm consistency, data A is set as to the Node A, B is set as to the Node B, C are set as to the Node C, D are set as to the Node D.

Five, fault tolerance consistency Hash algorithms and scalability

Unfortunately now assumed that the Node C is down, this time can see objects A, B, D are not affected, only the objects C are relocated to Node D. Generally, the consistency Hash algorithm, if a server becomes unavailable, the data is only affected server to the ring space in front of a server (i.e., the first server encountered traveling in the counterclockwise direction between the data), the other is not affected, as follows:
Here Insert Picture Description
Let us consider another case, if the additional server Node X, as shown below in the system:
Here Insert Picture Description
in this case the object Object a, B, D are not affected only the object C need to be relocated to the new Node X! Generally, the consistency Hash algorithm, if the additional server, the data is only affected to the new server before the ring space of a server (i.e., the first server encountered traveling in the counterclockwise direction) data between other data will not be affected.

In summary, to increase or decrease the consistency Hash Algorithm node is only a small portion of the ring relocation data space, has good fault tolerance and scalability.

Six, Hash data issue inclined ring

Consistency Hash Algorithm serving node when too easily since the node data segment caused by uneven inclination (cached objects are concentrated on a certain cache server) problems, such as system, only two servers, the distribution of the ring as follows:
Here Insert Picture Description
At this concentration will inevitably lead to large amounts of data to the Node A, and only a very small amount will be positioned on the Node B. In order to address this data skew problem, consistent Hash algorithm introduces virtual node mechanism that calculates a plurality of hash for each service node, each of the calculation results are placed in a location service node, called a virtual node. Specific practices can increase the number behind the IP or hostname of the server to achieve.

The case of the above example, three may be calculated for each virtual server node can then calculate the "Node A # 1", " Node A # 2", "Node A # 3", "Node B # 1" , respectively, "Node B # 2 "," node B # 3 " hash value, thus forming six virtual node:
Here Insert Picture Description
while the data unchanged positioning algorithm, but more virtual node mapping step of the actual node, e.g. target" node a # 1 "," node a # 2 " ," data node a # 3 "three virtual nodes are positioned on node a. This would solve the service node data skew problem came from. In practical applications, usually the virtual node number is set to 32 or greater, so even small service node can do a relatively uniform distribution of data.

Seven, consistency is reflected in where

k: key service data, n: number of clusters machine

  • The traditional way: k% n, where n changes, almost most of the key (not all), will not fall on the machine where the node before the change. That it is, for most of the key terms, add or delete nodes, change will lead to owning node, and is no longer consistent with the original , if it is done with the cache, it will cause the request directly through to the database, causing the so-called avalanche.

Keys: key total number, n business data: the number of clusters machine

  • Consistency hash virtual node way, to let keys can be random evenly distributed to the n.
  • When n increases, the original machine, will each come into the new part of the key to increase the machine, so that all nodes occupy roughly the same number key . Note here that each node is out part, to the new increase in the machine, rather than the above modulo almost entirely been disrupted reassigned.
  • When reducing the time n, shoot down the node, will he had possession of the key, divided equally alive to the machine . Again, here is not disrupted redistribution. I did not hang the machine stored in the key, and change will not migrate, but additional key and then share those previously stored hang some machines.

In summary, consistent hashing Consistent Hashingso-called consistency is key terms. Non-consistent hashing, modulo way, cluster nodes changes, key changes also followed. The consistent hashing, cluster nodes changes, key great situation next is not going to follow the changes. By comparison, it is closer to the same.

Published 158 original articles · won praise 119 · views 810 000 +

Guess you like

Origin blog.csdn.net/u013474436/article/details/104622354