Accessing keys and arguments in Redis - Time & Space Complexity
When working with Redis commands, it is important to understand how the time to access keys and arguments changes as the number of keys or arguments grows.
We want to know how the cost of accessing these keys and arguments grows when we have more of them.
Analyze the time complexity of the following Redis Lua script snippet.
-- Access keys and arguments in a Redis Lua script
local key1 = KEYS[1]
local key2 = KEYS[2]
local arg1 = ARGV[1]
local arg2 = ARGV[2]
redis.call('SET', key1, arg1)
redis.call('SET', key2, arg2)
This script accesses two keys and two arguments, then sets values for those keys using the arguments.
Look for repeated actions that take time as input grows.
- Primary operation: Accessing each key and argument once.
- How many times: Once per key and once per argument; no loops here.
As the number of keys and arguments increases, the number of accesses grows directly with them.
| Input Size (n keys + n args) | Approx. Operations |
|---|---|
| 10 | About 10 key accesses + 10 argument accesses = 20 operations |
| 100 | About 100 key accesses + 100 argument accesses = 200 operations |
| 1000 | About 1000 key accesses + 1000 argument accesses = 2000 operations |
Pattern observation: The work grows in a straight line as the number of keys and arguments increases.
Time Complexity: O(n)
This means the time to access keys and arguments grows directly in proportion to how many there are.
[X] Wrong: "Accessing keys and arguments is always constant time, no matter how many there are."
[OK] Correct: Each key and argument must be accessed individually, so more keys or arguments mean more work.
Understanding how accessing keys and arguments scales helps you write efficient Redis scripts and shows you can think about performance clearly.
"What if the script used a loop to access keys instead of accessing them one by one? How would the time complexity change?"
Practice
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 AQuick Check:
Keys = KEYS [OK]
- Confusing KEYS with ARGV
- Using ARGS instead of ARGV
- Assuming keys are in ARGV
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 CQuick Check:
First argument = ARGV[1] [OK]
- Using zero-based index ARGV[0]
- Confusing KEYS with ARGV
- Using ARGS instead of ARGV
local key1 = KEYS[1] local arg1 = ARGV[1] return key1 .. '-' .. arg1If the script is called with keys = ["user:1"] and arguments = ["active"], what is the returned result?
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 AQuick Check:
Concatenate KEYS[1] and ARGV[1] with '-' = "user:1-active" [OK]
- Swapping KEYS and ARGV values
- Missing separator '-' in concatenation
- Using zero-based indexing
local key = KEYS[1] local arg = ARGV[0] return key .. argWhen running the script with one key and one argument, what will happen?
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 BQuick Check:
Lua arrays start at 1, ARGV[0] returns nil, concatenation with nil results in string with 'nil' [OK]
- Using zero-based index ARGV[0]
- Assuming ARGV[0] returns first argument
- Ignoring Lua indexing rules
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 DQuick Check:
Keys in KEYS[], args in ARGV[], Lua index starts at 1 [OK]
- Using zero-based indexes
- Mixing KEYS and ARGV order
- Using ARGV for keys
Ready to prove it?
Pass the test and this pattern moves unknown
