集群模式下使用脚本注意事项

redis 集群模式使用注意事项

使用集群时


如果使用脚本对key访问,脚本中禁止出现多key访问,只能对一个key访问

集群版会将不同的key hash到不同节点上,如果出现跨节点访问,那么将会出现报错,引用官方的解释如下

Redis Cluster implements all the single key commands available in the non-distributed version of Redis. Commands performing complex multi-key operations like set unions and intersections are implemented for cases where all of the keys involved in the operation hash to the same slot.

Redis Cluster implements a concept called hash tags that can be used to force certain keys to be stored in the same hash slot. However, during manual resharding, multi-key operations may become unavailable for some time while single-key operations are always available.

Redis Cluster does not support multiple databases like the standalone version of Redis. We only support database 0; the SELECT command is not allowed

官网链接

测试


  1. 部署集群版redis,具体部署过程参考官网,部署的结果如下,注意使用redis 5.0+版本

     .\redis-cli.exe --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
     >>> Performing hash slots allocation on 6 nodes...
     Master[0] -> Slots 0 - 5460
     Master[1] -> Slots 5461 - 10922
     Master[2] -> Slots 10923 - 16383
     Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
     Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
     Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
     >>> Trying to optimize slaves allocation for anti-affinity
     [WARNING] Some slaves are in the same host as their master
     M: 87c044b9bef0c1ff4195cd97849a4906ff117008 127.0.0.1:7000
        slots:[0-10922] (5461 slots) master
     M: 20d97ec5b8a129366f7a7496a85404efb8e71371 127.0.0.1:7001
        slots:[5461-10922] (5462 slots) master
     M: 7a06e73f83922d81356ccd832c24259a99103b2d 127.0.0.1:7002
        slots:[10923-16383] (5461 slots) master
     S: 7fe11bbc6823aba9a8d94cb256440f17ca1121a0 127.0.0.1:7003
        replicates 87c044b9bef0c1ff4195cd97849a4906ff117008
     S: 9a8e3e98494d20dfe34d9ec814fd87b5895caf4f 127.0.0.1:7004
        replicates 20d97ec5b8a129366f7a7496a85404efb8e71371
     S: be03b397995428710aad5ee3248330f4394bc8eb 127.0.0.1:7005
        replicates 7a06e73f83922d81356ccd832c24259a99103b2d
     Can I set the above configuration? (type 'yes' to accept): yes
     >>> Nodes configuration updated
     >>> Assign a different config epoch to each node
     >>> Sending CLUSTER MEET messages to join the cluster
     Waiting for the cluster to join
     .
     >>> Performing Cluster Check (using node 127.0.0.1:7000)
     M: 87c044b9bef0c1ff4195cd97849a4906ff117008 127.0.0.1:7000
        slots:[0-10922] (10923 slots) master
        2 additional replica(s)
     S: 20d97ec5b8a129366f7a7496a85404efb8e71371 127.0.0.1:7001
        slots: (0 slots) slave
        replicates 87c044b9bef0c1ff4195cd97849a4906ff117008
     M: 7a06e73f83922d81356ccd832c24259a99103b2d 127.0.0.1:7002
        slots:[10923-16383] (5461 slots) master
        1 additional replica(s)
     S: be03b397995428710aad5ee3248330f4394bc8eb 127.0.0.1:7005
        slots: (0 slots) slave
        replicates 7a06e73f83922d81356ccd832c24259a99103b2d
     M: 9a8e3e98494d20dfe34d9ec814fd87b5895caf4f 127.0.0.1:7004
        slots: (0 slots) master
     S: 7fe11bbc6823aba9a8d94cb256440f17ca1121a0 127.0.0.1:7003
        slots: (0 slots) slave
        replicates 87c044b9bef0c1ff4195cd97849a4906ff117008
     [OK] All nodes agree about slots configuration.
     >>> Check for open slots...
     >>> Check slots coverage...
     [OK] All 16384 slots covered.
    
  2. 对两个key设置值

     set testkey test
     OK
    	
     set foo test
     -> Redirected to slot [12182] located at 127.0.0.1:7002
     OK
    

    当出现Redirected to slot [12182] located at 127.0.0.1:7002说明key foo被分配到其他节点上

  3. 执行脚本

     eval "local foo = redis.call('get','foo') local test = redis.call('get','testkey') return test" 0
    	
     (error) ERR Error running script (call to f_0c156039358148061b8c221e36754272416ca3c4): @user_script:1: @user_script: 1: Lua script attempted to access a non local key in a cluster node
    

    脚本中同时访问了key foo 和 key testkey;出现如下错误(error) ERR Error running script (call to f_0c156039358148061b8c221e36754272416ca3c4): @user_script:1: @user_script: 1: Lua script attempted to access a non local key in a cluster node

最后


所以客户端程序使用脚本时,禁止出现多key访问,开发阶段是单节点,问题暴露不出来,上线后是集群版,当暴露问题再修改会增加成本