Connecting Node.js to Redis To Go on Heroku

Heroku’s new Cedar stack is awesome for many reasons, one of which is first-class Node.js hosting.

I’m giving it a test run with an app that uses Redis for data caching and I couldn’t find any documentation on how to connect a Node app to the Redis To Go add-on. Here’s how I did it:

This assumes that you’ve already created the Heroku app or don’t need any help in doing so. Heroku has a great tutorial on how to deploy a Node app on Cedar.

First, add the Redis To Go add-on via the heroku command:

heroku addons:add redistogo

Installing the add-on will automatically add a REDISTOGO_URL environment variable to your app. You can see its value with the heroku config command.

The Redis client for Node that I use is node_redis, so the following is specific it for it. If you use another client, adjust as necessary.

When node_redis connects to a Redis instance on your local machine it assumes the default port and host information, so instantiating the client is as simple as this:

var redis = require("redis").createClient();

That works just fine in development, but we need it to authenticate to Redis To Go in production. To handle both cases I just check for the existence of the REDISTOGO_URL, like so:

if (process.env.REDISTOGO_URL) {
  // TODO: redistogo connection
} else {
  var redis = require("redis").createClient();
}

Everything should still work fine in development, but we still need to implement the Redis To Go connection. To do this we need to extract the port, hostname, and authentication string from REDISTOGO_URL using Node’s built-in url lib:

// inside if statement
var rtg   = require("url").parse(process.env.REDISTOGO_URL);
var redis = require("redis").createClient(rtg.port, rtg.hostname);

redis.auth(rtg.auth.split(":")[1]);

For some reason Node’s url lib won’t split up the auth section’s username and password, so that’s what is going on in the rtg.auth.split(":")[1] that is passed to the auth command.

That’s all it took to get my app connected and running smoothly. I hope this helps you do the same!



©Jerod Santo. Compiled from source by Jekyll. Original design credited to Jeff Kreeftmeijer.