Connecting Redis events to Lua Script execution and concurrency issues

559 views Asked by At

I have grouped key value pairs or data structures built using Redisson library. The design is that a change in value of any group of value(s) should be sent as event to subscribing Lua scripts. These scripts then do computations and update another group's key-value pair. This process is implemented as a chain such that once the Lua script updates a key-value per, that in turn generates a event and another Lua script does the work similar to first Lua script based on certain parameters.

Question 1: How to connect the Lua script and the event?

Question 2: Events are pipelined but it may be that my Lua Scripts may have to wait for network IO. In that case, I assume the next event is processed and the subscribing script executed. this for me is a problem because first script hasn't finished updating the key-value pair it needs to and the second script is going ahead with its work. This will cause errors for me. Is there a way to get over this?

Question 3: How to emit events from Redisson datastructures and I need the Lua script to understand that data structure's structure. How?

1

There are 1 answers

0
Redisson_RuiGu On BEST ANSWER

At the time of writing, Redis (3.2.9) does not allow blocking commands inside Lua scripts, including the subscribe command. So it is impossible to achieve what you have described via Lua script.

However you can do it using Redisson Topic and/or Redisson distributed services:

  1. Modify a value, send a message to a channel. Another process receives the message, do the computation and updating.

Or ...

  1. If there's only one particular process that does the computation and updating, you can use Redisson remote service to tell this process do the work, it works like RPC. Maybe it is able to modify the first value too.

Or ...

  1. Create the whole lot as one runnable job and send it to be processed by a Redisson remote executor. You can also choose to schedule the job if it is not immediately required.