Howto: Memcached replication = Repcached
Intro
Implementation of replicated memcached is fairly easy. Even though memcache isn’t suppose to be used in this manner and should be used as violatile service, there is alternative, which can replicate data from one memcached to another. It’s called Repcached. As long as one repcached server is up, you will keep all your data written in either of the repcached servers.
Installation
Basically, you have to download and compile your own version of repcached from source (http://repcached.lab.klab.org), but I was kind and made a .rpm for you in case you use CentOS 6.x and don’t want to install all the build utils needed to compile repcached. Download it here: memcached-1.2.8-repcached-2.2-1.el6.x86_64
Install prerequisites and repcached:
yum install perl-IO-Socket-INET6 libevent rpm -ivh memcached-1.2.8-repcached-2.2-1.el6.x86_64.rpm
Configuration
Repcached expects another parameter "-x"
besides the normal parameters of memcached. Set it in /etc/sysconfig/memcached
, edit line with OPTIONS=""
to:
OPTIONS="-x xxx.xxx.xxx.xxx"
Replace the xxx’s with ip of the other server running repcached. Do the same for the second repcached. You just need to cross link both of them.
Testing
At Host A running repcached:
mc001 ~ $ telnet localhost 11211 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. set test 0 0 4 test STORED
At host B running repcached:
mc002 ~ $ telnet localhost 11211 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. get test VALUE test 0 4 test END
You see key and value got replicated.
Implementation
That’s a little tricky part. When using connection to repcached server use stable version of memcache client from pecl. Do not use 3.0.x version, becouse it’s beta and you will get Segmentation fault, when you restart first repcached server and afterwards restart the second.
You can pass all your php sessions to repcached by editing your php.ini and setting:
[Session] session.save_handler = memcache session.save_path = "tcp://mc001:11211, tcp://mc002:11211"
In /etc/php.d/memcache.ini you should have:
extension=memcache.so [memcache] memcache.maxratio=0 memcache.allow_failover=1 memcache.redundancy=2
Note to the above: redundancy 2 actually isn’t needed, since you are running repcached, but I really like to be sure data was sent to both servers and was written. You can set it to 1 if you wish. Thing of personal preference. (It is double the network troughput to repcached though.)
Be sure that when you are doing "$mc = new Memcache()"
to use this in addServer options:
$mc->addServer( $host, // host (mc001,mc002) 11211, // port false, // persistent (for some reason balancing doesn't work when you use persistent connections) 50, // weight (choose whatever you wish...) 1, // timeout (time to wait before considering repcached server down) 1, // retry interval (retry dead server after this many seconds) true // status (this is important, have to be true, or else memcache client will blacklist dead repcached server) );
Test your application by alternate swithing each repcached on and off. You shouldn’t be logged out of your application.

session.save_handler = memcache
session.save_handler = memcached <- Is better
Sure, you can use newer version of session control which is memcached, but in that case I have to point out that you have to remove the “tcp://” part of the above config, or it will not work. Thank you for your comment.
Few days ago I was installing a SW solution that utilizes memcached-repcached to one of our clients just to stumble into a lot of errors while compiling the code.
Sir, I want to say that finding your RPM literally saved my ass
Thank you very much for sharing your package.
I’m glad it helped you!
Hi Alen,
Thanks for sharing repcached stack.
I am wondering if Repcached can support replica count. Say I have 5 MemCached server nodes, I only need data be replicated to 2 of the server nodes (distributed), if I deploy Repcached to all 5 nodes, does this mean I will end up having 5 copies of the same data?
Thanks,
~Jing
Yes, you would have 5 copies of the same data. Repcached was intended to have exact copy, since memcached doesn’t have it, so that once one node fails, the other can take over with same exact data.
Since the data is on all instances – which instance will the client library hit first? Does it follow the order they are add in (addServer) ?
Client library is not changed. It will hit the one it should by calculating the hash. You should see how memcached client works.