Memcached Distributed Deployment Solution design (including PHP code)

Source: Internet
Author: User
Tags hash md5 memcached php code


A memcache usually does not meet our requirements, which requires a distributed deployment. memcached distributed deployment scenarios usually take two approaches, one is the normal hash distribution, the other is the consistent hash distribution. This article will use PHP as the client, to analyze the two scenarios.
    Normal hash distribution:
<?php
Function test ($key = ' name ') {
    $md 5 = substr ( MD5 ($key), 0, 8);
    $seed = 31;
    $hash = 0;
    for ($i =0 $i <8; $i + +) {
        $hash = $hash * $seed + O RD ($MD 5[$i]);
   }
    return $hash & 0x7fffffff;
}

$memcacheList = Array (
Array (' Host ' => ' 192.168.1.2 ', ' Port ' =>6379),
Array (' Host ' => ' 192.168.1.3 ', ' Port ' =>6379),
Array (' Host ' => ' 192.168.1.4 ', ' Port ' =>6379),
Array (' Host ' => ' 192.168.1.5 ', ' Port ' =>6379),
);
$key = ' username ';
$value = ' Lane ';
Get hash based on key
$hash = $this->test ($key);
$count = count ($memcacheList);
$memcache = $memcacheList [$hash% $count];
$MC = new Memcached ($memcache);
$MC->set ($key, $value);
?>

The code is very simple, a hash function, according to the required key, will he MD5 to take the first 8 digits, and then after the hash algorithm returned an integer. Modulo this integer to the total number of servers. What you get is the number of the server list. The disadvantage of this approach is that the number of servers changed, the same key different hash, will not get the value.

Second, consistent hash distribution

The consistent hash may also result in loss of data, but the loss is minimal.
Imagine 2 of 32-1 as a circle, and the list of servers is listed above. Depending on the location of the key through the hash algorithm on the ring, then the desired server position in front of the key position in the nearest (clockwise).

<?php
Class flexihash{
   /server list
    private $serverList = Array ();
   /Whether to sort
    private $isSort = false;

   /**
     * Description:hash function to return the incoming key as an integer
      * @param string $key
     * @return int
     */
&NBSP;&NBSP;&N Bsp Private Function Myhash ($key) {
        $md 5 = substr (MD5 ($key), 0, 8);
        $seed = 31;
        $hash = 0;
        for ($i =0; $i <8; $i + +) {
             $hash = $hash * $seed + ord ($md 5[$i]);
       }
        return $hash & 0x7fffffff;
   }

   /**
     * Description: Add new server
     * @param $ Server
     */
    public Function Addserver ($server) {
         $hash = $this->myhash ($server);
        if (!isset ($this->serverlist[$hash])) {
             $this->serverlist[$hash] = $server;
       }
        $this->issort = false;
        return true;
   }

/**
* Description: Delete specified server
* @param $server
* @return BOOL
*/
Public Function Removeserver ($server) {
$hash = $this->myhash ($server);
if (Isset ($this->serverlist[$hash])) {
unset ($this->serverlist[$hash]);
}
$this->issort = false;
return true;
}

/**
* Description: Returns the server information for an operation based on the key to be manipulated
* @param $key
* @return Mixed
*/
Public Function Lookup ($key) {
The specified keyhash out of an integer
$hash = $this->myhash ($key);
if ($this->issort!== true) {
Krsort ($this->serverlist);
$this->issort = false;
}
foreach ($this->serverlist as $key => $server) {
if ($key <= $hash) {
return $server;
}
}
Return Array_pop ($this->serverlist);
}
}
How to use
$MC = new Flexihash ();
$MC->addserver (' 192.168.1.2 ');
$MC->addserver (' 192.168.1.3 ');
$MC->addserver (' 192.168.1.4 ');
$MC->addserver (' 192.168.1.5 ');

Echo ' Key=key1, the server that operates is: '. $MC->lookup (' Key1 '). ' <br> ';
Echo ' Key=key1, the server that operates is: '. $MC->lookup (' Key2 '). ' <br> ';
Echo ' Key=key1, the server that operates is: '. $MC->lookup (' Key3 '). ' <br> ';
Echo ' Key=key1, the server that operates is: '. $MC->lookup (' Key4 '). ' <br> ';
Echo ' Key=key1, the server that operates is: '. $MC->lookup (' Key5 '). ' <br> ';
?

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.