EVAL command for Lua execution in Redis - Time & Space Complexity
When running Lua scripts with Redis's EVAL command, it's important to know how the time to run the script changes as the script or data grows.
We want to understand how the execution time scales when the script processes more keys or data.
Analyze the time complexity of this Redis Lua script run with EVAL.
EVAL "
local sum = 0
for i, key in ipairs(KEYS) do
local val = redis.call('GET', key)
if val then
sum = sum + tonumber(val)
end
end
return sum
" 3 key1 key2 key3
This script sums the numeric values of the given keys by fetching each key's value one by one.
Look for repeated actions inside the script.
- Primary operation: Looping over each key in the KEYS list.
- How many times: Once for each key provided to the script.
The script does one GET call per key, so the work grows as the number of keys grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 GET calls + 10 additions |
| 100 | 100 GET calls + 100 additions |
| 1000 | 1000 GET calls + 1000 additions |
Pattern observation: The number of operations grows directly with the number of keys.
Time Complexity: O(n)
This means the time to run the script grows linearly with the number of keys it processes.
[X] Wrong: "The script runs in constant time no matter how many keys it processes."
[OK] Correct: Each key requires a separate GET call, so more keys mean more work and longer execution time.
Understanding how Lua scripts run inside Redis helps you write efficient scripts and explain their performance clearly in real-world situations.
"What if the script used redis.call('MGET', unpack(KEYS)) instead of looping over keys? How would the time complexity change?"
Practice
EVAL command in Redis?Solution
Step 1: Understand Redis scripting
Redis uses Lua scripts to perform complex or multiple operations atomically.Step 2: Identify EVAL command role
TheEVALcommand runs Lua scripts inside Redis, ensuring atomicity and reducing network calls.Final Answer:
To run Lua scripts inside Redis for atomic operations -> Option AQuick Check:
EVAL runs Lua scripts = D [OK]
- Confusing EVAL with SQL execution
- Thinking EVAL backs up data
- Assuming EVAL creates databases
EVAL in Redis?Solution
Step 1: Recall EVAL syntax
The correct syntax isEVAL script numkeys key [key ...] arg [arg ...], wherenumkeystells how many keys follow.Step 2: Match options
Only EVAL script numkeys key [key ...] arg [arg ...] matches this exact order and structure.Final Answer:
EVAL script numkeys key [key ...] arg [arg ...] -> Option AQuick Check:
Script, numkeys, keys, args order = A [OK]
- Placing numkeys after keys or args
- Mixing order of keys and args
- Omitting numkeys parameter
return redis.call('GET', KEYS[1]) and the command EVAL script 1 mykey, what will Redis return if mykey holds the value 'hello'?Solution
Step 1: Understand the Lua script
The script callsredis.call('GET', KEYS[1]), which fetches the value of the first key passed.Step 2: Analyze the command
The command passes 1 key:mykey. Ifmykeyholds 'hello', the script returns 'hello'.Final Answer:
'hello' -> Option CQuick Check:
GET mykey = 'hello' [OK]
- Confusing keys and args
- Expecting key name instead of value
- Assuming nil if key exists
EVAL 'return redis.call('GET', KEYS[1])' 1 mykeySolution
Step 1: Inspect Lua script quoting
The Lua script is enclosed in single quotes, but inside it also uses single quotes for'GET', causing quote conflict.Step 2: Identify syntax error
This causes a syntax error because the shell or Redis cannot parse the string properly.Final Answer:
The Lua script string uses conflicting single quotes causing syntax error -> Option BQuick Check:
Quote conflict in script string = A [OK]
- Ignoring quote conflicts
- Forgetting to escape inner quotes
- Miscounting number of keys
counter by a value passed as an argument using EVAL. Which Lua script and command correctly achieve this?Solution
Step 1: Understand INCRBY usage in Lua script
The commandredis.call('INCRBY', KEYS[1], ARGV[1])increments the key by the argument value.Step 2: Check command arguments
Passing1as number of keys, thencounteras key, and5as argument matches the script.Final Answer:
Script: return redis.call('INCRBY', KEYS[1], ARGV[1]) and Command: EVAL script 1 counter 5 -> Option DQuick Check:
INCRBY key arg with correct keys count = B [OK]
- Swapping keys and args order
- Incorrect number of keys argument
- Using zero keys when keys are needed
Ready to prove it?
Pass the test and this pattern moves unknown
