Is redis EVAL really atomic and crash safe?
Asked Answered
T

1

7

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)

Tammietammuz answered 23/4, 2020 at 13:58 Comment(0)
B
1

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.

Backandforth answered 2/12, 2021 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.