Search code examples
rubyruby-on-rails-4redisresqueredis-rails

How do I implement connection pooling for Redis-As-Rails-Cache (using Redis as rails cache) Rails 4.1?


How can I add connection pooling to my Redis setup?

for some reason I can't seem to find any information on this. I'm using redis (3.2.0) set up like so: (you can assume resque_uri and cache_uri has been parsed ahead of this c)

for resque:

$REDIS_BACKGROUND_JOB =  Redis.new(:host => resque_uri.host, 
                                  :port => resque_uri.port, 
                                  :password => resque_uri.password)

later in the initialization sequence, we do

Resque.redis = $REDIS_BACKGROUND_JOB

We have a 2nd Redis instance with a different memory, connection limit, and eviction policy, for the Rails cache

$REDIS_CACHE = Redis.new(:host => cache_uri.host, 
                        :port => cache_uri.port, 
                        :password => cache_uri.password)

UPDATE 2017-02-16@20-17EST: I am running two redis instances, one for resque (1.25.2) and another for the caching layer using redis-rails (5.0.1). I have updated the setup examples above. I suspect something is misconfigured in the initialization or there is a fundamental setup mismatch.

UPDATE 2017-02-16@21-37EST:

environments/development.rb has

config.cache_store = :redis_store, 'redis://localhost:6379/1/cache_store'


Solution

  • OK here's what I got working.

    added to my Gemfile

    gem 'connection_pool'

    For the RAILS CACHE:

    Initializing $REDIS_CACHE = Redis.new for the Rails cache is incorrect, as Rails does this inside of the config.cache_store setting in your environment files (config/environments/development.rb, config/environments/production.rb, etc)

    And then my initialization in my environment files is as follows:

    config.cache_store = :redis_store, ENV["REDISCLOUD_FOR_RAILS_CACHE_URL"], {pool_size: 10, pool_timeout: 10}

    (you will note that ENV["REDISCLOUD_FOR_RAILS_CACHE_URL"] is set as the full URI, including protocol, username, password, host, and port. An alternative is to pass these each one as its own parameter here in a hash.)