How to Get Easy Access to Hosted Redis with Redis Cloud - Archived

Redis is a new and incredibly popular, highly-scalable database solution. You may have noticed that a fully managed and supported Redis datastore is not available in the current list of cartridge services offered on OpenShift Online. However, the open nature of the OpenShift platform actually provides several ways for you to start using Redis in your applications today!

This post includes a few updates to the current Redis-Cloud QuickStart example app (contributed by Redis Labs), and provides a new quickstart example for javascript developers.

Introducing Redis Cloud from Redis Labs

Redis Cloud provides:

  • Access to professionally managed Redis dataservices that can automatically scale as your application grows
  • Support for automatic replication and failover
  • Basic access is free (25MB data)
  • And, you can pay to scale up

They have a great introduction video on their main website:

Setting up a Managed Redis DB

Before we get started, you’ll need to sign up for Redis Cloud, and configure your Redis DB instance:

  1. Sign up for a free account at Redis Cloud.
  2. Provision a Redis database, taking note of your DB connection details

Easy, Right?

Spin up an example application

Now that your Redis-Cloud datastore is available, let’s stand up a quick example application using the rhc command-line tool. You’ll need to provide your own REDISCLOUD_URL, REDISCLOUD_PORT, and REDISCLOUD_PASSWORD values in order to configure the application to connect to the datastore.

Learn about Redis DB interaction basics with this example ruby / Sinatra application:

rhc app create rediscloud ruby-1.9 \
  --from-code \

Or, try this javascript / nodejs / restify example:

rhc app create rediscloud nodejs-0.10 \
  --from-code git:// \

The above example commands use rediscloud as the sample application name. I’ll continue to use it as the application name in the remaining example commands.

If you failed to include your own Redis Cloud keys when creating your application, don’t worry – We have notes on how to reconfigure your application in the next section.

When the command completes you should have your own clone of the application source code in a folder named after your app (rediscloud), and a live copy of your application running at http://rediscloud-$

Using Environment Variables for Clean, Secure, and Reusable Source

These example applications provide a clean and reusable codebase that is designed to be free of application secrets and keys. They attempt to read their configuration details from environment variables when possible.

In our nodejs example application, the config/defaults.json file relies on environment variables when they are available, and provides sensible defaults for local development use cases as a fallback:

module.exports = {
  port: process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 3000,
  ip: process.env.OPENSHIFT_NODEJS_IP || '',
  redisCloud: {
    host: process.env.REDISCLOUD_URL,
    port: process.env.REDISCLOUD_PORT,
    password: process.env.REDISCLOUD_PASSWORD

If you created your example application via one of our web-based workflows, or were not able to correctly configure your application keys as a part if your app creation process – you’ll want to use rhc to set the following environment variables for your OpenShift app (substituting in your own values for the provided key names):


Then, run the following to restart your application (reloading it’s Redis DB configuration):

rhc app restart rediscloud

You can review your application’s full list configuration keys by typing:

rhc env list rediscloud

Local Development

You can run the same code in a local development environment by populating the required environment variables in your local development stage:


Then start your local server with:

npm start

Ruby developers can run:

ruby app.rb

You may need to run npm install or bundle install before starting your local server to in order to make sure that your application has access to all of it’s dependencies.

Building a Redis-powered API

Our main application code is clean and easy to read, since our configuration details have already been fully abstracted:

var config = require ('config'),
    restify     = require('restify'),
    redis = require("redis")
if(typeof( !== "undefined"){
  var client = redis.createClient(
      config.redisCloud.port,, {
        auth_pass: config.redisCloud.password
var app = restify.createServer()

The sinatra example app also has a nice configuration module to keep the main app code tidy.

Now that our application’s database connection has been successfully initialized, we can set up a few API endpoints to carry out basic DB interactions. See the node-redis library for additional Redis datastore interaction examples, and the restify docs for notes on crafting APIs.

The example application includes a /set endpoint that sets a welcome_msg key in your database:

app.get('/set', function (req, res, next){
  client.set('welcome_msg', "Hello World");

We can retrieve our welcome_msg value from the DB and return the stored value with our /get endpoint:

app.get('/get', function (req, res, next){
  client.get("welcome_msg", function (err, reply) {

An /info endpoint is defined for returning basic information about the status of our datastore:

app.get('/info', function (req, res, next){

And finally, we’ve included a mechanism for flushing the database, and clearing all data:

app.get('/flush', function (req, res, next){

Next, you can edit your local source to start experimenting with your new Redis-powered application.

A live copy of the Nodejs exmaple application is available at:

When you’re ready to deploy an update to OpenShift, git add, git commit, and git push can allow you to update your remote OpenShift example application using a standard code development workflow.

What’s Next?

News, Node.js, OpenShift Container Platform, OpenShift Online, OpenShift Origin, Ruby
, , ,
Comments are closed.