memcached-distributed consistent hashing algorithm practice

Source: Internet
Author: User
Tags crc32 memcached sprintf

Interfacehash{function_hash ($key);//calculates the 32-bit value corresponding to the key name}Interfacedistribution{functionLookup$node);//View Landing Point}classConsistentImplementsHashdistribution{protected $_nodes=Array();  Public function_hash ($key)    {        return sprintf("%u",CRC32($key)); }     Public functionLookup$key)    {       $point=$this->_hash ($key); $node= Current($this->_nodes);//The value corresponding to the smallest key is paid to node       foreach($this->_nodes as $k=$v){           if($k>=$point){                $node=$v;  Break; }       }       return $node; }     Public functionAddNodes ($node)    {        //Record server node        $this->_nodes[$this->_hash ($node)] =$node; $this-sortnodes (); }     Public functionsortnodes () {Ksort($this->_nodes,sort_regular); }     Public functiongetnodes () {return $this-_nodes; }}$con=Newconsistent ();$con->addnodes (' a ');$con->addnodes (' B ');$con->addnodes (' C ');Echo' Existing server nodes are: ';Print_r($con-getnodes ());Echo' The landing point for ' name '.$con->_hash (' Titleasdas '). "<br/>";Echo' should be written in '.$con->lookup (' Titleasdas '). " On the server ";

The above code simply implements the distribution of data to different servers. But the disadvantage is obvious. With this implementation of the distribution when the server is down, the server that is behind this server needs to assume the full workload of the server. So, the following strengthened a bit.

Interfacehash{function_hash ($key);//calculates the 32-bit value corresponding to the key name}Interfacedistribution{functionLookup$node);//View Landing Point}classConsistentImplementsHashdistribution{protected $_positions=Array(); protected $_nodes=Array(); protected $_mul= 64;  Public function_hash ($key)    {        return sprintf("%u",CRC32($key)); }     Public functionLookup$key)    {       $point=$this->_hash ($key); $node= Current($this->_positions);//The value corresponding to the smallest key is paid to node       foreach($this->_positions as $k=$v){           if($k>=$point){                $node=$v;  Break; }       }       return $node; }     Public functionAddpos ($node)    {        //Record server node         for($i= 0;$i<$this->_mul;$i++){            $pos=$this->_hash ($node.‘ -‘.$i); $this->_positions[$pos] =$node; }        $this-Sortpos (); }     Public functionSortpos () {Ksort($this->_positions,sort_regular); }     Public functionGetPos () {return $this-_positions; }}$con=Newconsistent ();$con->addpos (' a ');$con->addpos (' B ');$con->addpos (' C ');Echo' Existing server nodes are: ';Print_r($con-GetPos ());Echo' The landing point for ' name '.$con->_hash (' Titleasdas '). "<br/>";Echo' should be written in '.$con->lookup (' Titleasdas '). " On the server ";

Generate 64 different nodes for each server. And then sort them. This allows multiple servers to be evenly distributed across different node locations. When one of the servers down also does not occur when one of the servers all assume the pressure of that server.

For the understanding of the distributed algorithm is still insufficient, the above views if there are errors in the hope that the predecessors feel free, thank you very much.

memcached-distributed consistent hashing algorithm practice

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.