Memcached Notes and summaries (5) common hash distributions and consistent hash distributions for Memcached

Source: Internet
Author: User
Tags crc32

PHP implementation of common Hash distribution algorithm

First assume there are 2 servers: 127.0.0.1:11211 and 192.168.186.129:11211

When the stored key passes the modulo operation on 2 (2 servers), the server to which the key should be saved is obtained:

<?PHP$server=Array(    Array(' host ' = ' 127.0.0.1 ', ' port ' = 11211),Array(' host ' = ' 192.168.186.129 ', ' port ' = 11211),);$key= ' Thekey ';$value= ' Thevalue ';//Let's say two servers .$SC=$server[CRC32($key)% 2];Var_dump($SC);//The key should be saved on the first server$MC=NewMemcache ();$MC->connect ($SC[' Host '],$SC[' Port ']);$MC->set ($key,$value);

The result of the Var_dump ($SC) output is:

Array  string ' 127.0.0.1 ' (length=9)  ' port ' = = int 11211

At this point, use Telnet to connect to the native (127.0.0.1:11211) Memcached server, get the key:

When the key should be stored on the second server:

<?PHP$server=Array(    Array(' host ' = ' 127.0.0.1 ', ' port ' = 11211),Array(' host ' = ' 192.168.186.129 ', ' port ' = 11211),);$key= ' thekey% ';$value= ' Thevaluesecond ';//Let's say two servers .$SC=$server[CRC32($key)% 2];Var_dump($SC);//The key should be saved on the second server$MC=NewMemcache ();$MC->connect ($SC[' Host '],$SC[' Port ']);$MC->set ($key,$value);

The result of the Var_dump ($SC) output is:

Array  string ' 192.168.186.129 ' (length=15)  ' port ' = = int 11211

At this point, use Telnet to connect to the native (192.168.186.129:11211) Memcached server, get the key:

The disadvantage of the common hash distribution is that when the number of servers changes, the same key after the hash, the results with the server will not increase or decrease the server before the result may be different. For example: There are 8 servers, after 1, and 7 left, 8 servers $key% 8 = 0, $key% 7 = 0, at this time a hit (hits), if $key% 8 = 0,%key% 7 = 1, then miss.

To minimize the amount of lost data, a consistent hashing algorithm (consistent Hashing) can be used.

Consistent hashing algorithm

Step 1. Imagine a 32-bit integer (0~2^32-1) as a ring, 0 as the head of the ring, and 2^32-1 as the tail of the ring.

Step 2. The key is processed into integers by the Hash function:

$key 1 CRC32 ($key 1); $key 2 CRC32 ($key 2); $key 3 CRC32 ($key 3); $key 4 CRC32 ($key 4);

Step 3. Map the Memcached group to the ring. Use the Hash function to process the IP address of the server into integers:

$server 1 CRC32 (' 127.0.0.1 '); $server 2 CRC32 (' 192.168.186.129 '); $server 3 CRC32 (' 192.168.186.130 ');

With the above steps, both the key and the server are mapped to the ring.

Step 4. Map the data to the server.

, Key1 falls on server 3, key 4 and key 3 fall on server 2, and key 2 falls on server 1.

Step 5. Remove Server

When server 2 is down, only the data between server 3 and server 2 in the ring (key 3 and key 4) is affected, which is the data mapped to server 2.

Step 6. Add Server

If you are adding server 4, by mapping it will appear between key 3 and key 4, then the data between server 3 and server 4 will be affected (key 4). Re-map the key 4 to server 4.

Memcached Notes and summaries (5) common hash distributions and consistent hash distributions for Memcached

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.