Bird
Raised Fist0
Redisquery~20 mins

Transactions vs Lua scripts in Redis - Practice Questions

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between Redis transactions and Lua scripts?
easy
A. Transactions run commands one by one, Lua scripts run commands outside Redis.
B. Transactions group commands to run atomically, Lua scripts run complex logic atomically inside Redis.
C. Transactions can only read data, Lua scripts can only write data.
D. Transactions are faster than Lua scripts in all cases.

Solution

  1. Step 1: Understand Redis transactions

    Transactions group multiple commands to run atomically, meaning all commands succeed or none do.
  2. Step 2: Understand Lua scripts in Redis

    Lua scripts run inside Redis and can include complex logic, running atomically as a single operation.
  3. Final Answer:

    Transactions group commands to run atomically, Lua scripts run complex logic atomically inside Redis. -> Option B
  4. Quick Check:

    Transactions vs Lua scripts = Grouped commands vs complex logic [OK]
Hint: Transactions group commands; Lua scripts add logic inside Redis [OK]
Common Mistakes:
  • Thinking Lua scripts run outside Redis
  • Believing transactions can only read or write
  • Assuming transactions are always faster
2. Which of the following is the correct way to start a Redis transaction?
easy
A. EXEC
B. START TRANSACTION
C. BEGIN
D. MULTI

Solution

  1. Step 1: Recall Redis transaction commands

    Redis uses the command MULTI to start a transaction block.
  2. Step 2: Identify the correct command

    START TRANSACTION and BEGIN are SQL commands, not Redis. EXEC ends the transaction.
  3. Final Answer:

    MULTI -> Option D
  4. Quick Check:

    Start Redis transaction = MULTI [OK]
Hint: Use MULTI to start Redis transactions, EXEC to finish [OK]
Common Mistakes:
  • Using SQL commands like START TRANSACTION
  • Confusing EXEC as start command
  • Using BEGIN which is not Redis syntax
3. Given this Lua script in Redis:
local current = redis.call('GET', KEYS[1])
if not current then
  redis.call('SET', KEYS[1], ARGV[1])
  return ARGV[1]
else
  return current
end
What will be the output if the key does not exist and ARGV[1] is "100"?
medium
A. Error
B. nil
C. "100"
D. "0"

Solution

  1. Step 1: Analyze the Lua script logic

    The script checks if the key exists. If not, it sets the key to ARGV[1] and returns ARGV[1].
  2. Step 2: Apply the input values

    Since the key does not exist, it sets the key to "100" and returns "100".
  3. Final Answer:

    "100" -> Option C
  4. Quick Check:

    Key missing sets and returns ARGV[1] = "100" [OK]
Hint: If key missing, Lua sets and returns ARGV[1] [OK]
Common Mistakes:
  • Assuming nil is returned when key missing
  • Expecting an error from redis.call
  • Confusing return values with zero
4. You wrote this Redis transaction:
MULTI
SET key1 foo
INCR key1
EXEC
But the INCR command fails with an error. What is the likely cause?
medium
A. SET sets key1 as a string, INCR expects an integer value
B. INCR cannot be used inside MULTI blocks
C. EXEC was missing to run the transaction
D. MULTI command syntax is incorrect

Solution

  1. Step 1: Understand SET and INCR behavior

    SET stores the value as a string. INCR expects the key to hold an integer value.
  2. Step 2: Identify the error cause

    Since SET sets "foo" as a string, INCR fails because it cannot increment a non-integer string.
  3. Final Answer:

    SET sets key1 as a string, INCR expects an integer value -> Option A
  4. Quick Check:

    INCR needs integer, SET stores string [OK]
Hint: INCR needs integer; SET stores string by default [OK]
Common Mistakes:
  • Thinking INCR can't be in MULTI
  • Forgetting to EXEC the transaction
  • Assuming MULTI syntax is wrong
5. You want to atomically check if a key exists and increment it only if it does, otherwise set it to 1. Which approach is best in Redis?
hard
A. Use a Lua script that checks and updates the key atomically
B. Use Redis pipeline to send GET and INCR commands together
C. Run separate GET and INCR commands without transactions
D. Use a Redis transaction with WATCH, GET, and MULTI commands

Solution

  1. Step 1: Understand the atomicity requirement

    The operation must be atomic: check existence and update in one step without race conditions.
  2. Step 2: Compare approaches

    Transactions with WATCH can be complex and may fail due to race conditions. Separate commands or pipelines are not atomic. Lua scripts run atomically inside Redis and can handle logic safely.
  3. Final Answer:

    Use a Lua script that checks and updates the key atomically -> Option A
  4. Quick Check:

    Atomic check and update = Lua script [OK]
Hint: Use Lua scripts for atomic logic beyond simple transactions [OK]
Common Mistakes:
  • Assuming WATCH transactions are always safe
  • Using separate commands risking race conditions
  • Confusing pipelines with atomic execution
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard