PHP implementation of memcache distributed [consistent hash algorithm]

Source: Internet
Author: User
Recently looking at some of the distributed aspects of the article, so just use PHP to achieve consistency hash to practice practiced hand, used to be the most primitive hash to do distributed, when the production process to add or delete a memcache will result in all the data failure, consistency hash is to solve this problem, To minimize the failure data, the relevant information can be Google a bit!

PHP implementation of a certain lack of efficiency, if you want to high efficiency, or write extension is better
After testing, 5 memcache, each memcache generated 100 virtual nodes, set plus get1000 times, and a single memcache direct set plus get 5 times times slower, so the efficiency is general, need to be optimized!
Implementation process:

Memcache configuration ip+ Port + virtual node serial number to do hash, use is CRC32, form a closed loop.

CRC32 the key to be operated on

Binary method finds the closest virtual node in a virtual node loop

Extract the real memcache IP and port from the virtual node and make a singleton connection

 * @copyright copyright©2003-2012 phpd.cn* @license */class memcachehashmap {Private $_node = array ();        Private $_nodedata = Array ();        Private $_keynode = 0;                Private $_memcache = null;                 Number of virtual nodes per physical server [Note: The greater the number of nodes, the better the cache distribution uniformity, while the set get operation, but also more resources, 10 physical servers, using 200 more reasonable] private $_virtualnodenum = 200;                                                Private Function __construct () {/* put config file */$config = Array (                                                ' 127.0.0.1:11211 ', ' 127.0.0.1:11212 ', ' 127.0.0.1:11213 ', ' 127.0.0.1:112                                                        14 ', ' 127.0.0.1:11215 ');                if (! $config) throw new Exception (' Cache config NULL ');       foreach ($config as $key = = $value) {                 for ($i = 0; $i < $this->_virtualnodenum; $i + +) {$this->_node[sp rintf ("%u", CRC32 ($value. '_' . $i)] = $value. '_' .                        $i;        }} ksort ($this->_node); } Private Function __clone () {}/** * Singleton, guaranteed only one instance */static public function ge                Tinstance () {static $MEMCACHEOBJ = null;                if (!is_object ($MEMCACHEOBJ)) {$memcacheObj = new self ();        } return $MEMCACHEOBJ; }/** * Connect to a physical memcache server after a consistent hash based on key * @param string $key */Private fun                Ction _connectmemcache ($key) {$this->_nodedata = Array_keys ($this->_node);                $this->_keynode = sprintf ("%u", CRC32 ($key));                $nodeKey = $this->_findservernode (); If you go beyond the ring, use the dichotomy to find the nearest one, and then the head and tail of the ring to determine the closest node               if ($this->_keynode > End ($this->_nodedata)) {$this->_keynode-= End ($t                        His->_nodedata);                        $nodeKey 2 = $this->_findservernode ();                if (ABS ($nodeKey 2-$this->_keynode) < ABS ($nodeKey-$this->_keynode)) $nodeKey = $nodeKey 2;                } var_dump ($this->_node[$nodeKey]);                List ($config, $num) = Explode (' _ ', $this->_node[$nodeKey]);                if (! $config) throw new Exception (' Cache config Error ');                        if (!isset ($this->_memcache[$config])) {$this->_memcache[$config] = new Memcache;                        List ($host, $port) = Explode (': ', $config);                $this->_memcache[$config]->connect ($host, $port);        } return $this->_memcache[$config];         }/** * uses dichotomy to find the nearest node from the virtual memcache node * @param unknown_type $m* @param unknown_type $b */Private Function _findservernode ($m = 0, $b = 0) {$total = count ($t            His->_nodedata);            if ($total! = 0 && $b = = 0) $b = $total-1;                if ($m < $b) {$avg = Intval (($m + $b)/2);                if ($this->_nodedata[$avg] = = $this->_keynode) return $this->_nodedata[$avg]; ElseIf ($this->_keynode < $this->_nodedata[$avg] && ($avg-1 >= 0)) return $this->_findservernode                ($m, $avg-1);            else return $this->_findservernode ($avg +1, $b); if (ABS ($this->_nodedata[$b]-$this->_keynode) < ABS ($this->_nodedata[$m]-$this->_keyno                de)) return $this->_nodedata[$b];        else return $this->_nodedata[$m]; Public function set ($key, $value, $expire = 0) {return $this->_connectmemcache ($key)-&        Gt;set ($key, Json_encode ($value), 0, $expire); }                Public function Add ($key, $value, $expire = 0) {return $this->_connectmemcache ($key)-&G        T;add ($key, Json_encode ($value), 0, $expire); The Public function get ($key) {return Json_decode ($this->_connectmemcache ($key)->get ($k        EY), true);        Public Function Delete ($key) {return $this->_connectmemcache ($key)->delete ($key); }        }
$runData [' begin_time '] = Microtime (TRUE);//test 10,000 times set plus getfor ($i =0; $i <10000; $i + +) {        $key = MD5 (Mt_rand ());        $b = Memcachehashmap::getinstance ()->set ($key, Time (), 10);} Var_dump (Number_format (Microtime (True)-$runData [' begin_time '],6); $runData [' begin_time '] = Microtime (true); $m = new Memcache; $m->connect (' 127.0.0.1 ', 11211); for ($i =0; $i <10000; $i + +) {        $key = MD5 (Mt_rand ());        $b = $m->set ($key, Time (), 0, 10);} Var_dump (Number_format (Microtime (True)-$runData [' Begin_time '],6]);

Test result, consistent hash distribution efficiency is 5 times times different than that of native single table.

  • 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.