How do you cache common consistency hashes in a split-way?

Source: Internet
Author: User

How do you cache common consistency hashes in a split-way?

What is the problem of consistent hash?

Look at a scene first

There are n cache servers, and what cache is an object that is mapped to?

The hash value of the object can be computed using a common method and then mapped evenly to n caches

Hash (object)% n

The initial phase works fine, but the problem occurs when the cache server is changed

For example

(1) A cache server is down so that all objects mapped to this cache will be invalidated and this cache needs to be removed, and the mapping formula becomes

Hash (object)% (n-1)

(2) As the access is aggravated, the cache needs to be added, and the mapping formula becomes

Hash (object)% (n+1)

These two situations will almost invalidate all caches, and a large number of accesses will directly access the backend server, causing great stress

Consistent hash algorithm is to solve the problem of ordinary hash, to minimize the loss

The principle of consistent hash

Consistent hash organizes the entire hash value space into a virtual ring

Assume that the value space for a hash function h is 0-2^32-1 (32 times for a 2 2^32)

The entire space is organized in a clockwise direction, and 0 and 232-1 coincide in the 0-point direction.



Each cache server through the hash calculation, according to the hash value clockwise to the ring, can be calculated according to the server's IP or host name

For example there is a B C three servers



You can now map an object with the mapping rule:

The key uses the same hash to calculate the value, and to determine the location of the data on the ring, from this position to walk clockwise, the first cache server encountered is the server it should be located

For example, there are 4 objects that need to be mapped to a cache server



According to the mapping rules

Object 1 A
Object 2, 3, B
Object 4, C

Let's look at the cache server changes and additions

(1) Removal

For example, a appears an exception, need to remove



Object 1 fails and needs to be mapped to B

Only Object 1 is affected

(2) Add

Cache pressure increase, need to add server, new cache server D



Object 2 needs to be remapped to D

Only object 2 is affected

You can see that the consistency hash is much more efficient than the normal hash.

How do you cache common consistency hashes in a split-way?

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.