Introducing Redis Store

Redis Store aims to be a toolkit for your Ruby applications, it natively supports sharding, master/slave replication, marshalling, timeouts and namespaces. Plus, it’s really easy to use with the most popular frameworks, such as Ruby on Rails.

If you love modularity, you will love Redis Store too: under the hood it just activates, at runtime, the best set of low level features requested by the above software layers. It’s delivered as a set of gems, one for each targeted framework, with a common background that’s the redis-store gem itself. This decision helped me a lot to deal with different versions of Ruby, several frameworks, and versions.

How to use it

Let’s say we want to use Redis Store in our Rails project. First of all we need to add an entry in our Gemfile:

Then we have many options:

As you can see, it’s pretty easy to plug in, but let’s investigate how to manage our configuration. The first case is self-explanatory, we’re telling ActiveSupport to load our Redis backed store. Remember that, au contraire of Redis, which only supports strings, we can dump full objects here.

The second example is a little bit more complicated. First of all, we are employing a cluster of servers. As mentioned before, Redis Store supports sharding, that means the HTTP sessions will be transparently split across the two hosts. For each node we are specififying, respectively: protocol, password, ip, port and the Redis database.

The last one, instead describes the setup for the Rails recently added Rack::Cache. This is a Ruby implementation of an HTTP cache proxy (like Squid or Varnish), which helps to drastically improve response times, by storing the full contents for a given url. It has two components: the metastore, used mainly to check the existence of an entry, and the entitystore, that’s the repository itself. You’ve probably noticed another parameter in the configuration: it’s the namespace, all the keys will be prefixed with something like <namespace>:<key> . One last note: since we are storing just plain text, the mashalling module isn’t activated.

This is just a small portion of what Redis Store can do, it also supports Rack, Sinatra and I18n out of the box.

Conclusion

I strongly believe that Redis is must-have in your environment, it’s fast, flexible enough to be used as database, cache, pub/sub. That being said, Redis Store can be a great tool for scale your applications, but everything has a cost: fine tuning. Again, it-just-works, but as Rails itself, to step up, you’ll probably find out to experiment a little bit with the Redis configuration, in order to find the right threshold between performances, scalability and memory consumption.

If you want to give Redis store a try, please check it out on GitHub.

Sign up for a RedisToGo account and try this out in 5min.

A guest post by Luca Guidi he can be found on Twitter and Github.

Photos CC Commercial license from. DMPhotography, Generated and benarent

@benarent.

Posted by @benarent on June 17th, 2012 in Introducing
7 Comments

Comments

  1. Dean says:

    June 18th, 2012 at 9:19 am (#)

    What would be the advantages of using Redis Store over Memcached? Is it any faster?

  2. The Last 2 Weeks in Ruby: Rails 3.2.6, Savon 1.0, RailsInstaller for OS X, Pow 0.4.0, Rails 2 Asset Pipeline says:

    June 22nd, 2012 at 2:34 pm (#)

    [...] Redis Store: Redis Stores for Ruby Frameworks Redis Store provides a full set of stores (Cache, I18n, Session, HTTP Cache) for all the modern Ruby frameworks like: Ruby on Rails, Sinatra, Rack, Rack::Cache and I18n. It natively supports object marshalling, timeouts, single or multiple nodes and namespaces. [...]

  3. Sijo says:

    June 26th, 2012 at 8:19 am (#)

    Thanks for this. Can you compare redis with memcache OR can they be used side by side?

  4. @benarent says:

    June 28th, 2012 at 4:54 am (#)

    That’s a whole another blog post. What do you currently use a cache for?

  5. Viren says:

    June 28th, 2012 at 2:43 pm (#)

    Great

    So do does it achieve sharding have you done to incorporated there is this haskell code
    https://github.com/kni/redis-sharding-hs did you actually wrote a ruby version for it

    Yes or N

    Great Work Still truely Amazing

  6. @benarent says:

    July 2nd, 2012 at 12:33 am (#)

    Hi Viren, I’m not to sure. Best to ask Luca Guidi.

  7. fahim says:

    November 13th, 2013 at 7:10 am (#)

    How to check that session stored in redis and how redis_store remove session when user sign out ?

Leave a Comment