Want to use Redis on Heroku (Redis::CannotConnectError ( Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)) )

Yuki Ishikawa picture Yuki Ishikawa · Oct 7, 2013 · Viewed 8.4k times · Source

I want to use Redis on Heroku but I got this error Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)) .

I checked these , but didn't get helped. Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED, deploying redis to heroku unable to connect, How to get Redis to start on Heroku? .

I use Ruby 2.0.0p247 and Rails4. I'm using puma.

I use RedisToGo(nano) and, in /config/initializers/redis.rb I write this.

uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/" )
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

I checked "REDISTOGO_URL" is the same URL that I can see when $ heroku config .

I did Redis restart on Heroku GUI, but it doesn't work.

Please tell me anything I need to do. Thank you for your help.

Answer

Serge Seletskyy picture Serge Seletskyy · Nov 19, 2014

Check which ENV variable should contain redis configuration

For example if you are going to use Redis for Sidekiq you need to map ENV["REDISTOGO_URL"] to ENV["REDIS_PROVIDER"] because Sidekiq is expecting to get redis configuration from ENV["REDIS_PROVIDER"]

You can do it this way

heroku config:set REDIS_PROVIDER=REDISTOGO_URL

Also you don't need to URI.parse, Redis will do it instead of you in case it is needed.

$redis = Redis.new(url: ENV['REDISTOGO_URL' || "redis://localhost:6379/"])

I suggest to use the gem figaro to set ENV variables for your local development. Thus you remove OR statement

$redis = Redis.new(url: ENV['REDISTOGO_URL'])

and set alternative value in your config/application.yml

REDISTOGO_URL: redis://127.0.0.1:6379