Bird
Raised Fist0
Redisquery~20 mins

Lua vs transactions comparison in Redis - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Lua vs Transactions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Atomicity in Redis: Lua Scripts vs Transactions

Which statement best describes the atomicity guarantee when using Lua scripts compared to Redis transactions?

ALua scripts and transactions both guarantee atomicity only if the MULTI command is used inside the script.
BTransactions in Redis are fully atomic, but Lua scripts can be interrupted by other commands causing partial execution.
CBoth Lua scripts and transactions execute commands one by one without atomic guarantees.
DLua scripts execute atomically, ensuring all commands run as a single unit without interruption, while transactions may allow other commands to interleave between MULTI and EXEC.
Attempts:
2 left
💡 Hint

Think about how Redis handles command execution inside Lua scripts versus MULTI/EXEC blocks.

❓ query_result
intermediate
2:00remaining
Result of Concurrent Writes: Lua Script vs Transaction

Given two clients concurrently incrementing the same Redis key, which approach prevents race conditions?

Client 1 uses a Lua script to increment the key. Client 2 uses MULTI/EXEC transaction commands to increment the key.

What is the expected behavior?

ANeither Lua scripts nor transactions prevent race conditions; external locking is required.
BThe Lua script ensures increments are atomic and race conditions are prevented; the transaction may allow race conditions if commands overlap.
CBoth Lua scripts and transactions prevent race conditions equally by locking the key during execution.
DTransactions prevent race conditions by queuing commands, but Lua scripts do not provide any concurrency control.
Attempts:
2 left
💡 Hint

Consider how Redis executes Lua scripts and transactions in the context of concurrent clients.

📝 Syntax
advanced
2:00remaining
Identify the Correct Lua Script Syntax for Atomic Increment

Which Lua script correctly increments a Redis key atomically?

Redis
local current = redis.call('GET', KEYS[1])
local new = tonumber(current) + 1
redis.call('SET', KEYS[1], new)
return new
A
local current = redis.call('GET', KEYS[1])
local new = tonumber(current) + 1
redis.call('SET', KEYS[1], tostring(new))
return new
B
local current = redis.call('GET', KEYS[1])
local new = current + 1
redis.call('SET', KEYS[1], new)
return new
C
local current = redis.call('GET', KEYS[1])
local new = tonumber(current) + 1
redis.call('SET', KEYS[1], new)
return new
D
local current = redis.call('GET', KEYS[1])
local new = tonumber(current) + 1
redis.call('SET', KEYS[1], new)
return tostring(new)
Attempts:
2 left
💡 Hint

Remember Redis stores strings, so the value set must be a string.

❓ optimization
advanced
2:00remaining
Optimizing Multiple Commands: Lua Script vs Transaction

You want to execute multiple Redis commands atomically and efficiently. Which approach is generally faster and why?

AUsing a transaction with MULTI/EXEC is faster because it pipelines commands and executes them in batch.
BBoth approaches have the same performance because Redis processes commands sequentially regardless of method.
CUsing a Lua script is faster because it runs all commands server-side in one call, reducing network overhead and ensuring atomicity.
DUsing multiple separate commands without transactions or scripts is faster due to parallel execution.
Attempts:
2 left
💡 Hint

Think about network round-trips and server-side execution.

🔧 Debug
expert
2:00remaining
Debugging a Redis Transaction Failure

A Redis transaction using MULTI/EXEC fails silently and does not apply any commands. Which of the following is the most likely cause?

AOne of the commands inside the transaction is invalid or causes an error, causing EXEC to abort all commands.
BThe transaction was not started with the MULTI command, so EXEC has no commands to execute.
CThe client did not wait for the EXEC reply, so the commands were executed but the client missed the response.
DRedis transactions do not support atomic execution, so commands are applied partially and silently fail.
Attempts:
2 left
💡 Hint

Consider how Redis handles errors inside transactions.

Practice

(1/5)
1. Which statement best describes the difference between Lua scripts and transactions in Redis?
easy
A. Transactions are faster and more flexible than Lua scripts.
B. Transactions can execute complex logic, Lua scripts only group commands.
C. Lua scripts allow complex logic, while transactions group simple commands atomically.
D. Both Lua scripts and transactions cannot guarantee atomic execution.

Solution

  1. Step 1: Understand atomic execution in Redis

    Both Lua scripts and transactions ensure commands run atomically, meaning all commands execute as one unit without interruption.
  2. Step 2: Compare complexity and usage

    Lua scripts allow embedding complex logic and multiple commands in one script, while transactions simply group commands without logic.
  3. Final Answer:

    Lua scripts allow complex logic, while transactions group simple commands atomically. -> Option C
  4. Quick Check:

    Lua = complex logic, transactions = simple groups [OK]
Hint: Lua = complex logic, transactions = simple command groups [OK]
Common Mistakes:
  • Thinking transactions can run complex logic
  • Believing Lua scripts do not guarantee atomicity
  • Confusing speed differences between Lua and transactions
2. Which of the following is the correct way to start a transaction in Redis?
easy
A. MULTI
B. EVAL
C. EXEC
D. SCRIPT

Solution

  1. Step 1: Recall Redis transaction commands

    Redis transactions start with the MULTI command to begin queuing commands.
  2. Step 2: Identify correct command usage

    EXEC is used to execute queued commands, EVAL runs Lua scripts, SCRIPT manages scripts, so MULTI is the correct start.
  3. Final Answer:

    MULTI -> Option A
  4. Quick Check:

    Start transaction with MULTI [OK]
Hint: Start transactions with MULTI, execute with EXEC [OK]
Common Mistakes:
  • Using EXEC to start a transaction
  • Confusing EVAL with transaction commands
  • Using SCRIPT to start transactions
3. Given the following Lua script executed in 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
What will be the output if the key does not exist and ARGV[1] is "hello"?
medium
A. Error
B. nil
C. false
D. "hello"

Solution

  1. Step 1: Check key existence with redis.call('GET')

    If the key does not exist, redis.call('GET') returns nil in Lua script context.
  2. Step 2: Set key and return ARGV[1]

    Since val == nil, the script sets the key to ARGV[1] which is "hello" and returns "hello".
  3. Final Answer:

    "hello" -> Option D
  4. Quick Check:

    Key missing sets and returns ARGV[1] = "hello" [OK]
Hint: If key missing, Lua script sets and returns ARGV[1] [OK]
Common Mistakes:
  • Thinking GET returns false instead of nil
  • Expecting nil or error instead of returned value
  • Confusing Lua false with Redis nil
4. You wrote this Redis transaction:
MULTI
GET key1
SET key1 value1
EXEC
But the GET command returns nil instead of "value1". What is the likely cause?
medium
A. GET command syntax is incorrect
B. GET runs before SET executes in transaction
C. EXEC was not called to execute commands
D. Redis transactions do not guarantee atomicity

Solution

  1. Step 1: Understand Redis transaction command queuing

    Commands inside MULTI are queued and executed only after EXEC; GET does not see SET effect during queuing.
  2. Step 2: Explain why GET returns nil

    GET is queued before SET, so during EXEC it executes first and returns nil because SET has not been applied yet.
  3. Final Answer:

    GET runs before SET executes in transaction -> Option B
  4. Quick Check:

    Queued commands run after EXEC, GET sees old value [OK]
Hint: Queued commands run after EXEC; GET sees old value during queue [OK]
Common Mistakes:
  • Thinking commands run immediately inside MULTI
  • Assuming GET syntax error causes nil
  • Believing EXEC was not called
5. You want to atomically increment a counter only if its current value is less than 10. Which approach is best in Redis?
hard
A. Use a Lua script to check and increment atomically
B. Use MULTI/EXEC transaction with GET and INCR commands
C. Run GET and INCR commands separately in client code
D. Use WATCH on the key and then INCR without Lua

Solution

  1. Step 1: Understand atomic conditional increment need

    Increment only if value < 10 requires checking and updating atomically to avoid race conditions.
  2. Step 2: Compare Lua script vs transactions

    Lua scripts run all logic atomically in Redis, while transactions cannot conditionally check and increment atomically without race.
  3. Step 3: Evaluate WATCH option

    WATCH can detect changes but requires retry logic and is less clean than Lua script for this logic.
  4. Final Answer:

    Use a Lua script to check and increment atomically -> Option A
  5. Quick Check:

    Lua scripts handle complex atomic logic best [OK]
Hint: Use Lua for atomic conditional logic, transactions can't do it alone [OK]
Common Mistakes:
  • Assuming transactions can do conditional increments atomically
  • Ignoring race conditions with separate GET and INCR
  • Overestimating WATCH simplicity without retries
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