0
votes

Question

What is a best practice to pass an array of objects into Lua script? Is there any better way than converting objects to JSON and parse them with cjson within a script?

More context

I have streaming application which keeps it state in Redis. Every second we get 5-100 events, all operations are done within single transaction in order to boost performance like following:

RedisCommands<String, String> cmd = getOrCreateRedisClient();
cmd.multi();
for (Event event: listOfEvents) {
   cmd.sadd("users", event.getUserId());
   cmd.sadd("actions", event.getActionId());
   cmd.incrbyfloat("users:" + event.getUserId(), event.getImpact());
}
cmd.exec();

Now I have to move this logic to Lua scripts. I suppose it also will be faster to pass an array of events to Lua script instead of making up to 100 script invocations (one per event). Am I right? What is the best way to pass list of events to Lua script?

1

1 Answers

0
votes

It depends...

If your logic won't change in the future, i.e. you'll only use user id, action id, and impact of an event, you can just pass these three elements to Lua:

redis-cli --eval your-script.lua , userid1 actionid1 impact1 userid2 actionid2 impact2 userid3 actionid3 impact3

In this case, you don't need to convert an event object to JSON string, and Lua script doesn't need to parse JSON string, so it should be faster.

However, if your logic might change in the future, i.e. you might need to use other members of an event, you'd better convert an event object to a JSON string, and pass an array of JSON string to Lua script:

redis-cli --eval your-script.lua , {json1} {json2} {json3}

So that these changes will be transparent to your code, and you only need to change the Lua script.