0
0
Redisquery~20 mins

Why Lua scripts enable atomicity in Redis - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lua Atomicity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why are Lua scripts atomic in Redis?
In Redis, Lua scripts run atomically. What does this mean for commands inside a Lua script?
ACommands inside the script run one by one but can be interrupted by other clients.
BAll commands inside the script run as a single, uninterrupted operation.
CEach command inside the script runs in a separate transaction.
DLua scripts run asynchronously and may overlap with other commands.
Attempts:
2 left
💡 Hint
Think about what atomic means in the context of operations.
query_result
intermediate
2:00remaining
Output of a Lua script modifying a key atomically
Given this Lua script run in Redis:
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?
A"1"
B"0"
C"3"
Dnil
Attempts:
2 left
💡 Hint
The script increments the counter three times starting from zero.
📝 Syntax
advanced
2: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')
Afor i = 1, 5 do redis.call('INCR', 'score') end return redis.call('GET', 'score')
B)'erocs' ,'TEG'(llac.sider nruter dne )'erocs' ,'RCNI'(llac.sider od 5 ,1 = i rof
Cor i = 1, 5 do redis.call('INCR', 'score') end return redis.call('GET', 'score')
Dfor i = 1, 5 redis.call('INCR', 'score') end return redis.call('GET', 'score')
Attempts:
2 left
💡 Hint
Check the for loop syntax carefully.
optimization
advanced
2: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?
A
local a = redis.call('INCR', 'a')
local b = redis.call('INCR', 'b')
return {a, b}
B
redis.call('INCR', 'a')
return redis.call('INCR', 'b')
C
redis.call('INCR', 'a')
redis.call('INCR', 'b')
return {redis.call('GET', 'a'), redis.call('GET', 'b')}
D
redis.call('INCRBY', 'a', 1)
redis.call('INCRBY', 'b', 1)
return {redis.call('GET', 'a'), redis.call('GET', 'b')}
Attempts:
2 left
💡 Hint
Consider minimizing calls and returning values directly.
🔧 Debug
expert
2:00remaining
Why does this Lua script cause a runtime error in Redis?
Consider this Lua script:
local val = redis.call('GET', 'missing_key')
return val + 1

What error will Redis raise when running this script and why?
AAttempt to perform arithmetic on a nil value because 'missing_key' does not exist.
BSyntax error due to missing parentheses in redis.call.
CRedis returns nil without error because missing keys are allowed.
DType error because redis.call returns a table, not a string.
Attempts:
2 left
💡 Hint
Think about what happens when you try to add a number to nil in Lua.