What is the purpose of KEYS and ARGV in a Redis Lua script?
KEYS holds the list of keys passed to the script, while ARGV holds the list of additional arguments. They let the script access keys and parameters separately.
Click to reveal answer
beginner
How do you access the first key inside a Redis Lua script?
Use KEYS[1] to get the first key passed to the script.
Click to reveal answer
beginner
How do you access the second argument inside a Redis Lua script?
Use ARGV[2] to get the second argument passed to the script.
Click to reveal answer
intermediate
Why separate keys and arguments in Redis Lua scripts?
Separating keys and arguments helps Redis know which keys the script will access, improving safety and performance.
Click to reveal answer
intermediate
What happens if you try to access a key index that was not passed to the script?
Accessing a key index that was not passed returns nil, meaning no key exists at that position.
Click to reveal answer
In a Redis Lua script, how do you access the third key passed to the script?
AKEYS[3]
BARGV[3]
CKEYS(3)
DARGV(3)
✗ Incorrect
KEYS is a table containing all keys passed. KEYS[3] accesses the third key.
Which variable holds the additional arguments passed to a Redis Lua script?
AKEYS
BARGV
CARGS
DPARAMS
✗ Incorrect
ARGV holds the list of additional arguments passed to the script.
What will KEYS[1] return if no keys were passed to the script?
AAn empty string
BAn error
Cnil
D0
✗ Incorrect
If no keys are passed, KEYS[1] returns nil, meaning no value.
Why does Redis require keys to be passed separately from arguments in Lua scripts?
ATo allow Redis to track key usage for safety and performance
BTo enable multi-threading
CTo reduce script size
DTo improve script readability
✗ Incorrect
Redis tracks keys separately to ensure scripts only access declared keys, improving safety and performance.
How do you access the first argument (not key) passed to a Redis Lua script?
APARAMS[1]
BKEYS[1]
CARGS[1]
DARGV[1]
✗ Incorrect
ARGV[1] accesses the first argument passed to the script.
Explain how to access keys and arguments inside a Redis Lua script and why they are separated.
Think about how Redis manages keys and extra data separately.
You got /3 concepts.
Describe what happens if you try to access a key or argument index that was not passed to the Redis Lua script.
Consider what Lua returns when accessing missing table elements.
You got /3 concepts.
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
Step 1: Understand Redis script inputs
Redis Lua scripts receive keys and arguments separately.
Step 2: Identify the array for keys
The keys are accessed using the KEYS array inside the script.
Final Answer:
KEYS -> Option A
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
Step 1: Recall Lua array indexing
Lua arrays start at 1, not 0.
Step 2: Identify argument array
Arguments are accessed via ARGV array, so first argument is ARGV[1].
Final Answer:
ARGV[1] -> Option C
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
Step 1: Assign values from KEYS and ARGV
key1 = "user:1", arg1 = "active" from the inputs.
Step 2: Concatenate with separator
Return value is key1 .. '-' .. arg1 = "user:1-active".
Final Answer:
"user:1-active" -> Option A
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
Step 1: Check Lua indexing rules
Lua arrays start at 1, so ARGV[0] is invalid but returns nil.
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.
Final Answer:
It returns nil concatenated with key -> Option B
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
Step 1: Identify keys and argument positions
Two keys are KEYS[1] and KEYS[2], one argument is ARGV[1].
Step 2: Access in correct order
Access first key as KEYS[1], argument as ARGV[1], second key as KEYS[2].
Final Answer:
KEYS[1], ARGV[1], KEYS[2] -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.