I’m new to Redis, and this is how I see it:
It seems to me that watch/multi/exec is a rather awkward way of implementing optimistic transaction. The duration between watch and exec can be quite long and would typically last several roundtrips to complete (the client would need to send multiple commands to Redis server).
Lua script, on the other hand, enables much more powerful transactions (rollback, complex condition checking…) and doesn’t require multiple roundtrips to complete (all in one command). Also it’s much more straight-forward.
I understand that Lua script is relatively new compared with watch/multi/exec. So is there any reason to use watch/multi/exec now?
2
Answers
If you can do it all in one short Lua script that is probably the most efficient way to do it.
But, if your transaction might be long or requires accessing external resources to complete you should avoid Lua, especially since the Lua script execute blocks the Redis server to parallel calls.
There are pros and cons to anything.
Why use MULTI/EXEC (main reasons):
Once you need to read data in your transaction (i.e. use WATCH), I’d immediately go to Lua for its convenience. Of course, long-running transactions or Lua scripts block the server so try to avoid these. While it is harder to do so in the context of MULTI/EXEC, an infinite loop in Lua can be done with a single line 🙂