Howto: Memcached replication = Repcached

Intro

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

Comments

  1. By jaru

    Reply

  2. Reply

  3. By M.

    Reply

  4. Reply

  5. By Jing Song

    Reply

  6. Reply

  7. By Sem

    Reply

    • Reply

Leave a Reply

help-hint.png
Purpose of the commenting system is to share your experience. I encourage you to post feedback with your own suggestions, ideas or optimizations regarding the topic of a blog post. What commenting system isn't for, is asking questions about similar issues of yours and requesting support for it. Blog post is provided as is and I am not here to solve all your problems. Please bear that in mind and try to avoid posting such comments. I do take privilege to remove comment from my blog for any reason whatsoever. Usually I do it when I sense a comment was posted only for spam/seo reasons or is out of blog post's topic. Thank you for reading this, now you may continue :)
 

Your email address will not be published. Required fields are marked *