Redis cache – avoid timeout using ping

Are you getting those mysterious Airbrakes telling you a Timeout has occurred trying to talk to Redis and its kept you awake at night worrying what your poor users see while your (hopefully) slave instance takes over for your master in your redis-cluster?

Worry no more! Because we’re going to proactively ping our current Redis server connection to see if its up and hope to catch it napping before our users do. Ping is available via the redis library but how to get access to it from our Rails app?

Here’s how we’ll schedule a ping every 30 minutes. I’m using Rufus but you can use your scheduling gem of your choice:

require 'rufus-scheduler'
scheduler =

# keep-alive...
scheduler.every '30m' do
  store = ActiveSupport::Cache.lookup_store(MyApp::Application.config.cache_store)"Pinging Redis via cache-store ...")

As long as your session and cache stores both use the same cache server (but hopefully with different keys such as /sessions and /cache, respectively) you can use the above method of retrieving the current Redis client connection held in the @data instance variable of the ActiveSupport::Cache::RedisStore retrieved by ActiveSupport::Cache.lookup_store.


P.s. want connection pooling? check out this cool contribution by @findchris on github: