{"record":{"id":"0b278d3ba5568219","repo":"redis/redis-rb","slug":"redis-cluster-doesn-t-implement-connection","errorCode":null,"errorMessage":"Redis::Cluster doesn't implement #connection","messagePattern":"Redis::Cluster doesn't implement #connection","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"cluster/lib/redis/cluster.rb","lineNumber":48,"sourceCode":"      # @param error_message [String]\n      def initialize(errors, error_message = 'Command errors were replied on any node')\n        @errors = errors\n        super(error_message)\n      end\n    end\n\n    # Raised when cluster client can't select node.\n    class AmbiguousNodeError < BaseError\n    end\n\n    class TransactionConsistencyError < BaseError\n    end\n\n    class NodeMightBeDown < BaseError\n    end\n\n    def connection\n      raise NotImplementedError, \"Redis::Cluster doesn't implement #connection\"\n    end\n\n    # Create a new client instance\n    #\n    # @param [Hash] options\n    # @option options [Float] :timeout (5.0) timeout in seconds\n    # @option options [Float] :connect_timeout (same as timeout) timeout for initial connect in seconds\n    # @option options [Symbol] :driver Driver to use, currently supported: `:ruby`, `:hiredis`\n    # @option options [Integer, Array<Integer, Float>] :reconnect_attempts Number of attempts trying to connect,\n    #   or a list of sleep duration between attempts.\n    # @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not\n    # @option options [Array<String, Hash{Symbol => String, Integer}>] :nodes List of cluster nodes to contact\n    # @option options [Boolean] :replica Whether to use readonly replica nodes in Redis Cluster or not\n    # @option options [Symbol] :replica_affinity scale reading strategy, currently supported: `:random`, `:latency`\n    # @option options [String] :fixed_hostname Specify a FQDN if cluster mode enabled and\n    #   client has to connect nodes via single endpoint with SSL/TLS\n    # @option options [Class] :connector Class of custom connector\n    # @option options [String, Array<String>, false] :driver_info Identity a library built on top of","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/redis/redis-rb/blob/2ba9010b91dab9e0fde1fbae3a9aae003f8bc307/cluster/lib/redis/cluster.rb#L30-L66","documentation":"Redis::Cluster overrides #connection to always raise NotImplementedError. A cluster client is a multiplexer over many per-node connections managed by the redis-cluster-client gem, so there is no single connection whose host/port/path could be reported. The override makes code written for the standalone Redis client fail loudly instead of reading a misleading connection hash.","triggerScenarios":"Calling #connection on a Redis::Cluster instance: directly (redis.connection), or via shared/instrumentation code that reads redis.connection[:host]/[:port]/[:db] and is handed a client built with Redis.new(cluster: [...]). The gem's own cluster tests call it to assert the behavior (test_connection_information, test_default_id_*).","commonSituations":"An app migrated from standalone Redis to Redis Cluster (cluster: option or redis://cluster URLs); monitoring, APM, or logging helpers built against the standalone client that call #connection to build a client id; test suites shared between standalone and cluster clients.","solutions":["Branch on client type before introspecting: call redis.connection only when the client is not a Redis::Cluster instance","For cluster topology use redis.cluster(:nodes), redis.cluster(:slots), or redis.cluster(:info) instead — they query a random node and return structured data","Wrap shared code in rescue NotImplementedError and fall back to a label built from the node list you configured"],"exampleFix":"# before\ninfo = redis.connection  # => NotImplementedError when redis is a Redis::Cluster\n\n# after\ninfo = if redis.is_a?(Redis::Cluster)\n  { cluster: redis.cluster(:info) }\nelse\n  redis.connection\nend","handlingStrategy":"type-guard","validationCode":"info = redis.connection unless redis.is_a?(Redis::Cluster)","typeGuard":"def single_connection?(redis)\n  !redis.is_a?(Redis::Cluster)\nend","tryCatchPattern":"begin\n  info = redis.connection\nrescue NotImplementedError\n  info = nil  # cluster client: no single connection to report\nend","preventionTips":["Treat #connection as a standalone-client-only API in shared code","Build client labels from configuration you already hold (URLs/node list), not from the connection object","Exercise both client types in tests when writing shared helpers"],"tags":["redis-cluster","not-implemented","client-introspection"],"backgroundTag":"method-not-implemented","analyzedSha":"2ba9010b91dab9e0fde1fbae3a9aae003f8bc307","analyzedAt":"2026-08-23T03:54:57.017Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}