This article was reproduced from: http://blog.csdn.net/kongqz/article/details/6695417
I. Overview
1, our Memcache client uses the consistency hash algorithm Ketama to choose the data storage node. Unlike the conventional hash algorithm, we only hash the key we want to store the data, and allocate it to different node storage. The consistent hash algorithm computes the hash of the server where we want to store the data, and then confirms where each key is stored.
2, the application of the conventional hash algorithm and its drawbacks
The most common way is the way of the hash modulo. For example, the number of machines available in the cluster is n, then the data request with a key value of K is simple enough to be routed to a hash (k) mod n corresponding machine. Indeed, this structure is simple and practical. but in some of the fast-growing web systems, such solutions still have some drawbacks. With the increase of system access pressure, the cache system has to increase the corresponding speed and data carrying capacity of the cluster by increasing the machine node. Increase the machine means in accordance with the way of the hash, in the time of increasing the machine node, a large number of cache, cache data need to be re-established, or even the overall cache data migration, the moment will bring a very high system load on the DB, set the DB server downtime.
3, the design of distributed cache system, the consistency hash algorithm can help us solve what problems?
The core point of distributed cache design: When designing a distributed caching system, we need to equalize the distribution of the key, and after adding the cache server, the cache will be migrated to a minimum.
The consistency hash algorithm mentioned here Ketama the practice is: Select the specific machine node is not only rely on the key to cache the hash itself, but the machine node itself is also a hash operation .
Second, consistent hash algorithm scenario description (reproduced)
1. Hash machine node
First find the Machine node hash value (how to calculate the Machine node hash?) IP can be used as a hash parameter. Of course there are other ways), and then distribute it to a ring in the 0~2^32 (clockwise distribution). As shown in the following:
There are machines in the cluster: A, B, C, D, E five machines, through a certain hash algorithm we distribute it to the ring as shown.
2. Access method
If there is a write cache request where the key value is K, the calculator hash value is hash (k), the hash (k) corresponds to a point in the graph –1 ring, if the point corresponding to a specific machine node is not mapped, then look clockwise until the first time to find the node with the mapped machine, The node is the target node that is determined, and if it exceeds the 2^32 still cannot find the node, hit the first machine node. For example, the hash (K) value is between A~b, then the hit machine node should be a B node (such as).
3, increase the processing of nodes
For example, –1, on the basis of the original cluster to add a machine f, the increase process is as follows:
The hash value of the computer node that maps the machine to a node in the ring, such as:
After adding the Machine node F, the access policy does not change, still according to (2) in the manner of access, when the cache is still unavoidable, the data that cannot be hit is the hash (K) in increasing the node before the data between C~f . Although there is still a hit problem caused by the increase of the node, but compared with the traditional method of hashing, the consistency hash has reduced the data to a minimum.
Consistent Hashing minimizes the re-distribution of hash keys . In addition, in order to achieve a better load balancing effect, often in a small number of servers need to increase the virtual node to ensure that the server can be evenly distributed on the ring. Because of the general hash method, the map location of the server is unevenly distributed. using the idea of a virtual node, allocate 100~200 points on the circle for each physical node (server). This can suppress uneven distribution and minimize cache redistribution when the server is increasing or decreasing. The user data is mapped on a virtual node, which means that the user data is actually stored on the actual physical server represented by the virtual node.
Here is a diagram that describes the virtual nodes that need to be added for each physical server.
Third, the Spymemcache source to demonstrate the virtual node application
1, the above description of the consistency hash algorithm has a potential problem is:
(1), the node hash will be unevenly distributed on the ring, so that a large number of keys in the search for nodes, there will be key hit each node probability difference is large, unable to achieve effective load balancing.
(2), if there are three nodes node1,node2,node3, distributed in the ring when the three nodes close to the key on the ring to find the node, a large number of key clockwise is always assigned to Node2, and the other two nodes are found to be very small probability.
2, the solution of this problem can be:
Improve the hash algorithm, evenly distribute each node to the ring; [citation] Use the idea of a virtual node to allocate 100~200 points for each physical node (server) on a circle. This can suppress uneven distribution and minimize cache redistribution when the server is increasing or decreasing. The user data is mapped on a virtual node, which means that the user data is actually stored on the actual physical server represented by the virtual node.
When viewing the spy Memcached client, it was found that it adopted a hash algorithm called Ketama to solve Memcached distributed problem with the idea of virtual node.
3. Source Code Description
The client uses TreeMap to store all nodes and simulates a circular logical relationship. In this loop, the nodes are sequentially related, so the TreeMap key must implement the comparator interface.
How does that node fit into this ring?
1 protected voidSetketamanodes (list<memcachednode>nodes) {2Treemap<long, memcachednode> newnodemap =NewTreemap<long, memcachednode>();3 intnumreps=config.getnoderepetitions ();4 for(Memcachednode node:nodes) {5 //Ketama does some special work with MD5 where it reuses chunks.6 if(HashAlg = =Hashalgorithm.ketama_hash) {7 for(intI=0; I<numreps/4; i++) {8 byte[] digest=hashalgorithm.computemd5 (Config.getkeyfornode (node, i));9 for(intH=0;h<4; h++) {TenLong k = ((Long) (digest[3+h*4]&0xFF) << -) One| ((Long) (digest[2+h*4]&0xFF) << -) A| ((Long) (digest[1+h*4]&0xFF) <<8) -| (digest[h*4]&0xFF); - Newnodemap.put (k, node); theGetLogger (). Debug ("Adding node%s in position%d", node, k); - } - - } +}Else { - for(intI=0; i<numreps; i++) { + Newnodemap.put (Hashalg.hash (Config.getkeyfornode (node, i), node); A } at } - } -Assert newnodemap.size () = = Numreps *nodes.size (); -Ketamanodes = Newnodemap;
The above process can be summarized as follows: Four virtual nodes for a group, the Getkeyfornode method to get this set of virtual node NAME,MD5 encoding, each virtual junction corresponding to Md5 code 16 bytes of 4, forming a Long value, As the only key in this virtual node in the ring. The 10th row K Why is a long type? This is because the long type implements the comparator interface.
After processing the distribution of the formal node on the ring, you can start the game where key is looking for nodes on the ring.
For each key, you have to complete the above steps: Calculate the MD5, according to the MD5 byte array, through the kemata hash algorithm to get the position of key in this ring.
V. Summary
1, the consistent hash algorithm is only to help us reduce the number of machines in the cache cluster increase or decrease, the cache data can be rebuilt at least. As long as the number of servers in the cache cluster changes, data hit issues inevitably occur
2, for the data distribution balance problem, through the idea of virtual node to achieve a balanced distribution. Of course, the fewer cache server nodes we need, the more virtual nodes are needed to balance the load.
3, our cache client does not maintain a map to record where each key is stored, all through the hash of the key and Cacheserver (perhaps the IP can be used as a parameter) hash to calculate the current key should be stored on which node.
memcached Consistent hashing algorithm