Over a million developers have joined DZone.

Memcached: In-memory Caching System

DZone's Guide to

Memcached: In-memory Caching System

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

Originally developed for LiveJournal, Memcached is a distributed memory caching system that is now used on many popular sites. Memcached provides performance boosts to database-driven sites by caching data into RAM to reduce reads to external data.


Who uses Memcache?

  • YouTube
  • Reddit
  • Zynga
  • Facebook
  • Orange
  • Twitter
  • Wikipedia
  • Heroku offers a managed Memcached service built on Couchbase Server as part of their platform as a service.
  • Google App Engine, AppScale, Windows Azure and Amazon Web Services also offer a Memcached service through an API

Used for?


1) Reduces database load.
2) Perfect for websites with high database load.
3) Significantly reduces the number of retrieval requests to database.
4) Cuts down the I/O access (hard disk)


1) Not a persistent data store
2) Not a database
3) Not application specific
4) Cannot cache large object

Configuring Memcached (For Ubuntu)

Step 1) Install Libevent
Memcached uses the Libevent library for network IO.
cd libevent-1.4.11-stable$ autoconf

./configure --prefix=/usr/local


sudo make install
Step 2) Install Memcached
Download the latest version of Memcached from Danga.com, which developed Memcached originally for LiveJournal.
cd memcached-1.4.0


./configure --prefix=/usr/local


sudo make install
Step 3) Run Memcached

Start Memcached as a daemon with 512 MB of memory on port 11211 (default).
Then you can telnet to the server, port and use any of the available commands.
memcached -d -m 512 -p 11211

telnet localhost 11211Trying ::1...
Connected to localhost.
Escape character is '^]'.
get joe

set joe 0 3600 10  (Note: TTL 3600 and 10 bytes)California

get joe
VALUE joe 0 10
Step 4) Spy Memcached (Memcached Java Client)

 public class Users implements Serializable{
  private int id;
private String name ;
  // Getter and Setter methods

public class MemcachedDemo {
    public static void main(String[] args) {
        Users users = new Users();
        try {
            MemcachedClient c=new MemcachedClient(new InetSocketAddress("", 1121));
            c.set("someKey_1", 2592000, users);
            Object myObject=c.get("someKey_1");
            System.out.println("Object 1: " + myObject);
            System.out.println("Statistics: " + c.getStats());
            System.out.println("Statistics of individual Items: " + c.getStats("items"));
            c=null ;
        } catch (IOException e) {

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}