Challenge - 5 Problems
Redis Transactions and Lua Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this Redis transaction?
Consider the following Redis transaction commands executed sequentially:
1. MULTI
2. SET key1 100
3. INCR key1
4. EXEC
What will be the value of
1. MULTI
2. SET key1 100
3. INCR key1
4. EXEC
What will be the value of
key1 after the transaction completes?Redis
MULTI
SET key1 100
INCR key1
EXECAttempts:
2 left
💡 Hint
Remember that Redis transactions queue commands and execute them atomically.
✗ Incorrect
The transaction sets key1 to '100' and then increments it by 1, resulting in 101.
❓ query_result
intermediate2:00remaining
What does this Lua script return in Redis?
Given this Lua script run with EVAL:
local val = redis.call('GET', KEYS[1])
if val == nil then
redis.call('SET', KEYS[1], ARGV[1])
return ARGV[1]
else
return val
end
Assuming
local val = redis.call('GET', KEYS[1])
if val == nil then
redis.call('SET', KEYS[1], ARGV[1])
return ARGV[1]
else
return val
end
Assuming
key1 does not exist before running, what is the output of the script when called with ARGV[1] = 'hello'?Redis
local val = redis.call('GET', KEYS[1]) if val == nil then redis.call('SET', KEYS[1], ARGV[1]) return ARGV[1] else return val end
Attempts:
2 left
💡 Hint
Lua scripts in Redis return strings or nil, and redis.call returns nil if key does not exist.
✗ Incorrect
Since key1 does not exist, redis.call('GET', KEYS[1]) returns nil, so the script sets key1 to 'hello' and returns 'hello'.
🧠 Conceptual
advanced2:00remaining
Why might Lua scripts be preferred over Redis transactions?
Which of the following is the best reason to use Lua scripts instead of Redis transactions?
Attempts:
2 left
💡 Hint
Think about atomicity and complexity of operations.
✗ Incorrect
Lua scripts run atomically and can execute complex logic in one call, unlike transactions which queue commands but cannot perform conditional logic inside.
🔧 Debug
advanced2:00remaining
Identify the error in this Redis transaction usage
A developer writes this Redis transaction:
1. MULTI
2. GET key1
3. INCR key1
4. EXEC
But the GET command returns the old value of
1. MULTI
2. GET key1
3. INCR key1
4. EXEC
But the GET command returns the old value of
key1, not the incremented one. Why?Attempts:
2 left
💡 Hint
Think about when commands inside MULTI are executed.
✗ Incorrect
Inside MULTI, commands are queued but not executed until EXEC. So GET returns the old value because it is queued, not executed immediately.
❓ optimization
expert2:00remaining
Optimizing atomic updates: Lua script vs transaction
You want to atomically increment a counter only if it is less than 10. Which approach is more efficient and why?
Options:
1. Use a Redis transaction with WATCH, GET, INCR, and EXEC.
2. Use a Lua script that checks the value and increments if less than 10.
Choose the best option.
Options:
1. Use a Redis transaction with WATCH, GET, INCR, and EXEC.
2. Use a Lua script that checks the value and increments if less than 10.
Choose the best option.
Attempts:
2 left
💡 Hint
Consider network round trips and atomicity guarantees.
✗ Incorrect
Lua scripts run atomically on the server and avoid the overhead of retries and multiple round trips required by WATCH transactions.