DZone
Performance Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > Memcached - A Distributed Memory Object Caching System

Memcached - A Distributed Memory Object Caching System

Here's an overview of memcached: what it is, why it's valuable, and more!

Siva Prasad Rao Janapati user avatar by
Siva Prasad Rao Janapati
·
Mar. 30, 16 · Performance Zone · Analysis
Like (4)
Save
Tweet
18.44K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will learn about Memcached. Let us see what it is? How can we use it?

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 HTML . Memcached uses LRU caching algorithm(Least Recently Used (LRU) – discards the least recently used items first).

Why “Memcached”?

  • It's free and source

  • To achieve high performance

  • It's Simple to set up

  • Ease of development

  • Client APIs are available for most of the popular languages

Who is Using “Memcached”?

  • Youtube.com

  • Digg.com
  • Twitter.com
  • Wikipedia.com 
  • Flickr

  • Craiglist.com

  • and the list goes on ...........

Why Do We Need to Use “Memcached”?

  • To achieve high performance

  • To achieve high scalability

The architecture of the traditional system is given below.

Traditional Web Architecture

In the above design, the cache is part of the application server's JVM. In this case, some of the memory is allocated for the 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 tightly associated with the app server's JVM. To have better control, we need to separate the cache from the app server's JVM. 

High Scalability Architecture

In the above design, we have separated the cache from the application's JVM. This gives better control to manage the cache in a separate server. Based on the data size to cache, we can increase the cache server capacity without affecting the application. 

How to Install and Set Up “Memcached”?

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.org/files/memcached-1.4.25.tar.gz
  2. Extract the downloaded file and get into the extracted folder.
  3. Run the below commands
    • ./configure
    • Make & make install
  4. After successful installation, start the memcached using memcached -u <user name>
  5. The Memcached server will run by default on 11211 port
  6. Now, 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  you are done with the installation and the setup.

What Operations Can We Perform With “Memcached”?

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

Operation

Format

Parameters

Return Value

Set (Store key/value pair in Memcached)set <key> <flags> <exptime> <bytes>
  1. <key> : the key of the data stored
  2. <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
  3. <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
  4. <bytes> : number of bytes in the data block
  1. STORED to indicate success
  2. 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 for key)


get <key>


<key> : the key of the data stored. To retrieve client can request value of multiple keys separated by white space

  • 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)

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


  1. <key> : the key of the data stored
  2. <flags> : 32-bit unsigned integer that the server
  3. store with the data (provided by the user), and return along the data when the item is retrieved
  4. <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
  5. <bytes> : number of bytes in the data block
  • 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)replace <key> <flags> <exptime><bytes>

Delete (Deletes key/value pair in Memcached)


delete <key>


Flush_all (Flush the server key/value pair (invalidating them) )


flush_all


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


stats

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

stats items


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 client = new MemcachedClient(new InetSocketAddress(“localhost”, 11211));
  • After the completion of operations, call shutdown() on memcached client.
  • Memcached Cache (computing) operating system Data (computing) Memory (storage engine) Object (computer science) Java (programming language)

    Opinions expressed by DZone contributors are their own.

    Popular on DZone

    • Best Jira Add-ons for the Cloud
    • GraphQL in Enterprise: What It Takes to Build, Deploy, and Monitor a New Enterprise GraphQL Service
    • SRE: From Theory to Practice: What's Difficult About Incident Command?
    • Auth0 (Okta) vs. Cognito

    Comments

    Performance Partner Resources

    X

    ABOUT US

    • About DZone
    • Send feedback
    • Careers
    • Sitemap

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

    • Article Submission Guidelines
    • MVB Program
    • Become a Contributor
    • Visit the Writers' Zone

    LEGAL

    • Terms of Service
    • Privacy Policy

    CONTACT US

    • 600 Park Offices Drive
    • Suite 300
    • Durham, NC 27709
    • support@dzone.com
    • +1 (919) 678-0300

    Let's be friends:

    DZone.com is powered by 

    AnswerHub logo