Although memcached is called a "distributed" cache server, the server does not have a "distributed" function. The server only includes the memory storage function, which is easy to implement. The distributed memcached is fully implemented by the client library. This type of distribution is the biggest feature of memcached.
Basic Environment
In fact, the PHP-based extended Memcache Client has already been implemented and is very stable. First, I would like to explain some terms. Memcache is an open-source project of danga.com, which can be analogous to services such as MySQL. the Memcache extended by PHP is actually connected to Memcache.
First, you can view several other articles in the blog when installing Memcache;
Second, the PHP extension installation, the official address is http://pecl.php.net/package/memcache
Finally, start the Memcache service. for example, start multiple processes through different ports to simulate the distribution:
The code is as follows:
/Usr/local/bin/memcached-d-p 11211-u root-m 10-c 1024-t 8-P/tmp/memcached. pid
/Usr/local/bin/memcached-d-p 11213-u root-m 10-c 1024-t 8-P/tmp/memcached. pid
/Usr/local/bin/memcached-d-p 11214-u root-m 10-c 1024-t 8-P/tmp/memcached. pid
Start three instances and use only 10 MB of memory for testing.
Parameter description:
-D option is to start a daemon,
-M indicates the amount of memory allocated to Memcache. the unit is MB. here is 512 MB,
-U is the user who runs Memcache. here I am root,
-L is the IP address of the listening server. if there are multiple IP addresses, I have specified the IP address 192.168.0.1,
-P is the port for Memcache listening. I have set port 11211 here, preferably port 1024 or above,
-The "c" option is the maximum number of concurrent connections. the default value is 1024. I have set 512 here, which is based on the load of your server,
-P is the pid file for saving Memcache. here I am saving
Distributed deployment
Memcache in the PECL extension of PHP has actually implemented multi-server support in Version 2.0.0 and is now 2.2.5. See the following code.
$memcache = new Memcache;$memcache->addServer('localhost', 11211);$memcache->addServer('localhost', 11213);$memcache->addServer('localhost', 11214);$memStats = $memcache->getExtendedStats();print_r($memStats);
In the previous example, the distributed deployment of Memcache has been implemented. is it very simple.
Benign operation of distributed systems
In actual use of Memcache, the most serious problem is that when you increase or decrease the server, it will lead to a wide range of cache loss, which may lead to a database performance bottleneck. During the test, you can shut down a memcached process to test whether the data exists. for example:
<? Php // comments $ memcache = new Memcache; $ memcache-> addServer ('localhost', 11211) after setting the value for the first time ); // $ memcache-> set ("mykey", "added before 11213"); $ memcache-> addServer ('localhost', 11213); if (! $ Memcache) echo "Connection to memcached failed";/* $ memcache-> set ("str_key", "String to store in memcached "); $ memcache-> set ("num_key", 123); $ object = new StdClass; $ object-> attribute = 'test'; $ memcache-> set ("obj_key ", $ object); $ array = Array ('assoc '=> 123,345,567); $ memcache-> set ("arr_key", $ array ); */var_dump ($ memcache-> get ('MyKey'); var_dump ($ memcache-> get ('Str _ key'); var_dump ($ memcache -> Get ('Num _ key'); var_dump ($ memcache-> get ('obj _ key'); $ memStats = $ memcache-> getExtendedStats (); var_dump ($ memStats);?>
Disabling one of them during testing may cause data loss:
String 'The Value added before 11213 '(length = 35) string 'string to store in memcached' (length = 28) boolean falseboolean false
To avoid this problem, first read the Consistent hashing algorithm. for a Chinese introduction, refer to the memcached comprehensive analysis-4. memcached distributed algorithm, which is implemented by changing the server algorithm selected during access.
Although memcached is called a "distributed" cache server, the server does not have a "distributed" function.
Modify the PHP Memcache extension in the source code of memcache. c
"Memcache. hash_strategy" = standard
Is
"Memcache. hash_strategy" = consistent
Re-compile. at this time, we use the Consistent hashing algorithm to find the server to access data.
Effective test data shows that the use of Consistent hashing can greatly improve the situation of large cache loss when Memcache is added or deleted.
NonConsistentHash: 92% of lookups changed after adding a target to the existing 10NonConsistentHash: 90% of lookups changed after removing 1 of 10 targetsConsistentHash: 6% of lookups changed after adding a target to the existing 10ConsistentHash: 9% of lookups changed after removing 1 of 10 targets
Security Configuration
The Memcache server directly performs operations after being connected through the client without any verification process. in this way, it is dangerous to directly expose the server to the Internet, if data leaks are viewed by other unrelated personnel, the server is infiltrated because Mecache runs with the root permission. Besides, some unknown bugs or buffer overflow may exist, these are all unknown, so the danger is foreseeable.
Intranet access
It is recommended that the access between the two servers is in the intranet format, generally between the Web server and the Memcache server. Generally, the server has two NICs, one pointing to the Internet and the other pointing to the intranet, so that the Web server can access the Memcache server through the intranet Nic, when the Memcache server is started, it listens to the intranet IP address and port, and the access between the intranet can effectively prevent other illegal access.
The code is as follows:
# Memcached-d-m 1024-u root-l 192.168.0.200-p 11211-c 1024-P/tmp/memcached. pid
The Memcache server sets listening to port 11211 of the ip address 192.168.0.200 over the intranet, occupying 1024 MB of memory and allowing a maximum of concurrent connections.
Set firewall
Firewall is a simple and effective method. if both servers are connected to the Internet and Memcache needs to be accessed through an Internet IP address, you can use a firewall or proxy program to filter out illegal access. In Linux, we can use iptables or FreeBSD ipfw to specify rules to prevent unauthorized access. for example, we can set to allow only our Web servers to access our Memcache server, at the same time, other accesses are blocked.
# iptables -F# iptables -P INPUT DROP# iptables -A INPUT -p tcp -s 192.168.0.2 --dport 11211 -j ACCEPT# iptables -A INPUT -p udp -s 192.168.0.2 --dport 11211 -j ACCEPT
The above iptables rule only allows access from the Web server 192.168.0.2 to the Memcache server. it can effectively prevent some illegal access and add other rules to enhance security, this can be done according to your own needs.