Memcached and SpyMemcached



Blog Development

Hi all, today we’re going to write about Memcached, which we’ve been using for a while now.

Memcached ( ) is a free and open-source memory object caching system, which has a DB integrated version called Coachbase (previously called Membase). The main idea of a memory cache is to hold objects in memory, avoiding unneeded DB queries. 

When using a cache, you need to make sure that the data can be recovered from somewhere else when the cache fails since the data in the cache can be erased when a timeout occurs when it needs to free up some memory, or the cache server fails. An example of this is the daily currency rates (you can query these values once through a web service, and then store it locally in a cache server) since these values don’t change for several hours, you can store them in a local cache, so you don’t query the web service each time a user navigates to your site.

When storing objects in a Memcached server, there are some limits. The maximum size of the object must not exceed 1MB, and the key length has a maximum of 256 characters.

Memcached can be compiled for any Linux distribution (downloaded here) or run in Windows after downloading the ported binaries, which can be found here (

Either way, running and configuring the server is extremely easy. We will be running these examples in Windows, but the Linux way is analogous. The simplest way to run the server is with the command:


This command starts a memcached server, which by default, can hold up to 64MB of objects and listens on port 11211. You can change these parameters with the -m and -p options, respectively. 

There are other parameters you can set when starting up the memcached server, but these two are enough to have a functional memcached server. You can look up the other options in the wiki. Here is an example:

#Runs a memcached server which can hold up to 512MB objects and on port 11230
memcached -m 512 -p 11230

Memcached has clients for several languages (C, C++, Java, PHP, and so on); all the possible clients are listed here. You can even use it from a telnet client.

Since I am mostly into Java, I’ll talk a bit more about the Java clients. According to the client’s page, there are several clients for Java. I didn’t get to try them all, but I went with Spymemcached, since it is the one developed by Couchbase (the creators of memcached), it has support for couchbase and is one of the most recently updated ones. There is another recently updated project called xmemcached, which has nice documentation.

Jumping right into Spymemcached, it is quite straightforward to use it. Just download the jar file, create a client, and start setting keys:

//Create a memcached client (this will also start a thread which will monitor the server availability and communication)
MemcachedClient client=new MemcachedClient(new InetSocketAddress(“localhost”, 11211));
//Just start putting objects in the cache.
//Here,”someKey” is the key under which the object will be stored,
// 3600 is the number of seconds the object will be kept and
// someObject is any Object that implements Serializable.
client.set(“someKey”, 3600, someObject);
//This will synchronously retrieve the object from the cache
Object obj = client.get(“someKey”);

Even having a cluster of Memcached servers is easy, you just create one MemcachedClient with several addresses. The client decides where to store/retrieve the keys with an internal hashing method.

//Create the Memcached client and use it, simple as that
MemcachedClient c=new MemcachedClient(AddrUtil.getAddresses(“localhost:11211 localhost:11212”));

There is also a CacheMap class which allows you to access the cache as it were a Map:

MemcachedClient c=new MemcachedClient(AddrUtil.getAddresses(“localhost:11211”));
//Here, 100 is the default timeout to use when adding elements to the cache, “prefix” is the string that will be prepended to all the keys in this map
CacheMap map = new CacheMap(c,100,”prefix”);
//use map
mapa.put(“k1”, someObject);
//Retrieve the element using the map
Object obj1 = mapa.get(“k1”);
//or even using the MemcachedClient (prepending the prefix we used for this map):
Object obj2 = c.get(“prefixk1”);

Finally, we want to mention several alternatives for a caching system (these examples are designed to work with Java platform):

  1. Apache JCS: It has several caching levels, memory, disk, database, etc. It also seems a bit more complex to install and configure. link
  2. OSCache: Works inside the same VM in which the application is running, but works. It wasn’t updated since long ago.
  3. JCache: It’s an old api, which is getting refreshed with Java EE 7, end of 2012. link

That’s it for now, as simple as that you can start using Memcached. ?

We hope you enjoyed this post. Let me know your comments!

We’ll add some comparisons between using a Database as a cache and Memcached in our next post.

What’s your Reaction?

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you interested in a project like this?

Hire Us