Challenge - 5 Problems
Lua Atomicity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why are Lua scripts atomic in Redis?
In Redis, Lua scripts run atomically. What does this mean for commands inside a Lua script?
Attempts:
2 left
💡 Hint
Think about what atomic means in the context of operations.
✗ Incorrect
Atomic means the entire script runs without interruption, so no other commands can run in the middle.
❓ query_result
intermediate2:00remaining
Output of a Lua script modifying a key atomically
Given this Lua script run in Redis:
What is the output after running this script?
redis.call('SET', 'counter', 0)
for i = 1, 3 do
redis.call('INCR', 'counter')
end
return redis.call('GET', 'counter')What is the output after running this script?
Attempts:
2 left
💡 Hint
The script increments the counter three times starting from zero.
✗ Incorrect
The script sets 'counter' to 0, then increments it 3 times, so the final value is 3.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this Lua script for Redis
Which option contains a syntax error that prevents the Lua script from running atomically in Redis?
for i = 1, 5 do
redis.call('INCR', 'score')
end
return redis.call('GET', 'score')Attempts:
2 left
💡 Hint
Check the for loop syntax carefully.
✗ Incorrect
Option D is missing the 'do' keyword after the for loop declaration, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing Lua script atomic operations in Redis
Which Lua script is the most efficient way to atomically increment two keys 'a' and 'b' by 1 in Redis?
Attempts:
2 left
💡 Hint
Consider minimizing calls and returning values directly.
✗ Incorrect
Option A increments both keys and stores the results locally, then returns them, reducing extra calls.
🔧 Debug
expert2:00remaining
Why does this Lua script cause a runtime error in Redis?
Consider this Lua script:
What error will Redis raise when running this script and why?
local val = redis.call('GET', 'missing_key')
return val + 1What error will Redis raise when running this script and why?
Attempts:
2 left
💡 Hint
Think about what happens when you try to add a number to nil in Lua.
✗ Incorrect
When 'missing_key' does not exist, redis.call returns nil. Adding 1 to nil causes a runtime error.