Memcached - Quick Guide

Memcached - Overview

Memcached is an open source, high-performance, distributed memory caching system intended to speed up dynamic web applications by reducing the database load. It is a key-value dictionary of strings, objects, etc., stored in the memory, resulting from database calls, API calls, or page rendering.

Memcached was developed by Brad Fitzpatrick for LiveJournal in 2003. However, it is now being used by Netlog, Facebook, Flickr, Wikipedia, Twitter, and YouTube among others.

The key features of Memcached are as follows −

It is open source.

Memcached server is a big hash table.

It significantly reduces the database load

It is perfectly efficient for websites with high database load.

It is distributed under Berkeley Software Distribution (BSD) license.

It is a client-server application over TCP or UDP.

Memcached is not −

a persistent data store

a database

application-specific

a large object cache

fault-tolerant or highly available

Memcached - Environment

Installing Memcached on Ubuntu

To install Memcached on Ubuntu, go to terminal and type the following commands −

$sudo apt-get update
$sudo apt-get install memcached

Confirming Memcached Installation

To confirm if Memcached is installed or not, you need to run the command given below. This command shows that Memcached is running on the default port 11211.

$ps aux | grep memcached

To run Memcached server on a different port, execute the command given below. This command starts the server on the TCP port 11111 and listens on the UDP port 11111 as a daemon process.

$memcached -p 11111 -U 11111 -u user -d

You can run multiple instances of Memcached server through a single installation.

Memcached Java Environment Setup

To use Memcached in your Java program, you need to download spymemcached-2.10.3.jar and setup this jar into the classpath.

Memcached - Connection

To connect to a Memcached server, you need to use the telnet command on HOST and PORT names.

Syntax

The basic syntax of Memcached telnet command is as shown below −

$telnet HOST PORT

Here, HOST and PORT are machine IP and port number respectively, on which the Memcached server is executing.

Example

The following example shows how to connect to a Memcached server and execute a simple set and get command. Assume that the Memcached server is running on host 127.0.0.1 and port 11211.

$telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
// now store some data and get it from memcached server
set tutorialspoint 0 900 9
memcached
STORED
get tutorialspoint
VALUE tutorialspoint 0 9
memcached
END

Connection from Java Application

To connect the Memcached server from your java program, you need to add the Memcached jar into your classpath as shown in the previous chapter. Assume that the Memcached server is running on host 127.0.0.1 and port 11211. −

Output

On compiling and executing the program, you get to see the following output −

Connection to server successful
set status:true
Get from Cache:memcached
Prepend to cache:true
Get from Cache:redismemcached

Memcached - CAS Command

CAS stands for Check-And-Set or Compare-And-Swap. Memcached CAS command is used to set the data if it is not updated since last fetch. If the key does not exist in Memcached, then it returns NOT_FOUND.

Syntax

The basic syntax of Memcached CAS command is as shown below −

set key flags exptime bytes unique_cas_key [noreply]
value

The keywords in the syntax are as described below−

key − It is the name of the key by which data is stored and retrieved from Memcached.

flags − It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved.

exptime − It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as a UNIX timestamp for expiration.

bytes − It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached.

unique_cas_key − It is the unique key get from gets command.

noreply (optional) − It is a parameter that informs the server not to send any
reply.

value − It is the data that needs to be stored. Data needs to be passed on new line after executing the command with the above options.

Output

The output of the command is as shown below −

STORED

STORED indicates success.

ERROR indicates error while saving data or wrong syntax.

EXISTS indicates that someone has modified the CAS data since last fetch.

NOT_FOUND indicates that the key does not exist in the Memcached server.

Example

To execute a CAS command in Memcached, you need to get a CAS token from the Memcached gets command.

Output

On compiling and executing the program, you get to see the following output −

Connection to server successful
set status:true
Get from Cache:memcached
Delete from Cache:true
Get from Cache:null

Memcached - Increment Decrement Data

Memcached incr and decr commands are used to increment or decrement the numeric value of an existing key. If the key is not found, then it returns NOT_FOUND. If the key is not numeric, then it returns CLIENT_ERROR cannot increment or decrement non-numeric value. Otherwise, ERROR is returned.

Syntax - incr

The basic syntax of Memcached incr command is as shown below −

incr key increment_value

Example

In this example, we use visitors as key and set 10 initially into it, thereafter we increment the visitors by 5.

Memcached - Stats Sizes

Memcached stats sizes command provides information about the sizes and number of items of each size within the cache. The information is returned in two columns. The first column is the size of the item (rounded up to the nearest 32 byte boundary), and the second column is the count of the number of items of that size within the cache.

Syntax

The basic syntax of Memcached stats sizes command is as shown below −

stats sizes

Example

stats sizes
STAT 96 1
END

The item size statistics are useful only to determine the sizes of the objects you are storing. Since the actual memory allocation is relevant only in terms of the chunk size and page size, the information is only useful during a careful debugging or diagnostic session.

Memcached - Clear Data

Memcached flush_all command is used to delete all data (key-value pairs) from the Memcached server. It accepts an optional parameter called time that sets a time after which the Memcached data is to be cleared.

Syntax

The basic syntax of Memcached flush_all command is as shown below −

flush_all [time] [noreply]

The above command always returns OK.

Example

In the following example, we store some data into the Memcached server and then clear all the data.