0
0
Redisquery~10 mins

EVAL command for Lua execution in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - EVAL command for Lua execution
Start EVAL command
Parse Lua script
Receive KEYS and ARGV
Execute Lua script with KEYS and ARGV
Return result to client
End
The EVAL command runs a Lua script on Redis server using provided keys and arguments, then returns the script's result.
Execution Sample
Redis
EVAL "return {KEYS[1], ARGV[1]}" 1 mykey myarg
Runs a Lua script that returns the first key and first argument passed to it.
Execution Table
StepActionInputLua VariablesOutput
1Receive EVAL commandScript: return {KEYS[1], ARGV[1]}, NumKeys: 1, Keys: [mykey], Args: [myarg]KEYS = ["mykey"], ARGV = ["myarg"]None yet
2Parse Lua scriptreturn {KEYS[1], ARGV[1]}KEYS = ["mykey"], ARGV = ["myarg"]None yet
3Execute Lua scriptreturn {KEYS[1], ARGV[1]}KEYS = ["mykey"], ARGV = ["myarg"]["mykey", "myarg"]
4Return resultResult from scriptNone["mykey", "myarg"]
💡 Script execution completes and returns the array with first key and argument.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
KEYSempty["mykey"]["mykey"]["mykey"]
ARGVempty["myarg"]["myarg"]["myarg"]
Outputnonenone["mykey", "myarg"]["mykey", "myarg"]
Key Moments - 2 Insights
Why do we specify the number of keys after the Lua script in EVAL?
Because Redis needs to know how many arguments are keys (in KEYS array) and how many are regular arguments (in ARGV). This is shown in execution_table row 1 where NumKeys is 1.
What happens if the Lua script tries to access KEYS or ARGV indices that don't exist?
The Lua script will get nil for those indices, which can cause errors or unexpected results. This is implied in the variable_tracker where KEYS and ARGV only have one element each.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of ARGV after Step 1?
Aempty
B["mykey"]
C["myarg"]
Dnil
💡 Hint
Check the 'Lua Variables' column in row 1 of execution_table.
At which step does the Lua script actually run and produce output?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the step where 'Execute Lua script' action happens in execution_table.
If the number of keys specified was 0, where would 'mykey' appear?
AIn KEYS array
BIn ARGV array
CNowhere, error occurs
DIn both KEYS and ARGV
💡 Hint
Refer to key_moments explanation about how Redis separates keys and arguments.
Concept Snapshot
EVAL command runs Lua scripts on Redis server.
Syntax: EVAL <script> <numkeys> <key1> ... <arg1> ...
KEYS array holds keys, ARGV holds other args.
Script runs atomically and returns result.
Use to extend Redis with custom logic.
Full Transcript
The Redis EVAL command executes a Lua script on the server. You provide the script, the number of keys, then the keys and arguments. Redis separates keys into the KEYS array and other arguments into ARGV. The Lua script runs with these inputs and returns a result. For example, the script 'return {KEYS[1], ARGV[1]}' returns the first key and first argument. The execution steps include receiving the command, parsing the script, running it with KEYS and ARGV, and returning the output. It's important to specify the number of keys correctly so Redis knows how to split inputs. If the script accesses keys or args that don't exist, it gets nil. This command allows custom logic inside Redis with atomic execution.