0
0
Redisquery~20 mins

EVAL command for Lua execution in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lua EVAL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1:30remaining
Output of a simple Lua script with EVAL
What is the output of this Redis EVAL command?

EVAL "return ARGV[1] .. ARGV[2]" 0 Hello World
Redis
EVAL "return ARGV[1] .. ARGV[2]" 0 Hello World
A"Hello World"
B"ARGV[1]ARGV[2]"
CError: wrong number of arguments
D"HelloWorld"
Attempts:
2 left
💡 Hint
Remember that ARGV is a list of arguments passed after the key count, and concatenation in Lua uses .. operator.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in Lua script for EVAL
Which option contains a syntax error when used as the Lua script in Redis EVAL?
Areturn redis.call('incr', KEYS[1])
Breturn redis.call('get', KEYS[1])
Creturn redis.call('set', KEYS[1], ARGV[1])
Dreturn redis.call('del', KEYS[1])
Attempts:
2 left
💡 Hint
Check for missing or mismatched parentheses in the Lua code.
optimization
advanced
2:00remaining
Optimizing Lua script to increment multiple keys
You want to increment the values of multiple keys atomically using EVAL. Which Lua script is the most efficient?
Afor i=1,#KEYS do redis.call('incr', KEYS[i]) end return 'OK'
Bredis.call('incr', KEYS[1]) redis.call('incr', KEYS[2]) return 'OK'
Creturn redis.call('incr', KEYS[1]) + redis.call('incr', KEYS[2])
Dfor i=1,#ARGV do redis.call('incr', ARGV[i]) end return 'OK'
Attempts:
2 left
💡 Hint
Use KEYS array to loop over keys, not ARGV.
🧠 Conceptual
advanced
1:30remaining
Understanding KEYS and ARGV in EVAL
In Redis EVAL command, what is the main difference between KEYS and ARGV?
AKEYS are only used for read operations; ARGV only for write operations.
BKEYS are the keys the script will access; ARGV are additional arguments passed to the script.
CKEYS are arguments passed after ARGV; ARGV are keys to access.
DKEYS and ARGV are interchangeable and mean the same.
Attempts:
2 left
💡 Hint
Think about how Redis separates keys from other parameters for scripting.
🔧 Debug
expert
1:30remaining
Debugging a Lua script that returns nil unexpectedly
Given this EVAL command:

EVAL "return redis.call('get', KEYS[1])" 1 mykey

If the key 'mykey' does not exist, what will the script return?
Redis
EVAL "return redis.call('get', KEYS[1])" 1 mykey
A"" (empty string)
BError: key does not exist
Cnil
D0
Attempts:
2 left
💡 Hint
Consider what Redis returns when getting a non-existent key.