Dmitry Polyakovsky
Dmitry Polyakovsky

Reputation: 1585

Disable Rails caching if Redis is down

I am using Rails 4.1 and and setup shared Redis ElasticCache node for caching. I tried https://github.com/redis-store/redis-store and https://github.com/sorentwo/readthis and they seem great.

But what happens if Redis is down? Both readthis and redis-store completely fail. I'd rather have site be slow w/o cache than dead.

Does anyone have ideas? I thank you in advance.

Upvotes: 1

Views: 2085

Answers (2)

Parker Selbert
Parker Selbert

Reputation: 1546

As of https://github.com/sorentwo/readthis/pull/30 this is available directly in Readthis. It will be available in the upcoming 1.2 release. From the README:

In some situations it is desirable to keep serving requests from disk or the database if Redis crashes. This can be achieved wiht connection fault tolerance by enabling it at the top level:

Readthis.fault_tolerant = true

The default value is false, because while it may work for fetch operations, it isn't compatible with other state-based commands like increment.

Upvotes: 1

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34336

Here is an interesting discussion on this topic: Don't crash the application if redis is down

Since the issue is still open and they did not merge any fix for this yet, you can use one of the few suggestions from the discussion i.e. monkey patching like this:

# patch to do not crash on redis backend errors                                                 
# https://github.com/redis-store/redis-rails/issues/14                                          

    module ActiveSupport                                                                            
      module Cache                                                                                  
        class RedisStore                                                                   

          %w[increment decrement clear read_entry write_entry delete_entry].each do |method|        
            define_method "#{method}_with_rescue" do |*args, &block|                                
              begin                                                                                 
                self.send "#{method}_without_rescue", *args, &block                                 
              rescue                                                                                
                nil                                                                                 
              end                                                                                   
            end                                                                                     
            alias_method_chain method, :rescue                                                      
          end                                                                                       
        end                                                                                         
      end                                                                                           
    end 

Upvotes: 1

Related Questions