One way to execute commands in REDIS, is via the EVAL script.
Redis uses the same Lua interpreter to run all the commands. Also
Redis guarantees that a script is executed in an atomic way: no other
script or Redis command will be executed while a script is being
executed.
Since redis is single threaded, why do we need EVAL to offer atomicity? I would expect that this is implied by the one running thread.
Am I missing something? Apologies if my question is pretty simple, I am quite new to redis
2
Answers
Redis uses a single thread to execute commands from many different clients. So if you want a group of commands from one client to be executed in sequence, you need a way to direct Redis to do that. That’s what
EVAL
is for. Without it, Redis could interleave the execution of commands from other clients in with yours.Every (data path) command in Redis is indeed atomic.
EVAL
allows you to compose an “atomic” command with a script that can include many Redis commands, not to mention control structures and some other utilities that are helpful to implement server-side logic. To achieve the similar “atomicity” of multiple commands you can also useMULTI/EXEC
blocks (i.e. transactions) by the way.Without an
EVAL
or aMULTI/EXEC
block, your commands will run one after another, but other clients’ commands may interleave between them. Using a script or transaction eliminates that.