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

DISCARD cannot be used in Redis::Distributed because the key

Error message

DISCARD 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 "DISCARD 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:1346

    # Mark the start of a transaction block.
    def multi(&block)
      raise CannotDistribute, :multi unless @watch_key

      node_for(@watch_key).multi(&block)
    end

    # Execute all commands issued after MULTI.
    def exec
      raise CannotDistribute, :exec unless @watch_key

      result = node_for(@watch_key).exec
      @watch_key = nil
      result
    end

    # Discard all commands issued after MULTI.
    def discard
      raise CannotDistribute, :discard unless @watch_key

      result = node_for(@watch_key).discard
      @watch_key = nil
      result
    end

    # Control remote script registry.
    def script(subcommand, *args)
      on_each_node(:script, subcommand, *args)
    end

    # Set one or more contiguous values starting at an index in an array.
    def arset(key, index, *values)
      node_for(key).arset(key, index, *values)
    end

    # Get the value at an index in an array.
    def arget(key, index)

View on GitHub (pinned to 2ba9010b91)

Solutions

  1. Do not call discard on Redis::Distributed; transactions are not supported across shards.
  2. Restructure the code to avoid MULTI/DISCARD, or run it against a single-node connection.

When it happens

Trigger: Thrown at lib/redis/distributed.rb:1346 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/5555bb79d698f039. Report an issue: GitHub.