10

I have a Redis Cluster consisting of multiple nodes. I want to update 3 different keys in a single atomic operation. My Lua script is like:

local u1 = redis.call('incrby', KEYS[1], ARGV[1])
local u2 = redis.call('incrby', KEYS[2], ARGV[1])
local u3 = redis.call('incrby', KEYS[3], ARGV[1])

And I fired it with:

EVAL script 3 key1 key2 key3 arg

But I got the error message:

WARN Resp(AppErr CROSSSLOT Keys in request don't hash to the same slot)

The above operations cannot be done, and the updates will fail. It seems I cannot modify the keys in different nodes with a single Lua script. But according to the doc:

All Redis commands must be analyzed before execution to determine which keys the command will operate on. In order for this to be true for EVAL, keys must be passed explicitly. This is useful in many ways, but especially to make sure Redis Cluster can forward your request to the appropriate cluster node.

Note this rule is not enforced in order to provide the user with opportunities to abuse the Redis single instance configuration, at the cost of writing scripts not compatible with Redis Cluster.

So I think as long as I follow the key passing rule, the script should be compatible with Redis Cluster. I wonder what's the problem here and what should I do to update all keys in a single script.

James Mishra
  • 4,249
  • 4
  • 30
  • 35
J Freebird
  • 3,664
  • 7
  • 46
  • 81

1 Answers1

20

I'm afraid you've misunderstood the documentation. (And I agree that it's not very clear.)

Redis operations, whether commands or Lua scripts, can only work when all the keys are on the same server. The purpose of the key passing rule is to allow Cluster servers to figure out where to send the script and to fail fast if all the keys don't come from the same server (which is what happened in your case).

So it's your responsibility to make sure that all the keys you want to operate on are located on the same server. The way to do that is to use hash tags to force keys to hash to the same slot. See the documentation for more details on that.

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
  • I see. The reason I used a cluster is to want different keys to be in different nodes to scale. So in this case, I might just give up updating the keys atomically. Thanks a lot. – J Freebird Jul 11 '16 at 18:19
  • 1
    maybe I am a bit late, but I have a follow up question to that: If I make sure an unspecified key is on the same cluster, can I still use Lua scripts for that? So I break the rule of having to specify all keys, but follow the rule that all keys are on the same server. – user3688217 Feb 01 '17 at 10:39
  • 1
    @user3688217: Yes, I believe so. As long as the key is on the same server it will work, even if the key was undeclared. – Kevin Christopher Henry Feb 02 '17 at 16:02
  • I suppose mget can get keys on different nodes in a redis cluster ? – maki Feb 17 '20 at 08:35