Consistent ring Hash algorithm. NET implementation

Source: Internet
Author: User

A major use of the consistent ring Hash algorithm is to solve the Memcache server down machine problem. The purpose is to minimize the impact of a Memcache server after it is added or removed. I will not describe the theory. There is too much information on the Internet. Please search for it by yourself. A ConsistencyRing class is written here to implement the algorithm. The specific code is downloaded here. Test class: public static void Test () {ConsistencyRing cr = new ConsistencyRing (); Console. writeLine ("=============== AddServer Test ==============="); cr. addServer ("1"); cr. addServer ("5"); cr. addServer ("9"); cr. addServer ("11"); cr. addServer ("3"); Console. writeLine ("=============== RemoveServer Test =================="); cr. removeServer ("5"); cr. removeServer ("3"); cr. removeServer ("1"); cr. removeServer ("9");} {Co NsistencyRing cr = new ConsistencyRing (); cr. addServer ("1"); cr. addServer ("5"); cr. addServer ("9"); cr. addServer ("11"); cr. addServer ("3"); Console. writeLine ("============== GetServerIp Test =============="); string key; string serverIp; key = "A1"; serverIp = cr [key]; Console. writeLine ("A1 IP:" + serverIp); cr. removeServer ("9"); serverIp = cr [key]; Console. writeLine ("A1 IP:" + serverIp) ;}} AddSer above For demonstration purposes, the parameters in ver/RemoveServer are changed to a concise numeric string. You can use a string like 192.168.1.1 for transmission. Running result:

Related Article

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.