0

I have a Redis Cluster and I would like to execute a LUA script on a target node. When I do that I receive an error from Redis Cluster that say that keys must be on the same SLOT, but the script executes two commands on only 1 key.

if redis.call('HEXISTS', 'TEST', KEYS[1]) == 1
    then 
        redis.call('HSET', 'TEST', KEYS[1], ARGV[1])
        return 1
    end
return 0

I tried to use Hash Tag Key, but it doesn't work.

Davide Cerbo
  • 363
  • 3
  • 10

1 Answers1

3

You are getting this error because you're trying to access a Redis key named TEST, but don't make it known to Redis or your Redis client by listing it in the EVAL command.

Redis Lua scripts take two argument lists, one is a general purpose (ARGV[]) and the other is a strict list of the keys the script is going to access (KEYS[]).

You may have swapped the key name and field names in the HEXISTS call, i.e. you may need to use:

if redis.call('HEXISTS', KEYS[1], 'TEST') == 1
    then 
        redis.call('HSET', KEYS[1], 'TEST', ARGV[1])
        return 1
    end
return 0

This should work as long as the key name is properly passed on to the EVAL command.

yossi
  • 131
  • 4
  • thanks! the solution was under my nose, but I don't see :) The script below now works: if redis.call('HEXISTS', KEYS[1], ARGV[1]) == 1 then redis.call('HSET', KEYS[1], ARGV[1], ARGV[2]) return 1 end return 0 – Davide Cerbo Jan 18 '19 at 15:31