Dawei67
Dawei67

Reputation: 331

Is redis EVAL really atomic and crash safe?

Redis doc seems to affirm EVAL scripts are similiar too MULTI/EXEC transactions.

In my personnals words, this meens a LUA script guarantees two things :

(I don't have this concern with MULTI/EXEC on this second point because with MULTI/EXEC you can't do writes based on previous commands)

(sorry for basic english, I am french)

Upvotes: 7

Views: 1608

Answers (1)

Didi Bear
Didi Bear

Reputation: 366

Just tested it using this very slow script:

eval "redis.call('set', 'hello', 10); for i = 1, 1000000000 do redis.call('set', 'test', i) end" 0

^ This sets the hello key to 10 then infinitely sets the test key to a number.

While executing the script, Redis logs this warning:

# Lua slow script detected: still in execution after 5194 milliseconds. You can try killing the script using the SCRIPT KILL command. Script SHA1 is: ...

So I then tested to shutdown the container entirely while the script is executing to simulate a crash.

After the restart, the hello and test keys are nil, meaning that none of the called commands are actually been executed. Thus, scripts are indeed atomic and crash safe as the doc states.

My belief is that Redis wraps the Lua scripts within a MULTI/EXEC to make them atomic, or at least it has the same effect.

Upvotes: 1

Related Questions