What is Memcached?

Memcached logo

Memcached is a free and open-source high-performance memory caching system. It’s typically used to cache database data, API calls or page rendering chunks in RAM to increase the application performance.

It can store data as little as a number, or as big as a finished HTML page.

The system is designed to be accessed through TCP so it can work in a separate server, and can also be distributed among several servers, summing up a big hash table to store data.

When running out of space, the older data is overwritten. So it must be treated as a transitory (not-persisted) cache, meaning that your data may not still be there.

Installation

If you’re using macOS, it’s very easy to install Memcached using Homebrew:

Then you can start it using this command:

If you are using Linux, you can use your distribution’s package manager to install it easily.

Connecting

The default port is 11211. Let’s try to connect using Telnet:

Great! The server is up. Now quit Telnet (using the quit command) and let’s install a Ruby gem to interact with the server more comfortably:

Run a Ruby console (irb) and connect to the server using the default port:

Basic storage

Let’s start playing with Memcached storing a string and then getting it:

As you see, set is used to set a value to a key, which will be overwritten if it already exists. get is used to get the key value.

Data can also be limited by some time in seconds:

Multiple values can be get in a unique query using get_multi, resulting in a hash:

Let’s free some memory by deleting those greetings:

If you want to delete all keys, the flush operation is what you need:

Adding data

The set command overwrites the value if the key already exists:

To avoid this, the add command can be used instead, which will create the value if the key doesn’t exist:

Replacing data

replace is used to replace data for an existing key:

But it will do nothing if the key doesn’t exist:

Appending and prepending data

In order to append or prepend data, Dalli requires it to be previously set as raw:

Now we can append:

And prepend:

Increment and decrement data

Another useful case is to have a counter, so it can be incremented or decremented.

Check and set

Imagine having a value that can be updated by any concurrent client (trying to update it at the same time). Wouldn’t that be a problem? That’s a race condition that can be controlled using the cas operation, that checks if the value has been changed by other client before updating it.

It works like a set operation, but it requires a block:

Therefore, this is an atomic operation.

Conclusion

We have seen what’s Memcached and how it can be used directly using the Dalli gem. It may seem simple, but it can help a big web application to work faster like many important companies are doing, such as YouTube, Reddit, Facebook, Twitter, and Wikipedia.

Sometimes it’s essential to work with Memcached directly, for instance when developing a service with special needs. There are other times where the framework (Ruby on Rails for example) can store its cache out of the box in Memcached using a gem like Dalli.

Did you like it? Please share it:

Get my ebook for free

10 ideas that helped me become a better developer (and may help you too)

Subscribe to my mailing list and get my ebook on 10 ideas that helped me become a better developer.

About Me

David Morales

David Morales

I'm David Morales, a computer engineer from Barcelona, working on projects using Ruby on Rails and training on web technologies.

Learn More