Consistenthash consistent Hash

Source: Internet
Author: User

Distributed server layout needs to use the hash algorithm, set the node has N, the common hashing algorithm: key%n, in the case of node increase and decrease, for the simple logical calculation hash is no problem, but if the node cluster provides storage function, The effect is not ideal. The idea at this point is that the previous nodes are not affected when the nodes are added, and only those that are associated with reducing the node are affected when the node is reduced. Then we can not simply use the method of%, there is a solution is to abstract the object key and server node key, spaced out on a ring above, install the clockwise way object key to fetch the nearest server node. This method is Consistenthash, and the Java version of the algorithm is posted below:

Import Java.util.collection;import Java.util.sortedmap;import Java.util.treemap;public class ConsistentHash<T > {Private final hashfunction hashfunction;//hash algorithm private final int numberofreplicas;//virtual node number private final Sorted Map<integer, t> circle = new Treemap<integer, t> (); Public Consistenthash (hashfunction hashfunction, int numberofreplicas, collection<t> nodes) {//Physical node This.hash   Function = hashfunction;   This.numberofreplicas = Numberofreplicas;   for (T node:nodes) {Add (node); }} public void Add (T-node) {for (int i = 0; i < Numberofreplicas; i++) {Circle.put (Hashfunction.hash (node.tost   Ring () + i), node); }} public void remove (T-node) {for (int i = 0; i < Numberofreplicas; i++) {Circle.remove (Hashfunction.hash (nod   E.tostring () + i));   }} public T get (Object key) {//Key algorithm if (Circle.isempty ()) {return null;   }//Calculate hash value int hash = Hashfunction.hash (key); If this hash value is not included if (!circle.containskey (haSH) {sortedmap<integer, t> tailmap = Circle.tailmap (hash); hash = Tailmap.isempty ()?   Circle.firstkey (): Tailmap.firstkey (); } return Circle.get (hash); }}

An implementation scenario:

Using the Tree Management Server node, each server node holds a list of hash keys that can be indexed to it (the actual application is mostly distributed IP hashes), and when accessed, calculates the hash value of key and compares it to the tree-managed list to find the corresponding mapping server. Tree managed server nodes are not very good decisions can only be based on the keyhash value and the number of servers for Virtual node expansion.


Consistenthash consistent Hash

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.