DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • All You Need To Know About Garbage Collection in Java
  • Java Memory Management
  • Singleton: 6 Ways To Write and Use in Java Programming
  • Exploring Exciting New Features in Java 17 With Examples

Trending

  • How to Practice TDD With Kotlin
  • Memory Leak Due to Time-Taking finalize() Method
  • Docker Base Images Demystified: A Practical Guide
  • Docker Model Runner: Streamlining AI Deployment for Developers
  1. DZone
  2. Data Engineering
  3. Data
  4. 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!

By 
Siva Prasad Rao Janapati user avatar
Siva Prasad Rao Janapati
·
Mar. 30, 16 · Analysis
Likes (4)
Comment
Save
Tweet
Share
21.4K 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.

    Related

    • All You Need To Know About Garbage Collection in Java
    • Java Memory Management
    • Singleton: 6 Ways To Write and Use in Java Programming
    • Exploring Exciting New Features in Java 17 With Examples

    Partner Resources

    ×

    Comments

    The likes didn't load as expected. Please refresh the page and try again.

    ABOUT US

    • About DZone
    • Support and feedback
    • Community research
    • Sitemap

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

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

    LEGAL

    • Terms of Service
    • Privacy Policy

    CONTACT US

    • 3343 Perimeter Hill Drive
    • Suite 100
    • Nashville, TN 37211
    • support@dzone.com

    Let's be friends: