What if your inventory updates could never get mixed up, no matter how many customers buy at once?
Transactions vs Lua scripts in Redis - When to Use Which
Imagine you are managing a busy online store's inventory. You try to update stock counts manually by sending multiple separate commands to the database for each item sold.
Sometimes, two updates happen at the same time, causing confusion and wrong stock numbers.
Manually sending separate commands can cause errors because the database might process them out of order or interrupt one update with another.
This leads to inconsistent data, lost updates, and frustrated customers.
Using transactions or Lua scripts lets you bundle multiple commands into one atomic operation.
This means all commands run together without interruption, keeping your data accurate and safe.
SET stock 10 INCRBY stock -1 GET stock
MULTI DECR stock EXEC -- or -- EVAL 'redis.call("DECR", KEYS[1])' 1 stock
It enables safe, reliable updates to your data even when many users act at the same time.
When many customers buy the same product simultaneously, transactions or Lua scripts ensure the stock count never goes below zero and reflects true availability.
Manual separate commands risk data errors under concurrent use.
Transactions group commands to run safely together.
Lua scripts allow custom logic to run atomically inside the database.
Practice
Solution
Step 1: Understand Redis transactions
Transactions group multiple commands to run atomically, meaning all commands succeed or none do.Step 2: Understand Lua scripts in Redis
Lua scripts run inside Redis and can include complex logic, running atomically as a single operation.Final Answer:
Transactions group commands to run atomically, Lua scripts run complex logic atomically inside Redis. -> Option BQuick Check:
Transactions vs Lua scripts = Grouped commands vs complex logic [OK]
- Thinking Lua scripts run outside Redis
- Believing transactions can only read or write
- Assuming transactions are always faster
Solution
Step 1: Recall Redis transaction commands
Redis uses the command MULTI to start a transaction block.Step 2: Identify the correct command
START TRANSACTION and BEGIN are SQL commands, not Redis. EXEC ends the transaction.Final Answer:
MULTI -> Option DQuick Check:
Start Redis transaction = MULTI [OK]
- Using SQL commands like START TRANSACTION
- Confusing EXEC as start command
- Using BEGIN which is not Redis syntax
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"?Solution
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].Step 2: Apply the input values
Since the key does not exist, it sets the key to "100" and returns "100".Final Answer:
"100" -> Option CQuick Check:
Key missing sets and returns ARGV[1] = "100" [OK]
- Assuming nil is returned when key missing
- Expecting an error from redis.call
- Confusing return values with zero
MULTI SET key1 foo INCR key1 EXECBut the INCR command fails with an error. What is the likely cause?
Solution
Step 1: Understand SET and INCR behavior
SET stores the value as a string. INCR expects the key to hold an integer value.Step 2: Identify the error cause
Since SET sets "foo" as a string, INCR fails because it cannot increment a non-integer string.Final Answer:
SET sets key1 as a string, INCR expects an integer value -> Option AQuick Check:
INCR needs integer, SET stores string [OK]
- Thinking INCR can't be in MULTI
- Forgetting to EXEC the transaction
- Assuming MULTI syntax is wrong
Solution
Step 1: Understand the atomicity requirement
The operation must be atomic: check existence and update in one step without race conditions.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.Final Answer:
Use a Lua script that checks and updates the key atomically -> Option AQuick Check:
Atomic check and update = Lua script [OK]
- Assuming WATCH transactions are always safe
- Using separate commands risking race conditions
- Confusing pipelines with atomic execution
Ready to prove it?
Pass the test and this pattern moves unknown
