Distributed Caching with Memcached

Memcached Is Born

One day, sick of how painful it is to cache efficiently in mod_perl applications, I started dreaming. I realized we had a lot of spare memory available around the network, and I wanted to use it somehow. If you're a Perl programmer strolling through CPAN, you find an abundance of Cache::* modules. The interface to almost all of them is a dictionary. If you're fortunate enough to have missed Computer Science 101, a dictionary is the name of the abstract data type that maps keys to values. Perl people call that an associative array or a hash, short for hash table. A hash table is a specific type of data structure that provides a dictionary interface.

I wanted a global hash table that all Web processes on all machines could access simultaneously, instantly seeing one another's changes. I'd use that for my cache. And because memory is cheap, networks are fast and I don't trust servers to stay alive, I wanted it spread out over all our machines. I did a quick search, found nothing and started building it.

Each Memcached server instance listens on a user-defined IP and port. The basic idea is you run Memcached instances all over your network, wherever you have free memory and your application uses them all. It's even useful to run multiple instances on the same machine, if that machine is 32-bit and has more total memory than the kernel makes available to a single process. For example, while we were learning our lesson on scaling out and not up, we picked up a ridiculously expensive machine that happens to have 12GB of memory. Nowadays, we use it for a number of miscellaneous tasks, one of which is running five 2GB Memcached instances. That gives us 10GB more memory in our global cache from a single machine, even though each process on 32-bit Linux usually can address only 3GB of memory.

The trick to Memcached is that for a given key, it needs to pick the same Memcached node consistently to handle that key, all while spreading out storage (keys) evenly across all nodes. It wouldn't work to store the key foo on machine 1 and then later have another process try to load foo from machine 2. Fortunately, this isn't a hard problem to solve. We simply can think of all the Memcached nodes on the network as buckets in a hash table.

How Memcached Works

Figure 1. The Memcached client library is responsible for sending requests to the correct servers.

Step 1: the application requests keys foo, bar and baz using the client library, which calculates key hash values, determining which Memcached server should receive requests.

Step 2: the Memcached client sends parallel requests to all relevant Memcached servers.

Step 3: the Memcached servers send responses to the client library.

Step 4: the Memcached client library aggregates responses for the application.

If you know how a hash table works, skim along. If you're new to hashes, here's a quick overview. A hash table is implemented as an array of buckets. Each bucket (array element) contains a list of nodes, with each node containing [key, value]. This list later is searched to find the node containing the right key. Most hashes start small and dynamically resize over time as the lists of the buckets get too long.

A request to get/set a key with a value requires that the key be run through a hash function. A hash function is a one-way function mapping a key (be it numeric or string) to some number that is going to be the bucket number. Once the bucket number has been calculated, the list of nodes for that bucket is searched, looking for the node with the given key. If it's not found, a new one can be added to the list.

So how does this relate to Memcached? Memcached presents to the user a dictionary interface (key -> value), but it's implemented internally as a two-layer hash. The first layer is implemented in the client library; it decides which Memcached server to send the request to by hashing the key onto a list of virtual buckets, each one representing a Memcached server. Once there, the selected Memcached server uses a typical hash table.

Each Memcached instance is totally independent, and does not communicate with the others. Each instance drops items used least recently by default to make room for new items. The server provides many statistics you can use to find query/hit/miss rates for your entire Memcached farm. If a server fails, the clients can be configured to route around the dead machine or machines and use the remaining active servers. This behavior is optional, because the application must be prepared to deal with receiving possibly stale information from a flapping node. When off, requests for keys on a dead server simply result in a cache miss to the application. With a sufficiently large Memcached farm on enough unique hosts, a dead machine shouldn't have much impact on global hit rates.



Comment viewing options


Very nice and informative

anand sarwade's picture

Very nice and informative article.


iehsan's picture


JKnight's picture



Could you help me?
Thank a lot for support.

how to configure mem cahce in centos 5.4

Anonymous's picture


Anonymous's picture




Does NCache has client for Memcached?

Anonymous's picture


Does NCache has client for Memcached?

Anonymous's picture

Yes , NCache does have a client for Memcached.


Can you provide the distributed File system used in facebook

DIMEcyborgs's picture


Gear6 Web Cache?

SFcacheguy's picture


On the Gear6 memcached distribution

Mark Atwood's picture

Hi! I'm the Director of Community Development for Gear6.








.. Mark Atwood

if i have 3 server each

Anton Ongsono's picture


Distributed Caching using NCache

Jim Proint's picture




Team NCache

how to do distribut memcache

Anton Ongsono's picture


It's very nice artice Thanks

Wesley23's picture

It's very nice artice
Thanks for sharing this very good and helpful topic and comments

Hi, whenever I am storing

Anonymous's picture


I am using Danga's memcache client.
can u tell me where could the problem be ??

great article

Praful Todkar's picture

Great article on memcached.

There is a small confusion in my mind.



memcached is great !

mosh's picture

hello ;]
It's very nice artice
memcached is very powerfull module
for website with high traffic

few days ago I intall it on my forum counter strike
now i have load 1-20
thx for memcached :)
thx for great artice :)

greetings, mosh

thanks a lot!

seanlin's picture

i'm looking for this kind of solution everywhere!
thanks a lot

Deployed a system using

rkenshin's picture

Deployed a system using memcached through Hibernate. Pretty decent.
Looking into mixing Mysql for read-only tables and memcached
Thx for the article


memcached on a single server

Micheal's picture

Hi folks,


class memcacheoperations {

function insert($q) {
// insert into memcache and insert into mysql blah..


Memcache class available in PECL

Adam Nelson's picture



Nati Shalom's picture







You can read more on our site

Nati S.

can Java client be used with 'C' memcached server

Jason's picture


If you are using Java you

Nati Shalom's picture


Nati S.

I'm currently working for a

Yaron A.'s picture


Java client with the C memchaced works very well.

You can use Prevayler for jav

Anonymous's picture

You can use Prevayler for java.
its persistence, auto recoverable, etc.

Re: Distributed Caching with Memcached

obiwantcp's picture




shanx.lu's picture

I use memcached to store my count data.and set
memcached -d -m 2048 -p 9876
include_once "MemCachedClient.inc.php";
$options["debug"] = false;
$memc = new MemCachedClient($options);
$path = "dongdong";
if (!$get)
if ($i != 0)
$show = 1;
$show = $get + 1;

here is the bug:
MemCache: replace dongdong = 10952
sock_to_host(): Failed to connect to
sock_to_host(): Host is not available.
sock_to_host(): Host is not available.
sock_to_host(): Host is not available.

Re: Distributed Caching with Memcached

Anonymous's picture

Interesting, but could someone please fix the missing image?

Thank You

distributed HASHing

Anonymous's picture

You might be interested in the distributed hash stuff in

Re: Distributed Caching with Memcached

timstarling's picture




Tim Starling (Wikipedia developer)

At Wikipedia, we've also

Nati Shalom's picture



Nati S.

Write Once Scale Anywhere

in regards to the comments by Tim Starling

George Daswani's picture

