{"record":{"id":"074a1fea891c90b6","repo":"redis/redis-rb","slug":"randomkey-cannot-be-used-in-redis-distributed-bec","errorCode":null,"errorMessage":"RANDOMKEY cannot be used in Redis::Distributed because the keys involved need to be on the same server or because we cannot guarantee that the operation will be atomic.","messagePattern":"RANDOMKEY cannot be used in Redis::Distributed because the keys involved need to be on the same server or because we cannot guarantee that the operation will be atomic\\.","errorType":"exception","errorClass":"Redis::Distributed::CannotDistribute","httpStatus":null,"severity":"error","filePath":"lib/redis/distributed.rb","lineNumber":243,"sourceCode":"    def keys(glob = \"*\")\n      on_each_node(:keys, glob).flatten\n    end\n\n    # Move a key to another database.\n    def move(key, db)\n      node_for(key).move(key, db)\n    end\n\n    # Copy a value from one key to another.\n    def copy(source, destination, **options)\n      ensure_same_node(:copy, [source, destination]) do |node|\n        node.copy(source, destination, **options)\n      end\n    end\n\n    # Return a random key from the keyspace.\n    def randomkey\n      raise CannotDistribute, :randomkey\n    end\n\n    # Rename a key.\n    def rename(old_name, new_name)\n      ensure_same_node(:rename, [old_name, new_name]) do |node|\n        node.rename(old_name, new_name)\n      end\n    end\n\n    # Rename a key, only if the new key does not exist.\n    def renamenx(old_name, new_name)\n      ensure_same_node(:renamenx, [old_name, new_name]) do |node|\n        node.renamenx(old_name, new_name)\n      end\n    end\n\n    # Sort the elements in a list, set or sorted set.\n    def sort(key, **options)","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/redis/redis-rb/blob/2ba9010b91dab9e0fde1fbae3a9aae003f8bc307/lib/redis/distributed.rb#L225-L261","documentation":"Error \"RANDOMKEY cannot be used in Redis::Distributed because the keys involved need to be on the same server or because we cannot guarantee that the operation will be atomic.\" thrown in redis/redis-rb.","triggerScenarios":"Thrown at lib/redis/distributed.rb:243 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Do not use randomkey on Redis::Distributed; there is no global keyspace across the shards.","Pick a node (e.g. nodes.sample) and call randomkey on that node directly."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"2ba9010b91dab9e0fde1fbae3a9aae003f8bc307","analyzedAt":"2026-08-23T03:54:57.017Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}