Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link MemCached server together?

Tags:

memcached

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?

like image 851
GateKiller Avatar asked Dec 09 '08 12:12

GateKiller


People also ask

Can't connect to Memcached server?

In short, the error “could not connect to Memcached” occurs due to many reasons like an error in the configuration file, missing the Memcached module, missing port and many more.


4 Answers

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.

like image 61
Javier Avatar answered Oct 05 '22 09:10

Javier


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.

like image 43
Tristan Warner-Smith Avatar answered Oct 05 '22 10:10

Tristan Warner-Smith


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

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

like image 21
Daniel Von Fange Avatar answered Oct 05 '22 09:10

Daniel Von Fange


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.

like image 20
Ian Ringrose Avatar answered Oct 05 '22 08:10

Ian Ringrose