Hash hashes (ii) consistent hash (C + + implementation)

Source: Internet
Author: User

Consistent Hash

  A distributed hash (DHT) implementation algorithm, proposed by MIT in 1997, is designed to solve hot spot problems in the Internet and is often used for distributed, load balancing, and so on.

Principle

 A consistent hash is a special hashing algorithm. After using a consistent hashing algorithm, the average hash table only needs to remap the key words, in which the number of keywords is the number of map nodes. However, in a traditional hash table, it is almost necessary to remap all the keywords to add or remove a mapping node.

The original mapping is probably this, such as, not when the addition or deletion of a new node may cause the mapping of each node will change, if the yellow node represents the server, then each update of the number of servers will cause each server on the blue map node will change, When the number of clusters is large, the changes required for each node deletion will be too large.

  And in the consistency of the hash, the mapping is like this, for example, the general consistency hashing value range is -2^32~2^32, distributed on a circle

 The following pictures are more ugly, just make it easy to see ~ ~

The yellow node acts as a mapping node (real node), and the blue node is the key node that needs to be mapped to the map node

    • First, look at the diagram on the left, and hash the 8 blue keys on a circle with a range of 0~2^32.

    • Next, select three yellow nodes as the mapping nodes, and set the blue node to the yellow node in the clockwise direction of the circle.

    • Finally, because the 1-node load is 4, the largest, then we to reduce the load situation of 1 nodes, increase the yellow mapping node 4, still in the clockwise direction to modify the original mapping, then only need to change the Blue node 7, 8 and yellow node 1

Realize

Generally for convenience, we refer to the yellow map node is called the real node, which is fixed, and the blue node is called the virtual node, the virtual node needs to map to the real node, each time the deletion of real nodes will affect only the nearest node.

The Consistenthash algorithm is implemented here using C + +

  In terms of storage nodes, this program is simply the use of linked lists, the best way is of course the red and black trees, of course, for the sake of simplicity, using the list, mainly to understand the principle of consistent hash ~ ~

SOURCE Download: Https://github.com/yxd123/algorithm-notes/tree/master/ConsistentHash

Reference

http://blog.csdn.net/cywosp/article/details/23397179

http://zh.wikipedia.org/wiki/consistent Hash

Http://baike.baidu.com/view/1588037.htm?fr=aladdin


This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 3.0 non-localized version license agreement in the same way. Welcome reprint, please specify the source:
Reprinted from: Cococo Point http://www.cnblogs.com/coder2012

Hash hashes (ii) consistent hash (C + + implementation)

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.