Skip to content

Connection Management

Brian O'Rourke edited this page Apr 12, 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)
  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