Skip to content

Connection Management

Jean byroot Boussier edited this page Apr 6, 2022 · 4 revisions

Beginning in Redis 5.0, with the removal of Redis.current, Redis no longer provides a mechanism for managing a global connection.

Back when Redis.current was introduced, Ruby applications were rarely using threads, so a single global client made some things simpler. But most modern applications now use threads, so having a single client is an anti-pattern.

It is expected that end-users implement their own mechanism such as this 'RedisConnection' utility function

module RedisConnection
  extend self

  def redis_config
    @redis_config ||= Rails.application.config_for(:redis).merge(
      # per https://devcenter.heroku.com/articles/securing-heroku-redis
      ssl_params: {verify_mode: OpenSSL::SSL::VERIFY_NONE},
    )
  end

  def current
    @current ||= Redis.new(redis_config)
  end

  def current=(redis)
    @current = redis
  end

  def ok?
    current.ping == "PONG"
  rescue Redis::BaseConnectionError => e
    warn "could not connect to redis: #{e.inspect}"
    false
  end
end

And that ideally they'd use https://rubygems.org/gems/connection_pool. See: https://github.com/mperham/sidekiq/wiki/Using-Redis#complete-control

Clone this wiki locally