Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis::TimeoutError in Rails application

I keep getting Redis::Timeout error in my application (both in UI and in background jobs). I am using AWS ElastiCache service for Redis.

This is how I create a Redis connection. In my config/application.rb , I have:

$redis = Redis.new(host: REDIS_HOST, port: REDIS_PORT, db: REDIS_DB)

How can I avoid getting timeout errors? I am using the default connection settings as follows:

> $redis.client.options[:reconnect_attempts]
 => 1 
> $redis.client.options[:timeout]
 => 5.0 
> $redis.client.options[:tcp_keepalive]
 => 0 
> $redis.client.options[:inherit_socket]
 => false
like image 656
curiousboy Avatar asked Apr 02 '15 10:04

curiousboy


1 Answers

You should pool your Redis connections with the help of the Connection Pool Gem and increase the timeout value if the problem persists:

ConnectionPool.new(size: 5, timeout: 3) {Redis.new({:host => 'localhost', :port => 6379, :db => 1, :timeout => 240})}

Redis Gem

like image 113
Jean Meyer Avatar answered Oct 24 '22 23:10

Jean Meyer