How to use Redis on OpenShift from your Ruby application

26 Ago , 2014  

What is Redis

Redis is an in-memory database. Persistence is optional and has two forms – RDB and AOF (Append Only File). RDB provides an instant snapshot of the current data in the memory. AOF, in comparison, saves every operation in a log that may be later replayed and thus the memory image may be recreated.

Redis provides many different data structures in a very efficient way – e.g. strings, key-value maps, lists, sets, bitmaps. For a better overview, see the documentation. Because it provides so many data structures, Redis may be used for many use cases and probably the most common one is a replacement for Memcache as a cache solution.

Getting Redis

There are several ways to get Redis on OpenShift. There is a community cartridge, there is a partner provider in the marketplace, or you can use an external provider. We won’t talk about the last one today, but you can take a look at this article for more information. Even though it’s written with Node.js in mind, you can still get the basic idea of how to integrate with a third party.

Community cartridge

Clayton Coleman implemented a scalable Redis cartridge for OpenShift. With this cartridge, you have your Redis instances directly on OpenShift in separate gears. To get more information on this solution, take a look at this blog post.

Partner provider

OpenShift also provides integration with partners. Using the Marketplace, you can simply deploy services through partners and have them integrated automatically with OpenShift. That’s what we will demonstrate in this blog post.

Creating the application

First, let’s create a Ruby application on OpenShift. Then we will integrate the app with the Redis provider.

You can do this through the web interface or just simply use the command line tool:

{might want to change the name of the app to – redisapp – to help avoid confusion}

rhc app create redis ruby-2.0

Now we have a ruby application we can work with. Next, open the application detail in the OpenShift web interface. It should looks like this:

Ruby application instance

At the bottom most line, you can see a link to the Marketplace … click it. In the top right corner is a search box, enter “redis” and let it search. You will get two services by “Redis labs”.

Redis services in marketplace

We will choose “Redis Cloud”. Now that you see the overview of the service, open the “Editions and Pricing” tab

Redis cloud editions and pricing tab

Click the “25MB grey square”. You will be transferred to the screen where you add the plan to the purchase.

Add the plan to purchase

Then scroll down and click Continue.

Confirm the purchase

You will confirm the purchase.

Finish the process

And purchase will be finished.

Now there is a Redis instance running in the Redis Cloud. The next step is to integrate it with your OpenShift application.

Integrating with OpenShift

Go to the “Purchased products” section … or click the “Go to applications” button on the last screen.

Redis in the list

There you follow the “Add to application” button to the screen

Add Redis to application

From the list of your OpenShift applications, choose the one you need Redis for and click “Add Redis Cloud.”

Add Redis to OpenShift application

The item will change and will allow you to potentially remove Redis from the application. The application now has all of the information required for connecting to Redis in an environment variable.

Getting the connection parameters

To get a quick view of the environment variable, you can SSH into the gear and list it.

rhc ssh redis
env | grep redis

Mine looks like this:

Redis connection in ENV variables

or as a code


You read the environment variable from your application like this:

redis_config = ENV['rediscloud_4e70d']

Then you need to parse the JSON string. I would recommend you to use MultiJSON as that allows you to switch JSON backends more easily.

config = MultiJson.load(redis_config)

Finally you can connect to Redis

redis = => "redis://:#{config['password']}@#{config['hostname']}:#{config['port']}/")

Now you are all set to use Redis.


Using Redis from OpenShift is easy and there are multiple ways to do that. You can use a partner from the Marketplace, use an external provider, or deploy a community cartridge providing the Redis database.

Next Steps

, , , , , , ,

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *