How to do memcached cluster and how to be highly available when the website visit reaches a certain level, is the next question to be discussed.
There is a text to describe the "memcached cluster"
Memcached How to handle fault-tolerant?
No Deal! :) In the case of memcached node failure, there is no need for the cluster to do any fault-tolerant processing. If a node fails, the measures to be taken depend entirely on the user. When a node fails, here are a few scenarios to choose from:
* Ignore it! There are many other nodes that can deal with the effect of node failure before the failed node is restored or replaced.
* Remove the failed node from the list of nodes. Be careful with this operation! By default (the remainder hash algorithm), the client adds or removes nodes, causing all cached data to be unavailable! Because the list of nodes for the hash reference changes, most of the keys are mapped to different nodes (as they were) because of the change in the hash value.
* Start the hot standby node and take over the IP occupied by the failed node. This prevents hash disturbances (hashing chaos).
According to the above, memcached one of the nodes fails, memcached itself is not any strategy to maintain the failure of forwarding, which is an unacceptable fact for large systems.
To illustrate:
When the client connection is written to multiple server-side IP addresses, the client will automatically distribute the cached data on each of the different machines:
Defect Description:
If one of the cache nodes of the machine down, then the client's cached data will be lost a part of the figure is described in the red font "losed 33% cache Data", that part of the data is completely gone! If it is the user's critical information then play big,:
Solution: Using the Cache proxy Server
Using Magent cache proxy to prevent a single point of the phenomenon, the cache proxy can also do backup, through the client connection to the cache proxy server, the cache proxy Server connection cache server, the cache proxy server can connect more than one memcached machine, as shown, the accessories list is as follows:
Magent Proxy Server: 2 units, respectively, 192.168.1.2:12000, 192.168.1.3:12000
memcached master server: 3 units, respectively, 192.168.1.4:11211, 192.168.1.5:11211, 192.168.1.6:11211
Memcached Server: 2 units, respectively, 192.168.1.5:11211, 192.168.1.6:11211
Build memcahced Server:
Compile and install and run 192.168.1.8 on 192.168.1.4, 192.168.1.5, 192.168.1.6, 192.168.1.7, memcached, respectively,
Reference: CentOS6.3 compile and install memcached
Build magent Proxy Server:
Compile and install magent [CSDN download magent] on 192.168.1.2 and 192.168.1.3 respectively
Under #编译安装安装magent到/usr/local/
4 |
wget -c http://memagent.googlecode.com/files/magent-0.6. tar .gz |
5 |
tar xzvf ./magent-0.6. tar .gz |
7 |
sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile |
9 |
cp ./magent /usr/bin/magent |
Note: During compilation, there are several errors encountered during the process of error resolution, please refer to:
CentOS6.3 compile install memcached cluster Distributed Cache Agent Magent-0.6 Error rollup
Magent Command Detailed:
-H This message
-U UID
-G GID
-P port, default is 11211. (0 to disable TCP support)
-S Ip:port, set memcached server IP and port
-B ip:port, set backup memcached server IP and port
-l IP, local bind IP address, default is 0.0.0.0
-N number, set Max connections, default is 4096
-D do not go to background
-K Use Ketama key allocation algorithm
-f file, UNIX socket path to listen on. Default is Off
-I number, max keep alive connections for one memcached server, default is 20
-V Verbose
Run the magent on 192.168.1.2, 192.168.1.3, respectively:
1 |
magent -u root -n 51200 -l 192.168.1.2 -p 12000 -s 192.168.1.4:11211 -s 192.168.1.5:11211 -s 192.168.1.6:11211 -b 192.168.1.7:11211 -b 192.168.1.8:11211 |
Test the distribution of cached data:
Previously, we used PHP to connect more than one memcached server, when doing distributed cache, the reference code is as follows:
1 |
$memcache = new Memcache; |
2 |
$memcache ->addServer( ‘localhost‘ , 11211); |
3 |
$memcache ->addServer( ‘localhost‘ , 11212); |
4 |
$memcache ->addServer( ‘localhost‘ , 11213); |
5 |
for ( $i = 0; $i < 1000; $i ++) |
7 |
$memcache ->set( $i , $i , 0, 1000); |
Now, the code is still the code, but the host is not connected to the memcached server, but the Magent proxy server, to Addserver () method to pass the parameter, the incoming Magent host IP and Port! The test code is as follows:
05 |
$mem ->connect( $host , $port ); |
09 |
$mem ->add( $key1 , $value1 ); |
13 |
$mem ->add( $key2 , $value2 ); |
17 |
$mem ->add( $key3 , $value3 ); |
21 |
$mem ->add( $key4 , $value4 ); |
25 |
$mem ->add( $key5 , $value5 ); |
29 |
$mem ->add( $key6 , $value6 ); |
Description
1, PHP connection magent, the cache key1 to magent,magent according to their own configuration parameters, coupled with a certain hashing algorithm, will be calculated key1 There are 3 main memcached server on a platform, and then the same algorithm, The Key1 is also on one of the 2 standby memcached servers, and a copy of the data is saved. That is, the primary server is distributed storage, at the same time, from the server is also distributed storage;
2, when PHP gets the cache data Key1, Magent once learned that the data stored in the main memcached server hangs, it will be diverted from the standby memcached server to obtain data. Note: The location selection algorithm of the server is the same as when it is stored.
3, there is a flaw, when the down of the main memcached server back to normal, the memcahed is no data, that is, all the data lost, but at this time the standby memcached server will not synchronize data to the primary server.
4, through the memcached management software memadmin (click to download) to see the above data distribution, as follows:
192.168.1.4 SNSGOU6,SNSGOU3
192.168.1.5 SNSGOU4,SNSGOU1
192.168.1.6 SNSGOU5,SNSGOU2
192.168.1.7 SNSGOU5,SNSGOU3,SNSGOU1
192.168.1.8 SNSGOU4,SNSGOU6,SNSGOU2
5, the PHP connection agent, it is best to only one per random randomness, so that once an agent hung up (that is not connected), you can switch to another proxy server. and random to connect, but also to ensure a certain load balance.
6, originally I intend to modify the configuration file php.ini, so that the PHP system sessions (session) through the Magent agent saved to the memcached server, modify the way reference: How does PHP Save the session to memcached? How to save PHP session in a distributed way
However, the Mangent proxy server IP and port affixed to the php.ini (has restarted the relevant server), found that the session was not saved to the memcached, that is, the PHP bottom does not recognize the magent agent, depressed!!!
Reference:
Http://www.javabloger.com/article/memcached-cluster-error-msag.html
Magent compile and install and common errors
Magent do memcached cluster
[Banquet] memcached Proxy Server Software: magent use small note [original]
Magent Configuration Questions
memcached cluster/Distributed/highly available and Magent cache agent build process