0
0
Redisquery~10 mins

Accessing keys and arguments in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Accessing keys and arguments
Start command
Parse input
Identify keys
Identify arguments
Execute command using keys and args
Return result
The flow shows how a Redis command input is parsed to separate keys and arguments, then executed using those parts.
Execution Sample
Redis
EVAL "return {KEYS[1], ARGV[1]}" 1 mykey myarg
This Redis Lua script returns the first key and the first argument passed to the script.
Execution Table
StepInput PartActionValue ExtractedNotes
1EVALStart script execution-Begin processing the EVAL command
2"return {KEYS[1], ARGV[1]}"Parse Lua scriptScript codeScript to return first key and argument
31Number of keys1Indicates one key is passed
4mykeyExtract KEYS[1]mykeyFirst key identified
5myargExtract ARGV[1]myargFirst argument identified
6Execute scriptReturn values{mykey, myarg}Script returns key and argument
7-End-Execution complete
💡 Script execution ends after returning the first key and argument.
Variable Tracker
VariableStartAfter Step 4After Step 5Final
KEYS[1]-mykeymykeymykey
ARGV[1]--myargmyarg
Key Moments - 2 Insights
Why do we specify the number '1' after the script in the command?
The number '1' tells Redis how many keys are being passed. This is important because Redis separates keys from arguments based on this number, as shown in execution_table step 3.
What is the difference between KEYS and ARGV in the script?
KEYS holds the keys passed to the script, while ARGV holds the additional arguments. This distinction is clear in execution_table steps 4 and 5 where KEYS[1] and ARGV[1] are extracted separately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value is assigned to KEYS[1] at step 4?
Amykey
Bmyarg
C1
Dreturn {KEYS[1], ARGV[1]}
💡 Hint
Refer to execution_table row with Step 4 under 'Value Extracted' column.
At which step does the script extract the first argument (ARGV[1])?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Check execution_table rows for the step mentioning ARGV[1].
If the number of keys was changed from 1 to 2, what would change in the execution?
AArguments would be ignored
BTwo keys would be extracted as KEYS[1] and KEYS[2]
COnly one key would be extracted
DScript would fail to run
💡 Hint
Look at the role of the number after the script in execution_table step 3.
Concept Snapshot
Redis scripts receive keys and arguments separately.
Syntax: EVAL <script> <numkeys> <key1> ... <keyN> <arg1> ... <argM>
KEYS array holds keys; ARGV array holds arguments.
The number after script tells how many keys are passed.
Scripts access keys as KEYS[1], KEYS[2], etc., and args as ARGV[1], ARGV[2], etc.
Full Transcript
This visual execution trace shows how Redis processes a Lua script command that accesses keys and arguments. The command EVAL "return {KEYS[1], ARGV[1]}" 1 mykey myarg is parsed step-by-step. First, Redis reads the script and the number of keys (1). Then it extracts the first key 'mykey' as KEYS[1] and the first argument 'myarg' as ARGV[1]. The script executes and returns both values. The variable tracker shows how KEYS[1] and ARGV[1] are assigned. Key moments clarify why the number of keys is important and the difference between keys and arguments. The quiz tests understanding of these steps and the effect of changing the number of keys.