Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

  • sequential : the lua script is run like it is alone on server, thats ok with me
  • atomic / one shot writes : this I don't understand with LUA scripts. when is the "EXEC like" called on LUA scripts ? Because with scripts you can do conditionnal writes based on reads (or even writes because some writes returns values like NX functions). So how can redis garanthee that either all or nothing is executed with scripts ? What happen if the server crash in the middle of a script ? rollback is not possible with redis.

(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)

like image 665
Dawei67 Avatar asked Nov 15 '22 12:11

Dawei67


1 Answers

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.

like image 167
Didi Bear Avatar answered Nov 30 '22 20:11

Didi Bear