If you're using a Redis cache in Rails (e.g. :redis_cache_store), it's possible to configure additional parameters for your Redis connection.
Example config for Rails 7.2
config.cache_store = :redis_cache_store, {
pool: { timeout: 0.5 },
read_timeout: 0.2, # default 1 second
write_timeout: 0.2, # default 1 second
# Attempt two reconnects with some wait time in between
reconnect_attempts: [1, 5], # default `1` attempt in Redis 5+
url: REDIS_URL,
error_handler: ->(method:, returning:, exception:) {
Sentry.capture_exception(exception)
},
}
Timeouts
You probably want to adapt these settings depending on your application. The default value for read_timeout and write_timeout is 1 second, which is quite high for a standard application, where regenerating the cached values probably is a lot faster than the 1 second timeout. Be careful when changing these values, because setting a too low value for timeouts can make your cache timeout too often. Note that Rails explicitly sets connect_timeout, read_timeout and write_timeout, which means that only configuring timeout will not propagate as expected.
Reconnection attempts
The Redis client will raise a Redis::ConnectionError if it can't connect. An integer for reconnect_attempts will be interpreted as seconds to wait. An array of numbers will be taken as the seconds to wait before each reconnect attempt.
Error handler
To notice exceptions raised by the client you can configure e.g. Sentry to pick up these exceptions.
Details
See the official guides Show archive.org snapshot for more information.
You can use Rails.cache.stats to get basic information about the current redis connection.