redis/redis-rb · error · Redis::Distributed::CannotDistribute

RANDOMKEY cannot be used in Redis::Distributed because the k

Error message

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.

What it means

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.

Source

Thrown at lib/redis/distributed.rb:243

    def keys(glob = "*")
      on_each_node(:keys, glob).flatten
    end

    # Move a key to another database.
    def move(key, db)
      node_for(key).move(key, db)
    end

    # Copy a value from one key to another.
    def copy(source, destination, **options)
      ensure_same_node(:copy, [source, destination]) do |node|
        node.copy(source, destination, **options)
      end
    end

    # Return a random key from the keyspace.
    def randomkey
      raise CannotDistribute, :randomkey
    end

    # Rename a key.
    def rename(old_name, new_name)
      ensure_same_node(:rename, [old_name, new_name]) do |node|
        node.rename(old_name, new_name)
      end
    end

    # Rename a key, only if the new key does not exist.
    def renamenx(old_name, new_name)
      ensure_same_node(:renamenx, [old_name, new_name]) do |node|
        node.renamenx(old_name, new_name)
      end
    end

    # Sort the elements in a list, set or sorted set.
    def sort(key, **options)

View on GitHub (pinned to 2ba9010b91)

Solutions

  1. Do not use randomkey on Redis::Distributed; there is no global keyspace across the shards.
  2. Pick a node (e.g. nodes.sample) and call randomkey on that node directly.

When it happens

Trigger: Thrown at lib/redis/distributed.rb:243 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of redis/redis-rb@2ba9010b91 (2026-08-23). Data as JSON: /api/errors/074a1fea891c90b6. Report an issue: GitHub.