PHP implementation of a consistent hash algorithm steps

Source: Internet
Author: User
This time to bring you the PHP implementation of the consistency hash algorithm steps, the PHP implementation of the consistency hash algorithm considerations are what, the following is the actual case, together to see.

The consistent hashing algorithm is a common calculation in distributed systems. method, why use this algorithm?

For example: A distributed storage system, to store data to a specific node (server), in the case of no change in the number of servers, if the ordinary hash and then the total number of servers to model the method (such as the total amount of key% servers), if there is a server outage or need to increase the server, The problem is out. After the same key is hashed, the result of the modulo with the total number of servers is different from the previous result, which results in the loss of previously saved data. Therefore, a consistency hash (consistent Hashing) distribution algorithm is introduced.

The data with a hash function (such as MD5,SHA1), mapped to a ring, as shown, the data in the storage, based on the hash algorithm to calculate the hash value of the key, corresponding to the position in the ring, such as the K1 corresponds to the position shown in the map, and then along the clockwise direction to find server Node B, The K1 is then stored in the Node B.

If the b node goes down, the data on B falls to the C node, as shown in

In this way, only the C node will be affected, and the data for other nodes A and D will not be affected. However, the problem is that this will cause the C node to be overloaded, because the C node assumes the data of the B node, so the C node is prone to downtime, resulting in uneven distribution.

In order to solve this problem, the concept of "virtual node" is introduced: imagine that there are many "virtual nodes" on the empty upper ring, a real server node corresponds to multiple virtual nodes, and when the data is stored, the virtual node is found in the clockwise direction of the ring, and the corresponding real server node is found. Such as

The figure of A1, A2, B1, B2, C1, C2, D1, D2 are virtual nodes, machine a load storage A1, A2 data, machine B load Storage B1, B2 data, machine C load Storage C1, C2 data. Because these virtual nodes are large in number and evenly distributed, they do not cause "avalanche" phenomena.

PHP implementation of consistent hashing algorithm

An interface is given below

/** * Consistent Hash Implementation interface * Interface Consistenthash */interface consistenthash{//convert string to hash value public function chash ($STR);//Add a service To the server list, public function addserver ($server); Remove a server from the server public function removeserver ($server); Locate the appropriate server in the current server list to hold the data public function lookup ($key);}

This interface defines 4 methods, chash (processing strings to hash values), addserver (Adding a server), Removeserver (removing a server), lookup (finding a server to store data)

A concrete implementation of this interface is given below

/** * Concrete Consistency Hash Implementation * Author Chenqionghe * Class myconsistenthash */class Myconsistenthash implements consistenthash{public $ serverlist = Array (); Server column list Public $virtualPos = Array ();  The location of the virtual node public $virtualPosNum = 5; Each node corresponds to 5 virtual nodes/** * Converts a string to a 32-bit unsigned integer hash value * @param $str * @return int */Public Function Chash ($str) {$str = MD5 ($str  ); Return sprintf ('%u ', CRC32 ($STR)); /** * Locate the appropriate server in the current server list * @param $key key Name * @return Mixed return server IP address */Public function lookup ($key) {$point = $this->chash ($key);//The hash value of the landing point $finalServer = current ($this->virtualpos);//First Take the smallest node on the ring as the result foreach ($this-    >virtualpos as $pos = $server) {if ($point <= $pos) {$finalServer = $server;   Break }} reset ($this->virtualpos);//Reset the ring pointer to the first return $finalServer; /** * Add a server to the server list * @param $server server IP address * @return BOOL */Public Function Addserver ($server) {if (!isset ($thi  s->serverlist[$server]) {for ($i =0; $i < $this->virtualposnum; $i + +) {  $pos = $this->chash ($server. '-' .    $i);    $this->virtualpos[$pos] = $server;   $this->serverlist[$server] [] = $pos;  } ksort ($this->virtualpos,sort_numeric); } return TRUE; /** * Remove a server (loop through all the virtual nodes, delete the virtual node with the server address) * @param $key * @return BOOL */Public Function Removeserver ($key) {if (Isse T ($this->serverlist[$key])) {//delete corresponding virtual node foreach ($this->serverlist[$key] as $pos) {unset ($this->virtual   pos[$pos]);  }//delete the corresponding server unset ($this->serverlist[$key]); } return TRUE; }}

$hashServer = new Myconsistenthash (); $hashServer->addserver (' 192.168.1.1 '); $hashServer->addserver (' 192.168.1.2 '), $hashServer->addserver (' 192.168.1.3 '), $hashServer->addserver (' 192.168.1.4 '); $hashServer- >addserver (' 192.168.1.5 '); $hashServer->addserver (' 192.168.1.6 '); $hashServer->addserver (' 192.168.1.7 ') ; $hashServer->addserver (' 192.168.1.8 '); $hashServer->addserver (' 192.168.1.9 '); $hashServer->addserver (' 192.168.1.10 '); echo "Add 10 servers 192.168.1.1~192.168.1.10<br/>"; echo "Save Key1 to Server:". $hashServer->lookup (' Key1 '). ' <br/> '; echo "save Key2 to Server:". $hashServer->lookup (' Key2 '). ' <br/> '; echo "Save Key3 to Server:". $hashServer->lookup (' Key3 '). ' <br/> '; echo "save Key4 to Server:". $hashServer->lookup (' Key4 '). ' <br/> '; echo "save Key5 to Server:". $hashServer->lookup (' Key5 '). ' <br/> '; echo "save Key6 to Server:". $hashServer->lookup (' Key6 '). ' <br/> '; echo "save Key7 to Server:". $hashServer->lookup (' Key7 '). ' <br/> '; echo "save Key8 to Server:". $hashServer->lookup (' Key8 '). ' <br/> '; echo "save Key9 to Server:". $hashServer->lookup (' Key9 '). ' <br/> '; echo "save Key10 to Server:". $hashServer->lookup (' Key10 '). ' <br/> '; Echo ' 

The operation results are as follows

10 Additional Servers 192.168.1.1~192.168.1.10
Save Key1 to server:192.168.1.2
Save Key2 to server:192.168.1.1
Save Key3 to server:192.168.1.6
Save Key4 to server:192.168.1.8
Save Key5 to server:192.168.1.9
Save Key6 to server:192.168.1.10
Save Key7 to server:192.168.1.7
Save Key8 to server:192.168.1.4
Save Key9 to server:192.168.1.7
Save Key10 to server:192.168.1.4
Remove a server 192.168.1.2
Save Key1 to server:192.168.1.7
Save Key2 to server:192.168.1.1
Save Key3 to server:192.168.1.6
Save Key4 to server:192.168.1.8
Save Key5 to server:192.168.1.9
Save Key6 to server:192.168.1.10
Save Key7 to server:192.168.1.7
Save Key8 to server:192.168.1.4
Save Key9 to server:192.168.1.7
Save Key10 to server:192.168.1.4
Remove a server 192.168.1.6
Save Key1 to server:192.168.1.7
Save Key2 to server:192.168.1.1
Save Key3 to server:192.168.1.3
Save Key4 to server:192.168.1.8
Save Key5 to server:192.168.1.9
Save Key6 to server:192.168.1.10
Save Key7 to server:192.168.1.7
Save Key8 to server:192.168.1.4
Save Key9 to server:192.168.1.7
Save Key10 to server:192.168.1.4
Remove a server 192.168.1.8
Save Key1 to server:192.168.1.7
Save Key2 to server:192.168.1.1
Save Key3 to server:192.168.1.3
Save Key4 to server:192.168.1.10
Save Key5 to server:192.168.1.9
Save Key6 to server:192.168.1.10
Save Key7 to server:192.168.1.7
Save Key8 to server:192.168.1.4
Save Key9 to server:192.168.1.7
Save Key10 to server:192.168.1.4
Remove a server 192.168.1.2
Save Key1 to server:192.168.1.7
Save Key2 to server:192.168.1.1
Save Key3 to server:192.168.1.3
Save Key4 to server:192.168.1.10
Save Key5 to server:192.168.1.9
Save Key6 to server:192.168.1.10
Save Key7 to server:192.168.1.7
Save Key8 to server:192.168.1.4
Save Key9 to server:192.168.1.7
Save Key10 to server:192.168.1.4
Add a server 192.168.1.11
Save Key1 to server:192.168.1.7
Save Key2 to server:192.168.1.1
Save Key3 to server:192.168.1.11
Save Key4 to server:192.168.1.10
Save Key5 to server:192.168.1.9
Save Key6 to server:192.168.1.10
Save Key7 to server:192.168.1.7
Save Key8 to server:192.168.1.4
Save Key9 to server:192.168.1.7
Save Key10 to server:192.168.1.4

Yes, it is possible to see that, after using a consistent hash, the data integrity and uniformity are guaranteed to be maximized without increasing the server or reducing the server.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

thinkphp Frame Auto-fill principle and usage explanation

PHP Decorator Mode Use detailed

Related Article

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.