Tuesday, April 07, 2009

memcached replication

Wow... finally a solution that provides replication in memcache - repcached.

You can have a look at it repcached.lab.klab.org.

They provide two types of packages

1. a pached memcache source, which can be directly compiled.
2. a patch which can be applied to the memcache source and then compiled.

So, i downloaded the memcached-(version)-repcached-(version).tar.gz source and simply compiled it.

./configure --enable-replication
make
sudo make install


Note : When you enable replication, you cannot do --enable-threads.

I started two instances of memcached on ports 11211 & 11222

jayant@gamegeek:~/php$ memcached -p 11211 -m 64 -x 127.0.0.1 -v
replication: connect (peer=127.0.0.1:11212)
replication: marugoto copying
replication: close
replication: listen

jayant@gamegeek:~/php$ memcached -p 11222 -m 64 -x 127.0.0.1 -v
replication: connect (peer=127.0.0.1:11212)
replication: marugoto copying
replication: start


Now set and get a value on instance on port 11211

jayant@gamegeek:~$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set hello 0 0 5
world
STORED
get hello
VALUE hello 0 5
world
END


Connect to port 11222 and try getting this value

jayant@gamegeek:~$ telnet localhost 11222
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get hello
VALUE hello 0 5
world
END


Try the reverse as well

On 11222
<---snip-->
set key 0 0 5
myval
STORED
get key
VALUE key 0 5
myval
END
<---snip-->


On 11212
<---snip-->
get key
VALUE key 0 5
myval
END
<---snip-->


Suppose the master goes down (in this case lets assume that the memcached on port 11211 goes down). So, we redirect all traffic on port 11222. But later when memcached on port 11211 comes up, the data should be automatically replicated on the new instance. Lets kill the memcache on port 11211 and restart it

On port 11211

Killed
jayant@gamegeek:~/php$ memcached -p 11211 -m 64 -x 127.0.0.1 -v
replication: connect (peer=127.0.0.1:11212)
replication: marugoto copying
replication: start


On port 11222

<---snip-->
replication: close
replication: listen
replication: accept
replication: marugoto start
replication: marugoto 2
replication: marugoto owari
<---snip-->


Lets see if the data has been replicated on port 11211

jayant@gamegeek:~$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get hello
VALUE hello 0 5
world
END
get key
VALUE key 0 5
myval
END


Bingo...
Please share your experience if you have tried it on a live scenario with large number of sets and gets.

6 comments:

DjTeej said...

How is it replicating across servers if you are only specify a localhost address?

gamegeek said...

I have just taken an example. You could run repcached on two different machines and check out replication between them...

I dont see why they should not work...

Unknown said...

Thanks for you post. The repcached wiki has disappeared, and it's very helpful to have alternative sources fo information like this.

I've set this up with replication between two separate virtual machines. Works just fine.

Anonymous said...

Great. We needed this. How do we specify which is the "master" ? Lets say I have 3 servers and need to make the box 2 as "master".

Dileep said...

Thanks for this article.

Great help.

When we deploy it on 3 different machines, how do we specify which is the master ?

Anonymous said...

Thank you very much for this post.