Server Load balancer instancesAlgorithmThere are many such algorithms as round robin, hash, least connection, response time, and weighted). Hash algorithms are the most commonly used algorithms.
A typical application scenario is that there are n servers providing cache services. Load Balancing needs to be performed on the servers, requests are evenly distributed to each server, and 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
It can be seen that there are only two data locations unchanged: 1, 2, and four locations changed, accounting for 4/6 = 2/3 of the total 6 data records.
In this case, too much data is affected, and too much data needs to be loaded from the database to the cache again, seriously affecting the 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.
: Http://zwzweb.googlecode.com/files/Consistent%20Hashing.php