Hint: Remember: script, numkeys, keys, then args in EVAL [OK]
Common Mistakes:
Placing numkeys after keys or args
Mixing order of keys and args
Omitting numkeys parameter
3. Given the Lua script return redis.call('GET', KEYS[1]) and the command EVAL script 1 mykey, what will Redis return if mykey holds the value 'hello'?
medium
A. An error because arguments are missing
B. nil
C. 'hello'
D. The string 'mykey'
Solution
Step 1: Understand the Lua script
The script calls redis.call('GET', KEYS[1]), which fetches the value of the first key passed.
Step 2: Analyze the command
The command passes 1 key: mykey. If mykey holds 'hello', the script returns 'hello'.
Final Answer:
'hello' -> Option C
Quick Check:
GET mykey = 'hello' [OK]
Hint: Lua script returns value of first key passed [OK]
Common Mistakes:
Confusing keys and args
Expecting key name instead of value
Assuming nil if key exists
4. What is wrong with this Redis command? EVAL 'return redis.call('GET', KEYS[1])' 1 mykey
medium
A. Missing number of keys argument
B. The Lua script string uses conflicting single quotes causing syntax error
C. Keys and args are swapped
D. The command is correct and will run successfully
Solution
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 B
Quick Check:
Quote conflict in script string = A [OK]
Hint: Use different quotes or escape inner quotes in Lua script [OK]
Common Mistakes:
Ignoring quote conflicts
Forgetting to escape inner quotes
Miscounting number of keys
5. You want to atomically increment a Redis key counter by a value passed as an argument using EVAL. Which Lua script and command correctly achieve this?