Photo-1 Photo-2 Photo-3 Photo-4 Photo-5 Photo-6



Full-time web developer. Part-time smart ass.

I'm Brent Collier.

After a year and a half as an engineer on Twitter's Trust & Safety team, I'm looking for my next gig. Contact me if you know of something interesting.


Configuring Redis Store for Rails 2.3.x

Posted on 05/23/2012

I wanted to use Redis as the cache store for a Rails 2.3.8 app. After a little shopping around, I settled on RedisStore as the cache adapter. I following the configuration directions on the RedisStore website and restarted the app, expecting everything to be hunky-dory. Instead, I was kicked in the face with this:

uninitialized constant ActiveSupport::Cache::RedisStore

My redis-store configuration looked like this:

redis_config = YAML.load_file(RAILS_ROOT + '/config/redis.yml')[RAILS_ENV]
config.cache_store = :redis_store, redis_config

I took another look at the config instructions and realized I was using the Rails 3.x setup. Following the 2.x instructions, I updated the configuration to look like this:

redis_config = YAML.load_file(RAILS_ROOT + '/config/redis.yml')[RAILS_ENV]
config.gem 'redis-store'
config.cache_store = :redis_store, redis_config

After restarting the app, I was still seeing the same uninitialized constant error. Finally, a bit more googling revealed that v1.1.0 of redis-store didn't jive with Rails 2.3.8. Evidently what I needed was v1.0.0.1. I'm not sure why it's v1.0.0.1 and not v1.0.1, but I checked and saw that v1.0.0 was yanked for some reason.

alt text

Anyway, so I updated my Gemfile to specify version v1.0.0.1 of redis-store and bam! The app starts up without yelling at me. Good stuff. Or so I thought.

Everything was hunky-dory in my local development environment, but when I started testing in our staging environment, something was up. Our staging environment consists of two app servers, one of which has the Redis instance running on it.

Attempts to connect to Redis repeated failed with an "unable to connect" error on one web server, but not the other. Examining the redis connection, it was clear that the app was trying to access Redis via localhost, the default when no valid connection options were supplied. This explained why it worked on one app server and not the other, since Redis was in fact running locally on one of them.

A little console debugging demonstrated that the redis config expected a hash with symbol keys, whereas the config hash loaded from the yaml file had string keys.

config.cache_store = :redis_store, redis_config.symbolize_keys

Updating the redis-store config to symbolize the keys allowed the connection to be properly created, and everything was working as intended.