Memcached-a distributed memory object caching system

memcached

            In this article, we will learn about memcache. We will learn the memcache with question and answer approach.

What is “Memcached”?

                 Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Memcached uses LRU caching algorithm(Least Recently Used (LRU) – discards the least recently used items first) .

Why “Memcached”?

  • Free & Open source

  • High performance

  • Simple to set up

  • Ease of development

  • APIs are available for most popular languages

Who are all using “Memcached”?

  • ProliphiqViews ipad application

  • Youtube.com

  • Digg.com

  • Twitter.com

  • Wikipedia.com

  • Flickr

  • Craiglist.com

  • ….. So many users

What are the Use cases to use “Memcached”?

  • To achieve high performance

  • To achieve the scalability

The above question and answers will give us some insights about the memcached. Now, we will see “how the traditional systems will use the cache” from the below diagram.

Traditional_Arch

                             In the above architecture, the cache is part of the application server. In this case, some of the memory is allocated for cache from the heap size allotted to the JVM. If the cache size increases, then the heap size need to increase. There is no better control over the cache, because, cache is bundled with app server. To have better control, we need to separate the cache from the app server. The below architecture depicts the same.

Cache_System

In the above architecture , we have separated the cache from the application. So, either we can maintain the cache in the same server where the application is deployed or we can assign separate machine for the cache. If the cache size increases, we can increase the RAM size with out effecting the application. So, we have better control on the cache.

How to install and set up “Memcached”?

In this article, I have used Ubuntu operating system to install and set up the memcacahed. Follow the below steps to install and run the memcahed.

  1. Download the source code from https://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
  2. Extract the downloaded file and move to the extracted folder.
  3. Run the below commands
    1. ./configure
    2. Make & make install
  4. After the successful installation start the memcached using memcached -u <user name>
  5. The memcache server will run by default on 11211 port
  6. Open new command prompt and try to connect to the memcached through telnet . For example  telnet <host> 11211. If you are able to connect successfully to the memcached server then the  installation and the setup is done.

What operations we can perform with “Memcached”?

After establishing the connection to the memcached through telnet, we can perform the below operations.

  • Set(Store key/value pair in Memcached)

           Format:

  • set <key> <flags> <exptime> <bytes>

           Parameters:

  • <key> : the key of the data stored
  • <flags> : 32-bit unsigned integer that the server store with the data (provided by the user), and return along the data when the item is retrieved
  • <exptime> : expiration time in seconds, 0 mean no delay, if exptime is superior to 30 day, Memcached will use it as a UNIX time stamps for expiration
  • <bytes> : number of bytes in the data block

         These commands can return :

  • STORED to indicate success
  • NOT_STORED indicate that the data was not stored because condition for “add” or “replace” command wasn’t met, or the item is in a delete queue
  • Get(Get value from key)

          Format:

  • get <key>

          Parameters:

  • <key> : the key of the data stored to retrieve
  • <key>* mean you can request value of multiple keys separated by white space

         These commands return :

  • VALUE <flag> <bytes>\r\n<data>
  • END indicate the end of response
  • Add(Store key/value pair in Memcached, but only if the server doesn’t already hold data for this key)
  •           Format:

    • add <key> <flags> <exptime> <bytes>

             Parameters:

    • <key> : the key of the data stored
    • <flags> : 32-bit unsigned integer that the server
    • store with the data (provided by the user), and return along the data when the item is retrieved
    • <exptime> : expiration time in seconds, 0 mean no delay, if exptime is superior to 30 day, Memcached will use it as a UNIX time stamps for expiration
    • <bytes> : number of bytes in the data block

            These commands can return :

    • STORED to indicate success
    • NOT_STORED indicate that the data was not stored because condition for “add” or “replace” command wasn’t met, or the item is in a delete queue
    • Replace(Store key/value pair in Memcached, but only if the server already hold data for this key)

            Format:

    • replace <key> <flags> <exptime>
      <bytes>
  • Delete(Deletes key/value pair in Memcached)
  •        Format:

    • delete <key>
  • Flush_all(Flush the server key/value pair (invalidating them) )
  •        Format:

    • flush_all
  • stats(Return general-purpose statistics like up time, version, memory occupation, …)
  •        Format:

    • stats
  • Item stats(Return items statistics, will display items statistics (count, age, eviction, …))
  •        Format:

    • stats items

    Do we have any client API’s?

    We have client API’s for all the major languages. You can find the list of client API’s here.

    How to use JAVA client API?

    Follow the below steps to use the JAVA client API.

    • To contact Memcached from Java, download the java client from http://code.google.com/p/spymemcached/
    • Get Memcached client
      • MemcachedClient c=new MemcachedClient(new InetSocketAddress(“localhost”, 11211));
    • After the completion of operations, call shutdown() on memcached client.

    Now, you can try out “memcached”  as part of your applications.

    Advertisements

    I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

    Tagged with:
    Posted in memcached, Performance
    9 comments on “Memcached-a distributed memory object caching system
    1. […] Redis看起来和Memcached很相像,但它们之间还是有些不同。 […]

    2. Yesuraj G says:

      Very good article. Thanks for your effort. Keep posting new articles.

    3. Anonymous says:

      I couldn’t refrain from commenting. Exceptionally well written!

    4. nicholson says:

      The technology usage with use cases are good. Are there any other prominent cache frameworks available?

    5. venetta_gamboa says:

      I am using spring framework for our applications. How can we use memcached with spring framework. Any references?
      Thanks.

    6. Anonymous says:

      Nice explanation!!!
      Thanks!

    7. Anonymous says:

      The article is good, thats why i have read it
      completely

    8. Thiyagarajan Muthusamy says:

      You made it looks very simple. Thanks for you effort!

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    DZone

    DZone MVB

    Java Code Geeks
    Java Code Geeks
    %d bloggers like this: