book a call

estimate project

The belighted Blog

Application performance improvement via in memory key-value store: MemCached and the Dalli Ruby Gem

by Stéphane A., on 20 March 2013

Some time ago, as we were facing a performance drop in one of our projects, the decision was made to cache some intensively used data pieces and thus improve the overall system read efficiency.

After some research, we found some interesting key-value stores (as MemCached, Redis,…) which could perfectly do the trick. Since one has to be chosen, we finally picked up Memcached.

MemCached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) coming from results of database calls, API calls, or page rendering.

The system uses a client–server architecture wherein servers maintain a key–value associative array and clients populate this array and query it. To do so clients use client-side libraries to contact servers which, by default, expose their service on port 11211.

Using MemCached in your applications will allow to layer your requests, resulting in faster responses coming from RAM when available and falling back on a slower store such as a database otherwise.

But pay attention, values stored in MemCached are only available until the server runs out of memory. At which point, oldest data is discarded, a mechanism known as LRU. Therefore, clients must treat Memcached as a transitory cache. They cannot assume that data stored in Memcached is still there when they need it.

And as quite everything is well thought and done in our ruby world, a MemCached client implementation exists through the ruby Dalli gem (Which is the officiall MemCached client since Rails 4).

Installation and usage

So, what do we need precisely to incorporate of all these features into our application?

First of all, an up and running MemCached server (The set up could differ from one OS to another):

  • for Mac OS (via HomeBrew):

    brew install memcached
  • for debian-based linux distributions:

    apt-get install memcached

Then you can simply install the Dalli gem (or put it into your gemfile):

gem install dalli

and use it into your ruby/rails project as shown in this piece of code:

require 'dalli' 
dc ='localhost:11211') 
dc.set('abc', 123) 
value = dc.get('abc')

Some tips

If you pass an array of strings, for instance: ["offices", "BE"], as key. Dalli will concatenate all those using backslashes as separator, resulting into "offices/BE" being really sent to the MemCached server.

You can even contact a MemCached server without ruby or rails using telnet.

If you’re in a rails project, you can even directly use MemCached as rails cache store via this additional configuration put into the desired environnement configuration file (rails 3 feature) :

config.cache_store = :dalli_store

We’ll certainly talk about some other no sql alternatives, their advantages, drawbacks and use cases in further blog posts, so stay tuned !


Topics:      Under the hood

What is SaaS?

Everything You Need to Know About Moving to a SaaS Model.

Get the guide now >

Subscribe to Updates