Another discussion on the Uniform hash algorithm

Source: Internet
Author: User

Consistent hashing algorithm was put forward in the paper consistent hashing and random trees in 1997, and is widely used in the cache system.
1 Basic Scenarios
For example, if you have n cache server (hereafter referred to as cache), then how to map an object to n cache, you are likely to use a common method like the following to calculate the hash value of object, and then map evenly to the n cache;
Hash (object)%N
Everything is running normally, consider the following two cases;
11 Cache server M down (this must be considered in the actual application) so that all objects mapped to the cache m will be invalidated, what to do, need to remove the cache m from the cache, when the cache is N-1, the mapping formula becomes HA Sh (object)% (N-1);
2 because of the access aggravating, need to add the cache, this time the cache is n+1, mapping formula into a hash (object)% (n+1);
What does 1 and 2 mean? This means that suddenly almost all of the caches are dead. For the server, this is a disaster, flood-like access will be directly rushed back to the server;
Consider the third problem, because the hardware capabilities are getting stronger, you may want to add more nodes to do more work, obviously the above hash algorithm can not be done.
Is there any way to change this situation, this is consistent hashing ...
2 hash Algorithm and monotonicity
A measure of the Hash algorithm is monotonicity (monotonicity), which is defined as follows:
Monotonicity refers to the addition of a new buffer to the system if some content has been allocated to the corresponding buffer by hashing. The result of the hash should be to ensure that the original allocated content can be mapped to a new buffer without being mapped to another buffer in the old buffer collection.
Easy to see, above the simple hash algorithm hash (object)%N difficult to meet the monotonicity requirements.
Principle of the 3 consistent hashing algorithm
Consistent hashing is a hash algorithm, in a nutshell, when removing/adding a cache, it can change the existing key mappings as small as possible, and satisfy the monotonic requirements as much as necessary.
Here are the basic principles of the consistent hashing algorithm in 5 steps.
3.1 Ring Hash Space
Consider that the usual hash algorithm is to map value to a key value of 32, which is the value space of the 0~2^32-1; we can think of this space as a ring with a first (0) tail (2^32-1), as shown in Figure 1 below.

Figure 1 Ring Hash space
3.2 Mapping objects to the hash space
Next consider 4 objects Object1~object4, the hash function calculated by the hash value of key on the ring distribution 2 is shown.
Hash (object1) = Key1;
... ...
Hash (OBJECT4) = Key4;

Figure 2 Key value distributions for 4 objects
3.3 Mapping the cache to the hash space
The basic idea of consistent hashing is to map both the object and the cache to the same hash value space, and use the same hash algorithm.
Assuming that there are currently a A, a, a and C a total of 3 caches, then its mapping results will be 3, they are in the hash space, the corresponding hash value arrangement.
Hash (cache a) = key A;
... ...
Hash (cache c) = key C;

Figure 3 Key value distributions for cache and objects

Speaking of which, by the way, the cache hash calculation, the general method can use the cache machine's IP address or machine name as a hash input.
3.4 Mapping objects to cache
Now that both the cache and the object have been mapped to the hash value space using the same hash algorithm, the next thing to consider is how to map the object to the cache.
In this annular space, if you start from the object's key value in a clockwise direction until you meet a cache, the object is stored on the cache because the hash value of the object and the cache is fixed, so the cache must be unique and deterministic. Did you find the mapping method for the object and cache?! The
still continues with the above example (see Figure 3), then the object Object1 will be stored on cache a according to the above method, Object2 and object3 correspond to cache C; Object4 corresponds to cache B;
3.5 Observe the change of the cache
said before, through the hash and then the method of redundancy brings the biggest problem is not to meet the monotony, when the cache changes, the cache will fail, and then the background server caused a huge impact, and now to analyze and analyze consistent Hashing algorithm.
3.5.1 Remove the cache
Consider assuming that cache B hangs, according to the mapping method described above, this will only be affected by those objects that go counterclockwise through cache B until the next cache (cache C), which is mapped to the cache Those objects on the B. The
therefore only needs to change the object Object4, to remap it to cache C, see Figure 4.

Figure 4 Cache mapping after cache B is removed
3.5.2 Add Cache
Consider adding a new cache D, assuming that in this ring hash space, cache D is mapped to the object Object2 and Object3. The only things that will be affected are those objects that traverse the cache D counterclockwise until the next cache (cache B), which is also mapped to a portion of the object on cache C, to remap the objects to cache d.

The

Therefore only needs to change the object object2 and remap it to cache D; see Figure 5.

Figure 5 Mapping relationship after adding cache D
4 virtual node
Consider another indicator of the hash algorithm is the balance (Balance), defined as follows:
Balance
Balance means that the result of the hash can be distributed to all buffers as far as possible So that all the buffer space can be exploited. The
hash algorithm is not guaranteed to be absolutely balanced, and if the cache is small, the object cannot be mapped evenly to the cache, as in the example above, where only cache A and cache C are deployed, in 4 objects, cache a only stores the object 1, while cache C stores Object2, Object3, and Object4, and the distribution is very uneven.
In order to solve this situation, consistent hashing introduces the concept of "virtual node", which can be defined as follows:
"Virtual node" is the actual node in the hash space of the replica (replica), a real node corresponding to the To do a "virtual node", the corresponding number is also a "number of copies", "Virtual node" in the hash space in the hash value. The
still takes the case of deploying only cache A and cache C, as we have seen in Figure 4, where the cache distribution is not uniform. Now we introduce the virtual node, and set the "number of copies" to 2, which means there will be 4 "virtual nodes", the cache A1, cache A2 represents the cache A; Cache C1, Cache C2 represents the cache C; Suppose a more ideal situation See Figure 6.

Mapping relationship after "virtual node" is introduced in Figure 6

At this point, the mapping of the object to the virtual node is:
Objec1->cache A2; objec2->cache A1; Objec3->cache C1; Objec4->cache C2;
So objects Object1 and Object2 are mapped to cache a, and object3 and Object4 are mapped to cache C; The balance has improved a lot.
After the "Virtual node" is introduced, the mapping relationship is transformed from {object---node} to {Object-and-virtual node}. The mapping relationship 7 is shown when querying the cache of an object.

Figure 7 The cache where the object is queried

The hash calculation of "virtual node" can be based on the IP address of the corresponding node plus the number suffix. For example, assume that the IP address of cache A is 202.168.14.241.
Before introducing "Virtual node", calculate the hash value of cache A:
Hash ("202.168.14.241");
After introducing "virtual node", compute the hash value of the "virtual section" point cache A1 and cache A2:
Hash ("202.168.14.241#1"); Cache A1
Hash ("202.168.14.241#2"); Cache A2
5 Summary
The basic principle of consistent hashing is these, the specific distribution of such theoretical analysis should be very complex, but generally not used.
Http://weblogs.java.net/blog/2007/11/27/consistent-hashing above has a Java version of the example, you can refer to.
Http://blog.csdn.net/mayongzhan/archive/2009/06/25/4298834.aspx reproduced a PHP version of the implementation code.
Http://www.codeproject.com/KB/recipes/lib-conhash.aspx C language version

Another discussion on the Uniform hash algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.