Redis Ruby

Make Redis Ruby super easy with Redis Labs

Make Redis Ruby super easy with Redise

Redis Labs’ products and services enable running Redis datasets in a highly available and auto-scalable manner, with predictable top performance.

The Redise Pack lets you install an enterprise grade Redis cluster in your environment of choice, whether an on-premises data-center or your preferred cloud platform. It gives you full control of your data and configuration – no clustering or sharding knowledge required!

Redise Cloud is a fully-managed cloud service for hosting and running Redis dataset without dealing with nodes, clusters, failure recovery or performance stabilization. Our technology does all that in a fully automated manner. Redise Cloud is available on all popular clouds and platforms-as-a-service.

Using Redis with Ruby

In order to use Redis with Ruby you will need a Ruby Redis client. In the following sections, we will demonstrate the use of redis-rb, a Ruby client library for Redis. Additional Ruby clients for Redis can be found under the Ruby section of the Redis Clients page.

Installing redis-rb

redis-rb’s installation instructions are given in the README file. Use gem to install redis-rb:

$ gem install redis

Or, include redis-rb in your Gemfile by adding to it the following line:

gem 'redis'

Followed by executing bundle install.

Opening a Connection to Redis Using redis-rb

The following code creates a connection to Redis using redis-rb:

require 'redis'

redis = (
    :host => 'hostname',
    :port => port,
    :password => 'password')

To adapt this example to your code, make sure that you replace the following values with those of your database:

  • In line 4, the :host should be your database’s hostname or IP address
  • In line 5, the :port should be your database’s port
  • In line 6, the :password should be your database’s password

Using SSL and redis-rb

redis-rb does not support SSL connections natively. For an added security measure, you can secure the connection using stunnel or this redis-rb fork that has been added with SSL support.

Reading and Writing Data with redis-rb

Once connected to Redis, you can start reading and writing data. The following code snippet writes the value bar to the Redis key foo, reads it back, and prints it:

# open a connection to Redis

redis.set('foo', 'bar');
value = redis.get('foo');
puts value

The output of the above code should be:

$ ruby example_redis-rb.rb


For more information on using Redis Labs’ products and services with Ruby please see the Howto page.