Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In a Redis Lua script, which array is used to access the keys passed to the script?
easy
A. KEYS
B. ARGV
C. KEYSARGS
D. ARGS

Solution

  1. Step 1: Understand Redis script inputs

    Redis Lua scripts receive keys and arguments separately.
  2. Step 2: Identify the array for keys

    The keys are accessed using the KEYS array inside the script.
  3. Final Answer:

    KEYS -> Option A
  4. Quick Check:

    Keys = KEYS [OK]
Hint: Keys are always in KEYS array, arguments in ARGV [OK]
Common Mistakes:
  • Confusing KEYS with ARGV
  • Using ARGS instead of ARGV
  • Assuming keys are in ARGV
2. Which of the following is the correct way to access the first argument (not key) inside a Redis Lua script?
easy
A. KEYS[1]
B. ARGV[0]
C. ARGV[1]
D. ARGS[1]

Solution

  1. Step 1: Recall Lua array indexing

    Lua arrays start at 1, not 0.
  2. Step 2: Identify argument array

    Arguments are accessed via ARGV array, so first argument is ARGV[1].
  3. Final Answer:

    ARGV[1] -> Option C
  4. Quick Check:

    First argument = ARGV[1] [OK]
Hint: Lua arrays start at 1, so first argument is ARGV[1] [OK]
Common Mistakes:
  • Using zero-based index ARGV[0]
  • Confusing KEYS with ARGV
  • Using ARGS instead of ARGV
3. Consider this Redis Lua script snippet:
local key1 = KEYS[1]
local arg1 = ARGV[1]
return key1 .. '-' .. arg1
If the script is called with keys = ["user:1"] and arguments = ["active"], what is the returned result?
medium
A. "user:1-active"
B. "active-user:1"
C. "user:1active"
D. An error occurs

Solution

  1. Step 1: Assign values from KEYS and ARGV

    key1 = "user:1", arg1 = "active" from the inputs.
  2. Step 2: Concatenate with separator

    Return value is key1 .. '-' .. arg1 = "user:1-active".
  3. Final Answer:

    "user:1-active" -> Option A
  4. Quick Check:

    Concatenate KEYS[1] and ARGV[1] with '-' = "user:1-active" [OK]
Hint: Concatenate KEYS and ARGV with .. operator in Lua [OK]
Common Mistakes:
  • Swapping KEYS and ARGV values
  • Missing separator '-' in concatenation
  • Using zero-based indexing
4. You wrote this Redis Lua script:
local key = KEYS[1]
local arg = ARGV[0]
return key .. arg
When running the script with one key and one argument, what will happen?
medium
A. It throws an error due to invalid index
B. It returns nil concatenated with key
C. It returns only the key
D. It returns concatenated key and argument

Solution

  1. Step 1: Check Lua indexing rules

    Lua arrays start at 1, so ARGV[0] is invalid but returns nil.
  2. Step 2: Understand behavior on invalid index

    Accessing ARGV[0] returns nil, concatenating key .. nil results in key .. "nil" or error depending on Lua version.
  3. Final Answer:

    It returns nil concatenated with key -> Option B
  4. Quick Check:

    Lua arrays start at 1, ARGV[0] returns nil, concatenation with nil results in string with 'nil' [OK]
Hint: Lua arrays start at 1, never use index 0 [OK]
Common Mistakes:
  • Using zero-based index ARGV[0]
  • Assuming ARGV[0] returns first argument
  • Ignoring Lua indexing rules
5. You want to write a Redis Lua script that takes two keys and one argument. The script should return the value of the first key concatenated with the argument, then the value of the second key. Which is the correct way to access these inside the script?
hard
A. ARGV[1], KEYS[1], KEYS[2]
B. ARGV[0], KEYS[1], ARGV[1]
C. KEYS[0], ARGV[0], KEYS[1]
D. KEYS[1], ARGV[1], KEYS[2]

Solution

  1. Step 1: Identify keys and argument positions

    Two keys are KEYS[1] and KEYS[2], one argument is ARGV[1].
  2. Step 2: Access in correct order

    Access first key as KEYS[1], argument as ARGV[1], second key as KEYS[2].
  3. Final Answer:

    KEYS[1], ARGV[1], KEYS[2] -> Option D
  4. Quick Check:

    Keys in KEYS[], args in ARGV[], Lua index starts at 1 [OK]
Hint: Keys in KEYS[], args in ARGV[], index from 1 [OK]
Common Mistakes:
  • Using zero-based indexes
  • Mixing KEYS and ARGV order
  • Using ARGV for keys
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard