Redis: Why is Lua scripting going to replace transactions?

Its true that lua scripts can do whatever transactions can, but I don't think Redis transactions are going away.

EVAL script does not let you watch variables

When an eval script is running, nothing else can run concurrently. So, watching variables is pointless. You can be sure that nobody else has modified the variables once you have read the values in the script.

The problem I see with EVAL is that you cannot GET the state of those variables inside of the script and make a unique set of writes based on the state of those variables.

Not true. You can pass keys to the eval script. Within your eval script, you can read the values from Redis, and then based on those values conditionally execute other commands.

The script is still deterministic. If you take that script and run it on the slave, it will still execute the same write commands because the master and slave have the same data.


EVAL scripting actually extends and simplifies functionality of transactions.

It may help to view the two types of transactions in Redis in the following way:

1. procedural (MULTI EXEC)

Pure MULTI EXEC is grouping commands to be executed in one go and returns an array of replies from each command. It has a serious limitation. It does not allow using an intermediate result from one command in the next one within the transaction. In this pure form it's not very useful in practical applications. It's basically a pipelining with guaranteed atomicity.

Adding the WATCH key before the transaction, allows for optimistic locking and using in the transaction the values obtained from Redis outside the transaction. If a race condition happens, the transaction fails and must be retried. This complicates application logic and the optimism is often unwarranted since you may end up in the endless retrying loop.

2. functional (EVAL scripting)

EVAL is not only grouping Redis commands but also gives you the power of the full programming language, notably conditions, loops, and local variables. In the Lua script you can read values from Redis in one command and use them later in the next command.

You submit the script which is executed in an atomic way. It's guaranteed by the single threaded, "stop the world" approach. No other script or Redis command will be executed while the script is being executed. Consequently EVAL also has a limitation: scripts must be small and fast to prevent blocking other clients.

We also need to trust that other clients don't submit a slow script which should be considered as a programming error. It suites well into the security model since "Redis is designed to be accessed by trusted clients inside trusted environments".

Tags:

Redis