Consistent Hash Algorithm and its PHP implementation

Source: Internet
Author: User
The following describes how to use PHP to implement a consistent hash algorithm. you can refer to the following server load balancer algorithms that you can choose when you are doing server load balancer, including Round Robin, HASH, Least Connection, Response Time, and Weighted. Hash algorithms are the most commonly used algorithms.

Typical application scenarios are:There are N servers that provide the cache service. load balancing needs to be performed on the servers, and requests are evenly distributed to each server. Each machine is responsible for 1/N of services.

A common algorithm is to obtain the remainder (hash () mod) of the hash result. N): For the machine number from 0 to the N-1, according to the custom hash () algorithm, for each request hash () value according to N modulo, obtain the remainder I, then, the request is distributed to the machine numbered I. However, such an algorithm has a fatal problem. if a machine goes down, requests that fall into the machine cannot be processed correctly, in this case, the server to be removed from the algorithm, there will be (N-1)/N of the server cache data needs to be re-computed; if a new machine, the cache data of a server with N/(N + 1) needs to be recalculated. This is usually unacceptable for systems (because it means that a large amount of cache is invalid or data needs to be transferred ). So how to design a load balancing policy to minimize the number of affected requests?

The Consistent Hashing algorithm is used in Memcached, Key-Value Store, Bittorrent DHT, and LVS. it can be said that Consistent Hashing is the preferred algorithm for distributed system load balancing.

1. description of the Consistent Hashing algorithm

The following uses the Consisten Hashing algorithm in Memcached as an example.
Because the hash algorithm results are generally of the unsigned int type, the hash function results should be evenly distributed between [0,232-1, if we use 232 points to perform even cutting for a ring, first calculate the hash value of the server (node) based on the hash (key) function and distribute it to 0 ~ 232 on the circle.

Use the same hash (key) function to find the hash value of the key for storing data and map it to the circle. Search clockwise from the location where the data is mapped, and save the data to the first server (node.

Principle of Consistent Hashing

When a node is added, only the data of the first node in the counterclockwise direction of the new node on the ring will be affected. When deleting a node, only the data of the first node that was originally deleted clockwise on the ring will be affected, therefore, the Consistent Hashing solves the hash value bumps caused by adding and deleting nodes in server load balancer.

Consistent Hashing

Virtual node (virtual nodes ):The reason why we want to introduce virtual nodes is that when the number of servers (nodes) is small (for example, there are only three servers) calculating the hash value of a node is not evenly distributed (sparse) on the ring, and load imbalance of each node still occurs. A virtual node can be considered as a replica of an actual node (replicas), which is essentially the same as the actual node (the key is not the same ). After a virtual node is introduced, the number of actual servers (nodes) is increased according to a certain proportion (for example, 200 times) and its hash (key) is calculated) values are evenly distributed to the ring. During load balancing, the hash value of the virtual node actually falls into the actual node. Because all actual nodes are copied to virtual nodes in the same proportion, the distribution of hash values in the ring is even when the number of nodes is small.

Impact of virtual nodes on Consistent Hashing results

It can be seen that when the number of nodes is 10 and the number of virtual nodes on each actual node is-times that of the actual node, the results are still balanced.

The text in section 3rd contains: "But such an algorithm has a fatal problem. if a machine goes down, requests that fall on the machine cannot be processed correctly, in this case, remove the server from the algorithm.(N-1)/NServer cache data needsRe-computing;"

Why is it (N-1)/N? Explanation:

For example, if there are three machines, the distribution of hash values 1-6 on these three machines is:
Host 1:14
Host 2:25
Host 3: 3 6
If one instance is suspended, only two instances are left, and the modulus is 2, the distribution will be:
Host 1:13 5
Host 2:24 6

As you can see, there are only two data locations unchanged: 1, 2, and four locations changed. the ratio of the six data locations is 4/6 = 2/3, too much data is affected, so too much data needs to be loaded from the database to the cache again, seriously affecting performance.

[Consistent hashing method]
In the hash modulo mentioned above, the modulus is relatively small, generally the number of loads, and the essence of consistent hashing is to take the modulus relatively large, to the power of 32 of 2 minus 1, it is a maximum 32-bit integer. Then, you can easily arrange data orientation. The figure is quite intuitive.
The following is a PHP implementation of the consistent hash algorithm.Click to download

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.