Take the 2-minute tour ×

Using PHP's memcache and memcached Both of them have addServer.

So I add 2 different memcache servers running on different hardware and even on different ports. When I "set", "add" or "increment" -- which one will be choosen with memcache and which one with memcached? If one of the servers becomes unavailable, and then become available again, what happens?

And how do I turn on redunandancy/replication of data on both?

Say I want data to be stored both to server A and server B, and when one is unavailble, and then available again it gets back to whats on other server.

I think this can be done with memcache cluster, but how PHP's client libs memcache and memcached are handling that?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

To get the desired effect some work has to be done on your part. I'd recommend using Membase instead, with replication turned on and persistence turned off to achieve the same effect. But first let me explain:

In both the Memcached and Memcache clients the hash of the key determines which memcache server stores the data. So which server stores the data is really dependent on the key.

Memcached allows the option to have consistent hashing (Memcached::DISTRIBUTION_CONSISTENT or Memcached::OPT_LIBKETAMA_COMPATIBLE) so that if a memache server goes down or a server is added, not all the data is reshuffled.

Not recommended but... To achieve the desired effect (inefficiently), use consistent hashing and:

1.) Check if a server is down. Create a Memcache(d) object with only the server in question and store and retrieve a random piece of data with it. If it works, it's up.

2.) Take your working servers and add them to your server list when creating your Memcached object.

3.) Enjoy!

PS: As servers go up and down, your cache miss rates will too.

share|improve this answer
Also checkout techzone.couchbase.com/wiki/display/membase/… –  Saurav Apr 1 '11 at 0:48
add comment

Your Answer


