Performance Zone is brought to you in partnership with:

I am a JAVA software engineer and Cloud Technology Savvy. I have been experience in enterprise solutions using Java(EE), Apache Solr, GWT, GWT-P, SmartGWT, Restlet and Hibernate. Moreover my interest are in system architecture and integration, data modeling, relational databases and mapping with NoSQL for high throughput. Sunil has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Memcached: In-memory Caching System

09.03.2013
| 10414 views |
  • submit to reddit
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.

Architecture


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?


Pros

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)

Cons

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

make

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

autoconf

./configure --prefix=/usr/local

make

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 127.0.0.1 -p 11211

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

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

get joe
VALUE joe 0 10
California
END
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();
        users.setId(1);
        users.setName("Sunil123");
        try {
            MemcachedClient c=new MemcachedClient(new InetSocketAddress("127.0.0.1", 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.delete("someKey_2");
            c.shutdown();
            c=null ;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



Published at DZone with permission of its author, Sunil Gulabani. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)