tags:

views:

1385

answers:

4

I'm looking into using MemCached for a web application I am developing and after researching MemCached over the past few days, I have come across a question I could not find the answer to.

How do you link Memcached server together or how do you replicate data between MemCached server?

Additionally: Is this functionality controlled by the servers or the clients and how?

+4  A: 

when you set several servers, the client libraries use a first hash to pick one where to store each key/data pair. that means that there's no replication, and also that every client has to use the same set of servers.

pros:

  • almost zero overhead, storage and bandwidth grow linearly.
  • server code is kept simple and reliable.

cons:

  • any change in the set of servers (one goes down, or you add a new one) suddenly invalidates (almost) the whole cache.
  • you have to be sure to use the same algorithm on every client.

if you have control to the client's code, you can simply store each key/data pair twice on two servers. just be sure to search on the same places when reading from a different client.

Javier
Surely that's not the only way of doing it?
Marcus Downing
Consistant hashing can significantly limit the number of keys invalidated by their re-hashing to a different server. This is also dependant on the number of servers you have though.
Alister Bulman
+1  A: 

It sounds like you wish to have caches that can cope with machines rebooting etc if so…

In a lot of case (assuming you are not writing Facebook) a RDMS is fast enough for caching. Just create a table that has a key and a blob column. If the RDBS server has enough ram, all the data will be in RAM and just saved to disk so as to allow recovery.

Remember this could be a separate server(s) from your main database server.

If you wish to get more fancy and are using a high-end RDMS, you may be able to set up change notifications on the queries that are used to build the “cached data” that delete out-of-date rows from the cache.

Someone you can set up triggers to clear invalid rows from the cache, however this can be very complex very quickly.

Ian Ringrose
+1  A: 

I've used BeITMemcached and in that you create an instance of MemcacheClient and set the servers you want to use, just as strings.

At that point the client itself determines which of the servers it has available to put different items into. You never know which an item will be in.

Check here to see how the servers handle failover.

The easiest thing is to have a repopulate mechanism. In my case, I store several hundred objects in memcache which come out of a database. I can just call repopulate and put them all back in there. Whenever I add, update or delete them to the database, I make those same calls to memcache.

TreeUK
+1  A: 

http://repcached.lab.klab.org/

Also, the PHP PECL memcache client can replicate data to multiple servers, see memcache.redundancy.

Daniel Von Fange