The consistent hash distribution algorithm is divided into 4 steps:
Step 1: Think of a 32-bit integer [0 ~ (2^32-1)] as a ring, 0 as the beginning, (2^32-1) as the end, of course, this is just imagination.
Step 2: The key is processed into integers through the hash function. This allows you to find a location on the ring that corresponds to it.
Step 3: Map the memcached server farm to the ring, using the hash function to process the server corresponding IP address.
Step 4: Map the data to the memcached server. Here's how to find the memcached server location for a key: from the position of the current key, go clockwise along the circle, find the nearest memcached server, and save the data for the key to this server.
code example:
<?php/** * Consistent hash distribution * Tianya PHP blog * http://blog.phpha.com */class flexihash{//server List Private $serverList = Array ();//record is Sort private $isSorted = false;//Add a server public function Addserver ($server) {$hash = $this->mhash ($server); if (!isset ($ this->serverlist[$hash]) {$this->serverlist[$hash] = $server;} Need to reorder $this->issorted = False;return TRUE;} Remove a server public function Removeserver ($server) {$hash = $this->mhash ($server); if (Isset ($this->serverlist[$ Hash]) {unset ($this->serverlist[$hash]);} Need to reorder $this->issorted = False;return TRUE;} In the current server list, look for the appropriate server public function lookup ($key) {$hash = $this->mhash ($key);//reverse-sort First operation if (! $this->issorted) { Krsort ($this->serverlist, sort_numeric); $this->issorted = TRUE;} A clockwise direction on the ring looks for a server immediately adjacent to the current key foreach ($this->serverlist as $pos + $server) {if ($hash >= $pos) return $server;} If not found, returns the last server in the clockwise direction return $this->serverlist[count ($this->serverlist)-1];} hash function Private Function Mhash ($key) {$MD 5 = substr (MD5 ($key), 0, 8$seed = $hash = 0;for ($i = 0; $i < 8; $i + +) {$hash = $hash * $seed + ord ($md 5{$i}); $i + +;} return $hash & 0x7FFFFFFF;}}? >
Test:
<?php/** * Consistency Hash Distribution test code * Tianya PHP Blog * http://blog.phpha.com */$hserver = new Flexihash ();//initial 5 servers $hserver-> Addserver ("192.168.1.1"), $hserver->addserver ("192.168.1.2"), $hserver->addserver ("192.168.1.3"); $hserver- >addserver ("192.168.1.4"); $hserver->addserver ("192.168.1.5"); echo "Save Key1 in Server:", $hserver->lookup (' Key1 '), "<br/>"; echo "Save Key2 in Server:", $hserver->lookup (' Key2 '), "<br/>"; Echo ' ================ ===============================<br/> ';//Remove 1 Servers $hserver->removeserver ("192.168.1.4"); echo "Save Key1 in Server: ", $hserver->lookup (' Key1 ')," <br/> "; echo" Save Key2 in Server: ", $hserver->lookup (' Key2 ')," <br /> "; Echo ' ===============================================<br/> ';//Add 1 Servers $hserver->addserver (' 192.168.1.6 '), echo "Save Key1 in Server:", $hserver->lookup (' Key1 '), "<br/>"; echo "Save Key2 in Server:", $hser Ver->lookup (' Key2 ');? >
The test results are as follows: Save Key1 in Server:192.168.1.4save Key2 in Server:192.168.1.2==================================save Key1 in S Erver:192.168.1.3save Key2 in Server:192.168.1.2==================================save key1 in server: 192.168.1.3save Key2 in server:192.168.1.2
Memcached distributed consistent hash algorithm demo