[ Content | View menu ]

Redis and Ruby

Mark Mzyk | June 2, 2009

At the May raleigh.rb meetup, Kevin Smith gave a talk on alternative databases, in which he covered CouchDB and Redis.

The talk convinced me that Redis would be the perfect database to underpin Get Encouraged.  Redis is a key-value pair database that is blazing fast: perfect for a small Twitter app that really only needs to store tweets.

Being rather new to the Ruby world, there are obstacles that are easy for others, but confound me, since I haven’t encountered them before.  One of these obstacles is that I don’t know the gem system very well, and there isn’t a gem for Redis that you can grab using gem install that I could find.  This meant I had to figure out how to install a Redis Ruby client library gem using code from github.  Since I figure there are others who are as baffled by this as I was, here are the steps you need to take to install Redis and the redis gem on your system, using the redis-rb library.

The first step is to download the redis-rb client library from github.  It is here:


There are other Redis Ruby client libraries, but my directions pertain to redis-rb, since that is the one I used.  Why start with a Redis client library and not Redis itself?  Because the redis-rb library provides a rake task that will install Redis for you, although you can still install Redis without using the rake task if you so choose.

Once you’ve downloaded redis-rb, untar or unzip it, whatever the case may be, and cd into that directory.

Install the rspec gem, if you don’t already have it:

sudo gem install rspec

Now run the rake command to install Redis:

rake redis:install

This will install Redis to /usr/bin/.  The Redis configuration file, redis.conf will be in /etc/.  Redis can be started by running /usr/bin/redis-server.  You do not need to setup a configuration file, since Redis will just use its default one; however, there are options that are worth exploring in the conf file, so at some point open up /etc/redis.conf and take a look.

Next, redis-rb requires that you have dtach installed, if you don’t already:

rake dtach:install

This installs dtach to /usr/bin/.  You don’t have to do anything with dtach, it just needs to be present on your system so redis-rb can find it.

The final step is to make the redis-rb gem, so you can use it in your code.  It’s easy enough:

rake gem

That command will package up the redis-rb gem and put it in the folder pkg under the current directory.  If you cd into the pgk directory, you can then install the gem:

sudo gem install redis

Note: while the github repo is called redis-rb, the gem it creates is called redis.

You now have Redis installed on your system, along with the redis gem to enable your Ruby code to access Redis.  Here’s some trivial example code to help get you up and running (this example is based off of one in the redis-rb github repo, where you can find more examples):

require 'rubygems'
require 'redis'
r = Redis.new
r.delete('first_key') #clear it out, if it happens to be set
puts 'Set the key {first_key} to {hello world}'
r['first_key'] = 'hello world'
puts 'The value of {first_key} is:'
puts r['first_key']

Save this code to a file, then start Redis on your machine (/usr/bin/redis-server), run the code, and watch it do its magic.  Hope that helps you get started with Redis.  Enjoy.

Update 6/3/2009: Kevin has now posted his slides from his raleigh.rb presentation.  Find them here.