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 an 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, the cache is bundled with the app server. To have better control, we need to separate the cache from the app server. The architecture given below 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 without affecting the application. So, we have better control over the cache.

How to install and set up “Memcached”?

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

  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 a 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 with 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 timestamp 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 the condition for “add” or “replace” command wasn’t met, or the item is in a delete queue
  • Get(Get value from the key)

          Format:

  • get <key>

          Parameters:

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

         These commands return :

  • VALUE <flag> <bytes>\r\n<data>
  • END indicates the end of the 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 with the data when the item is retrieved
  • <exptime>: expiration time in seconds, 0 mean no delay, if exptime is superior to 30 days, 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 the 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 uptime, 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 APIs 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.

Advertisement

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dzone.com
DZone

DZone MVB

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