Skip to content
PLAY VIDEO PLAY VIDEO PLAY VIDEO
By Ben Wiseley

Simple strategy for Redis in Ruby-on-Rails

Here's a simple "it just works" approach to using Redis in your Rails app.

Quick background first. Redis requires a redis-server to be running. A redis-server runs against a port (the default port is 6381). While you can use name-spacing strategies to use the same redis-server for multiple redis db's it's not recommended (see KISS principle).

First: Install Redis

brew install redis

Second: Add to your project

Add this to Gemfile

gem 'redis'

And bundle install

Create config/redis/development.conf

daemonize yes
port 6390
logfile ./log/redis_development.log
dbfilename ./db/development.rdb

Create config/redis/test.conf

daemonize yes
port 6391
logfile ./log/redis_test.log
dbfilename ./db/test.rdb

These scripts tell redis to start up on port #### - it can be any number but I'd recommend not using 6381 (the default) in case something else is running against that. Redis conf's can do quite a bit and are worth reading up on before hitting production. Example of a fairly complex redis.conf file

While you can start up redis outside of rails, for simplicity sake, when working with other devs, why not make idiot proof for them? Less in the readme. We do this in this init script

Create config/init/redis_init.rb

require "redis"

redis_conf = File.read(Rails.root.join("config/redis", "#{Rails.env}.conf"))

port = /port.(\d+)/.match(redis_conf)[1] </span><span style="color:#2B2">redis-server </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>redis_conf<span style="font-weight:bold;color:#666">}</span></span><span style="color:#161"> res = </span><span style="color:#2B2">ps aux | grep redis-server</span><span style="color:#161">

unless res.include?("redis-server") && res.include?(redis_conf) raise "Couldn't start redis" end

REDIS = Redis.new(:port => port)

If you're deploying to Heroku and using redis-to-go - you can use the modified script below.


require "redis"

if ::Rails.env == "production" uri = URI.parse(ENV["REDISTOGOURL"]) REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password) else redisconf = File.read(Rails.root.join("config/redis", "#{Rails.env}.conf"))

port = /port.(\d+)/.match(redis_conf)[1] </span><span style="color:#2B2">redis-server </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>redis_conf<span style="font-weight:bold;color:#666">}</span></span><span style="color:#161"> res = </span><span style="color:#2B2">ps aux | grep redis-server</span><span style="color:#161">

unless res.include?("redis-server") && res.include?(redis_conf) raise "Couldn't start redis" end

REDIS = Redis.new(:port => port) end

Now you should be able to fire up bundle exec rails c and see that it's working by using the REDIS constant

irb(main):001:0> REDIS.keys
=> []

Latest Articles by Our Team

Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.

We Hire Only the Best

reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.

We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.

Free Community Workshops

We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.

  • Webinars

    Webinars

    Webinars are our online portal for tips, tricks and lessons learned in everything we do. Make the most of this free resource to help you become a better developer.

    Learn more about webinars

  • Installfest

    Installfest

    The Ruby on Rails Installfest includes a full setup of your development environment and step-by-step instructions on how to build your first app hosted on Heroku. Over 1,800 attendees to date and counting.

    Learn more about Installfest

  • Development Hub

    Development Hub

    The Ruby on Rails Development Hub is a monthly event where you will get the chance to spend time with our team and others in the community to improve and hone your Ruby on Rails skills.

    Learn more about Development Hub

Get the “reinteractive Review” Monthly Email