Book Image

Getting Started with Memcached

By : Ahmed Soliman
Book Image

Getting Started with Memcached

By: Ahmed Soliman

Overview of this book

<p>Web application performance is no longer a non-functional requirement, but an implicit condition for an engaging user experience. As a result, responsive and highly scalable applications are becoming a necessity. Memcached is a high-performance distributed memory caching system built to speed up dynamic web applications by offloading pressure from your database. <br /><br />Getting Started with Memcached is a hands-on, comprehensive guide to the Memcached service and it’s API in different programming languages. It contains practical recipes to integrate Memcached within your Rails, Django, or even Scala Play! applications.<br /><br />This book will show you everything you need to know to start using Memcached in your existing or new web applications.<br />This book uses real-world recipes to help you learn how to store and retrieve data from your clustered virtual memory cache pool and how to integrate caching into your favourite web development framework.</p> <p><br />You will also learn how to build a Memcached consistent-hashing scalable cluster and how Memcached clients are properly configured to use different servers to scale out your memory cache pool in Ruby, Python, PHP, and Java. With this book, you will see how to cache templates and database queries in the most popular web development framework in use today.</p>
Table of Contents (9 chapters)

Talking with memcached (Advanced)


Now, since we have memcached installed, let's see what kind of commands memcached daemon supports and how simple the memcached protocol is.

We will be using a plain simple TELNET tool to connect to the memcached daemon.

Remember that memcached has no persistent storage whatsoever, so it's totally memory-based and once we terminate the daemon everything we have stored is simply gone!

Getting ready

You will need to have telnet client installed on your machine, in most cases you will find it already installed but in case you didn't find it you can install it on your Ubuntu box using

sudo apt-get install telnet

You also must make sure that the memcached daemon is actually running.

Connect to the running memcached daemon with telnet on port 11211.

telnet localhost 11211

You should see something like the following:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

How to do it...

So, let's play with some basic storage commands to understand the main concepts behind memcached. We believe that this way is the best way to understand the features of the service and to truly realize its design simplicity and power.

Memcached supports a plain ASCII (text) protocol, you can find the protocol definition and specifications in the document in this link https://github.com/memcached/memcached/blob/master/doc/protocol.txt

  1. The first command is stats where you request some basic information about the running service:

    stats
    STAT pid 8141
    STAT uptime 1926
    STAT time 1380294691
    STAT version 1.4.13
    STAT libevent 2.0.16-stable
    STAT pointer_size 64
    STAT rusage_user 0.108006
    
    
  2. So, let's now use it for settings:

    stats settings
    STAT maxbytes 67108864
    STAT maxconns 1024
    STAT tcpport 11211
    STAT udpport 11211
    STAT inter 127.0.0.1
    STAT verbosity 0
    STAT oldest 849
    STAT evictions on
    STAT domain_socket NULL
    STAT umask 700
    STAT growth_factor 1.25
    STAT chunk_size 48
    STAT num_threads 4
    STAT num_threads_per_udp 4
    STAT stat_key_prefix :
    STAT detail_enabled no
    STAT reqs_per_event 20
    STAT cas_enabled yes
    STAT tcp_backlog 1024
    STAT binding_protocol auto-negotiate
    STAT auth_enabled_sasl no
    STAT item_size_max 1048576
    STAT maxconns_fast no
    STAT hashpower_init 0
    STAT slab_reassign no
    STAT slab_automove no
    END
    
  3. Now, let's store some value for a given key:

    set mykey 0 300 5 16
    I Love Memcached
    
  4. After you hit the return key, you will see the STORED message. So the whole listing is as follows:

    set mykey 0 300 16
    I Love Memcached
    STORED
    
  5. Now, let's read this key by using the get command:

    get mykey
    VALUE mykey 0 16
    I Love Memcached
    END
    

How it works...

This recipe gives you a glimpse of the kind of commands you can send to your memcached daemon using a simple tool like telnet.

We started by connecting to the memcached daemon on the default port 11211 using telenet. Then we used the stats command which asks the daemon to send us some useful statistics from the service such as the uptime, how many get requests actually returned data get_hits, and how many get requests resulted in a miss hit get_misses.

Then, we used stats settings which prints out the settings and configuration of the current running daemon, you will be able to see things such as tcpport which points to the port it is listening to and something such as maxbytes which is the maximum number of bytes allowed in this cache server.

Then, we moved to the storage commands set and get. Storage commands have the following format:

<command name> <key> <flags> <exptime> <bytes>

The <command name> field can be set, add, replace, append, or prepend.

The <key> field is the name of the key you are storing, in our case that was mykey.

The <flags> field is an arbitrary 16-bit unsigned number that the server stores along with the key and is returned when the client requests to get the value. It's opaque to the server, so it doesn't give any special meaning to the server itself but the client can use this number to add some special meaning to this key if needed. In our case, we just passed 0 for this field.

The <exptime> field indicates the expiration time, if it's 0, the item never expires (although it might get deleted when the server needs to free up place for another key to be stored). If it's non-zero (either Unix time or offset in seconds from the current time), it is guaranteed that clients will not be able to retrieve this item after the expiration time arrives (measured by the server time).

The <bytes> field indicates the length of the value to be stored, in our case that was 16, which is the length of the words I Love Memcached.

After hitting your return (Enter) key, you are supposed to feed the server with the value to be stored along with the key. Then, after hitting another return, you receive a STORED message indicating that the key-value pair has been stored.

Then, we moved to get, it's very simple, you get a <key> field and the value returns along with the <flags> field and the length of the value, then the value is printed before the END sentinel.

VALUE mykey 0 16
I Love Memcached
END