0
0
Redisquery~20 mins

Transactions vs Lua scripts in Redis - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis Transactions and Lua Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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 key1 after the transaction completes?
Redis
MULTI
SET key1 100
INCR key1
EXEC
AError: key1 is not an integer
B1
C100
D101
Attempts:
2 left
💡 Hint
Remember that Redis transactions queue commands and execute them atomically.
query_result
intermediate
2: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 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
Anil
BError: key does not exist
C"hello"
Dfalse
Attempts:
2 left
💡 Hint
Lua scripts in Redis return strings or nil, and redis.call returns nil if key does not exist.
🧠 Conceptual
advanced
2: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?
ALua scripts cannot access Redis keys, so they are safer.
BLua scripts guarantee atomic execution and can perform complex logic in one call.
CTransactions are faster because they do not support atomicity.
DTransactions allow rollback on error, Lua scripts do not.
Attempts:
2 left
💡 Hint
Think about atomicity and complexity of operations.
🔧 Debug
advanced
2: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 key1, not the incremented one. Why?
ACommands in a transaction are queued and executed only after EXEC, so GET sees the old value.
BGET command is invalid inside MULTI block.
CINCR does not change the value immediately, so GET returns nil.
DEXEC discards all commands except the last one.
Attempts:
2 left
💡 Hint
Think about when commands inside MULTI are executed.
optimization
expert
2: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.
ALua script is more efficient because it runs atomically on the server without retries.
BNeither is efficient; use client-side locking instead.
CBoth are equally efficient because Redis handles atomicity internally.
DTransaction with WATCH is more efficient because it retries automatically.
Attempts:
2 left
💡 Hint
Consider network round trips and atomicity guarantees.