0

I have Redis system that utilizes key nesting and key composition with different prefixes:

SET prefix-1:concrete-key-1 value-1
SET prefix-1:concrete-key-2 value-2
SET prefix-2:concrete-key-1 value-3
SET prefix-2:concrete-key-2 value-4
SET id-123 concrete-key-1
SET id-456 concrete-key-2
SET id-789 concrete-key-1

So by using id-xxx and <prefix> I can get to specific value in 2 commands

id-123 -> concrete-key-1
prefix-1 + concrete-key-1 -> value-1

To reduce redis call and traffic amount I've created small LUA script to do getter on Redis side:

key = redis.call('GET', KEYS[1]);
return redis.call('GET', <prefix>..key);

Question is how to properly pass prefix in this case?
According to Redis I should explicitly specify keys as KEYS parameter: https://redis.io/topics/lua-api

Should I pass it as a key?

key = redis.call('GET', KEYS[1]);
return redis.call('GET', KEYS[2]..key);

However there are no standalone <prefix> keys, they are all composed of <prefix>:<key>. So should I pass prefixes as an argument?

key = redis.call('GET', KEYS[1]);
return redis.call('GET', ARGV[1]..key);

Or does it even makes sense to pass it like that? Is there other way how to properly get value using scripts?

1 Answers1

0

The purpose of declaring KEYS is so that Redis Cluster can route the script to the right server, and fail fast if you try to reference keys residing on different servers. See my answer here for more detail.

So your first step is to think about whether all possible key combinations will reside on same server. If not, your won't be able to run your script in any case.

If so, then your current scheme of just passing the id-123 key in the KEYS argument is fine. It definitely does not make sense to pass the prefix in KEYS, since it's not one and Redis won't know what to do with it. Whether to hardcode it or pass it in ARGV is up to you, just like it would be if you were calling a function.

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102