How to gracefully handle Rails cache failure? (redis-cache)

1.7k Views Asked by At

I'm using redis-cache for fragment caching in my app. When redis is down or unreachable, I get timeout exceptions that raise 500 errors.

This happens rarely, but for a cache I would expect rails to treat a timeout as:

  • no-op on write
  • cache-miss on read.

Any suggestions? Is this something that is configurable in rails caching, or do I need to do some monkey patching to handle this case gracefully?

The relevant part of my stacktrace is below, in case it helps in any way . . .


execution expired
 /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/1.8/timeout.rb:60:in `new'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/connection/ruby.rb:26:in `connect'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/connection/ruby.rb:128:in `with_timeout'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/connection/ruby.rb:25:in `connect'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/client.rb:227:in `establish_connection'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/client.rb:23:in `connect'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/client.rb:247:in `ensure_connected'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/client.rb:137:in `process'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/client.rb:206:in `logging'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/client.rb:136:in `process'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis/client.rb:46:in `call'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis.rb:157:in `get'
 /opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/1.8/monitor.rb:242:in `synchronize'
 bundle/gems/ruby/1.8/gems/redis-2.2.2/lib/redis.rb:156:in `get'
 bundle/gems/ruby/1.8/gems/redis-store-1.0.0.1/lib/redis/store/interface.rb:5:in `get'
 bundle/gems/ruby/1.8/gems/redis-store-1.0.0.1/lib/redis/store/marshalling.rb:13:in `get'
 bundle/gems/ruby/1.8/gems/redis-store-1.0.0.1/lib/active_support/cache/redis_store.rb:119:in `read_entry' 

1

There are 1 best solutions below

1
On

Kevin,

Curious, what Rails version are you using?

This issue on the redis-store project seems to be related to what that you are experiencing: https://github.com/jodosha/redis-store/issues/56. That fix only addresses Rails 3 though.

The fix for Rails 2 is here: https://github.com/jodosha/redis-store/pull/83. Looking at the source of redis-store 1.0.0.1, I don't see that fix being included.