redisluaredis-cluster

Redis Lua function call failing with CROSSSLOT error


I have a Redis 7.x cluster with 3 master nodes running on ports 7000, 7001, 7002. Each master has 1 slave/replica.

I am trying to load and run this Lua based function:

#!lua name=paramsLogLib
local function paramsLog(keys, args)
    local param1 = keys[1]
    local param2 = keys[2]
    local param3 = keys[3]
    redis.log(redis.LOG_NOTICE, param1, param2, param3)
end
redis.register_function('paramsLog', paramsLog)

The function has been loaded in all the 3 master nodes.

I tried to call the function paramsLog from all the 3 master nodes:

127.0.0.1:7000> fcall paramsLog 3 p1 p2 p3

127.0.0.1:7001> fcall paramsLog 3 p1 p2 p3

127.0.0.1:7002> fcall paramsLog 3 p1 p2 p3

I get the same error:

(error) CROSSSLOT Keys in request don't hash to the same slot

How can I pass different parameters to any function in the Redis? Why does Redis need to check the hash slot of the parameters itself?


Solution

  • Because the Redis Cluster command, including Lua script, runs on a single node, and it cannot access keys on other nodes in the cluster. If your command/script needs to access multiple keys, which are not located on the same slot, you get the CROSSSLOT error.

    In your case, your 3 keys, i.e. p1, p2, p3, located on different slots (maybe also on different nodes), and that's why you get the error.

    As @Kevin mentioned in the comment, if p1, p2 and p3 are not keys, but parameters, you can pass them as arguments, and access them with args. If they indeed the keys, you can try to use hash-tag to ensure these keys located on the same slot. So that your script can access all of them.