Memcache Story

In computing, memcached is a general-purpose distributed memory caching system that was originally developed by Danga Interactive for LiveJournal, but is now used by many other sites. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached runs on Unix, Linux, Windows and MacOSX and is distributed under a permissive free software license.

Memcached’s APIs provide a giant hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order. Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store, such as a database.

The system is used by sites including YouTube, Reddit, Zynga, Facebook, Orange, and Twitter. Heroku (now part of Salesforce) offers a Couchbase-managed memcached add-on service as part of their platform as a service. Google App Engine, AppScale and Amazon Web Services also offer a memcached service through an API. Memcached is also supported by some popular CMSs such as Drupal, Joomla, and WordPress.


Memcached was first developed by Brad Fitzpatrick for his website LiveJournal, on May 22, 2003.


The system uses a client–server architecture. The servers maintain a key–value associative array; the clients populate this array and query it. Keys are up to 250 bytes long and values can be at most 1 megabyte in size.

Clients use client side libraries to contact the servers which, by default, expose their service at port 11211. Each client knows all servers; the servers do not communicate with each other. If a client wishes to set or read the value corresponding to a certain key, the client’s library first computes a hash of the key to determine the server that will be used. Then it contacts that server. The server will compute a second hash of the key to determine where to store or read the corresponding value.

The servers keep the values in RAM; if a server runs out of RAM, it discards the oldest values. Therefore, clients must treat Memcached as a transitory cache; they cannot assume that data stored in Memcached is still there when they need it. A Memcached-protocol compatible product known as MemcacheDB provides persistent storage.

If all client libraries use the same hashing algorithm to determine servers, then clients can read each other’s cached data; this is obviously desirable.

A typical deployment will have several servers and many clients. However, it is possible to use Memcached on a single computer, acting simultaneously as client and server.


Most deployments of Memcached exist within trusted networks where clients may freely connect to any server. There are cases, however, where Memcached is deployed in untrusted networks or where administrators would like to exercise control over the clients that are connecting. For this purpose Memcached can be compiled with optional SASL authentication support. The SASL support requires the binary protocol.

A presentation at BlackHat USA 2010 revealed that a number of large public websites had left memcached open to inspection, analysis, retrieval, and modification of data.

General questions about memcache

  1. When would I need to use memcache::addServer and what is the difference between this and memcache::connect?
  2. Does memcache overwrite stored values if it runs out of memory, even if the item has not yet expired?
  3. What would I use memcache::getExtendedStats for?
  4. How do I check to see if a connection to memcache already exists and if not, create a connection?
  5. If I have my usual memcache server of ‘localhost’ set up, how would I go about setting up another memcache server on my same dedicated server?
  6. Apart from more memory, what is the benefit of having more than one memcache server?
  7. Should I check for memcache server updates regularly?
  8. Does it use a lot of memory to run memcache::connect at the beginning of each page, even if I am not using it?
  9. When am I likely to return errors and how do I catch these?
  10. Most importantly, if I am using memcache within another class that has several methods that may be called more then once per script, how should I go about initialising the object and connecting to the server within each method?

You’d want to use Memcache::addServer when you need to add more Memcached servers. For example, if you had a really busy website or web app… you’d probably want to have more than one Memcached server running. Memcache::connect is used when you want to start a connection to one of your Memcached servers. Also, according to the Memcache::addServer docs, another difference between Memcache::addServer and Memcache::connect is that with Memcache::addServer, the connection is not established until actually needed.

If Memcached runs out of RAM, it will discard the oldest values.

Memcache::getExtendedStats is used to check information about your Memcached server. For example, if you need to find out how long the server has been up (uptime,) how many connections the server has, or general server usage, this is a great tool.

Probably the easiest way to check if a connection to Memcached already exists is to check your $memcache connection variable to see if it returns TRUE5. If you need to have a persistent connection (that keeps on going even after your script ends,) there is the option to use Memcache::pconnect.

If you want to have two Memcached servers going on… and your first server is already your localhost, you will most likely need to have a separate, distinct server for the second>.

At least one other benefit of having more than one Memcached server is the idea that whenever you diversify your data (or servers,) even when one server goes down… you still have however many other servers there to pick up the pieces. Memcached looks like it is distributed over however many servers you have running… so if a server goes down, you are still losing that part of the cache. But, you do still have other servers up and running to help keep going.

In general, it’s not a bad idea to keep almost any type of software up to date. It looks like Memcached is still a highly active project so you may want to update it when you can. But the essence of Memcached doesn’t seem to change a whole lot over past versions… so, it might not be as critical to update it compared to something like operating system software.

It sounds like the way that Memcached allocates memory for TCP connections (when you make a call to your Memcached server via Memcache::connect,) does end up costing you memory. If you are sure you aren’t going to need that connection on some of your pages, you may want to avoid making that connect call.

Hard to say what type of errors might come up in your code. But, with something like Memcached, you may find errors coming up when you are running out of memory.

Like the answer to question eight, I would still recommend trying to only call that $memcache->connect() in areas where you absolutely need it. You might be using Memcached in a lot of your application or scripts; but there still will probably be places where you won’t need it.

You can leave a response, or trackback from your own site.

12 Responses to “Memcache Story”

  1. Clotildes says:

    it was nice reading this post.

  2. Areusa says:

    thanks this has really helped, as has many of your other articles.

  3. source says:

    Have you considered including some social bookmarking links to these blogs. At the very least for myspace.

    • promignavi says:

      Hi Elsje Donovan, thanks for suggestion, I have added the bookmarking link for myspace, just click on sharethis (at the bottom of the post) and then click on more.. near the Share button.

  4. Daiani says:

    i learned a lot here. thank you so much for your personal help.

  5. nootropic says:

    Very wonderful info can be found on this web site.

  6. rift platinum says:

    Very interesting info!Perfect just what I was looking for!

  7. Edney says:

    recently came across your article and have been reading along. i want to express my admiration of your writing skill. great to know that!

  8. Graphic Design Derry says:

    Hi, i think that i noticed you visited my web site so i came to “go back the desire”.I am attempting to to find things to enhance my site!I assume its adequate to make use of some of your ideas!!

  9. Zen Cart Developer says:

    hey there and thank you to your information ? I have certainly picked up anything new from proper here. I did alternatively expertise some technical points using this web site, as I skilled to reload the website lots of occasions previous to I could get it to load properly. I had been puzzling over if your web host is OK? Not that I’m complaining, however sluggish loading instances times will often impact your placement in google and could injury your quality score if ads and marketing with Adwords. Anyway I’m including this RSS to my email and could look out for a lot more of your respective interesting content. Ensure that you update this again very soon..

  10. Shauna Malkin says:

    Very good data can be observed on this web web site….

Leave a Reply

Designed for Ares Galaxy in Collaboration with Hostgator coupons and Popular Wordpress Themes and unlock iphone 5