Using Redis as Node.js Express Session Storage on Heroku

The default way to use sessions with Node.js and Express is using the bundled memory store. However, if you want to deploy your app to Heroku and use multiple dynos it won’t work because the memory store doesn’t work across dynos.

The solution is to use a database for session storage and it seems like Redis or MongoDB are the most common used together with Node.js and Express. Here a quick walk through to setup Redis for this purpose. The example is using CoffeeScript.

First you want to install Redis on your machine to test against localhost. Download the source and then:

Install the Redis add-on on Heroku: $ heroku addons:add redistogo
You can check your config to make sure process.env.REDISTOGO_URL is available: $ heroku config

Now the implementation in your main app.coffee file:

Make sure to add the new dependencies to your package.json:
“connect-redis”: “1.3.0″, “redis”: “0.7.1″

That’s pretty much it. Now just scale your dynos: $ heroku ps:scale web=2

Thanks to Jerod Santo for his blog post which got me started.
Also read the docs on Express Session Handling.

4 comments to Using Redis as Node.js Express Session Storage on Heroku