Question

I have the following setup:

  • 2 different datastructures: Sets, Strings
  • They are in different namespaces *:collections:*, *:resources:*
  • The client doesn't know about this and I try to get both namespaces every time.
  • Based on exists I decide which datastructure to get finally.
  • all calls to redis are done asynchronous (vert.x redis-mod)

Now I have to decide if I execute this as lua script or as single commands.

The lua script I came up with:

local path = KEYS[1]
local resourcesPrefix = ARGV[1]
local collectionsPrefix = ARGV[2]

if redis.call('exists',resourcesPrefix..path) == 1 then
    return redis.call('get',resourcesPrefix..path)
elseif redis.call('exists',collectionsPrefix..path) == 1 then
    return redis.call('smembers',collectionsPrefix..path)
else
    return "notFound"
end 

Are there any pros and cons for single calls or lua script?

Was it helpful?

Solution

Yes, LUA script is a best solution in case of EVALSHA call:

  • You are working woth redis asynchronous. So LUA helps you to reduce number of code and code readability.
  • LUA case is faster becouse of reduce network communication.

I think you can write your code with just 2 commands. You do not need exists in your code.

local path = KEYS[1]
local resourcesPrefix = ARGV[1]
local collectionsPrefix = ARGV[2]
local ret

set ret = redis.call('get',resourcesPrefix..path)
if ret then
   return ret
end  
set ret = redis.call('smembers',collectionsPrefix..path)
if ret then
   return ret
end  

return "notFound" 

OTHER TIPS

It looks like a good use of Redis LUA scripting to me. The execution time will be short (long scripts are to be avoided as they are blocking). It avoids doing multiple calls, so reduces the total network communication time. So I think it's a better solution than single calls, if you make many of these calls. Especially if you use EVALSHA to have the script cached in Redis.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top