📖 Scenario: You are managing a simple Redis database for a small online store. You want to update the stock quantities of two products together to keep the data consistent.
🎯 Goal: Build a Redis transaction using MULTI and EXEC commands to update the stock of two products atomically.
📋 What You'll Learn
Create two keys with initial stock values
Start a transaction with MULTI
Queue two DECRBY commands to reduce stock
Execute the transaction with EXEC
💡 Why This Matters
🌍 Real World
Redis transactions help keep data consistent when multiple related changes must happen together, like updating stock levels in an online store.
💼 Career
Understanding Redis transactions is useful for backend developers and database administrators working with caching and real-time data stores.
Progress0 / 4 steps
1
DATA SETUP: Create initial stock keys
Create two Redis keys called product:1001:stock and product:1002:stock with values 50 and 30 respectively using the SET command.
Redis
Hint
Use the SET command to assign values to keys.
2
CONFIGURATION: Start the transaction
Start a Redis transaction by issuing the MULTI command.
Redis
Hint
Use MULTI to begin queuing commands for a transaction.
3
CORE LOGIC: Queue stock decrement commands
Queue two DECRBY commands inside the transaction to reduce product:1001:stock by 5 and product:1002:stock by 3.
Redis
Hint
Use DECRBY key amount to decrease stock values inside the transaction.
4
COMPLETION: Execute the transaction
Execute all queued commands atomically by issuing the EXEC command.
Redis
Hint
Use EXEC to run all queued commands in the transaction.
Practice
(1/5)
1. What does the EXEC command do in Redis transactions?
easy
A. It runs all queued commands in a transaction at once.
B. It starts queuing commands for a transaction.
C. It cancels all queued commands in a transaction.
D. It checks the status of the Redis server.
Solution
Step 1: Understand Redis transaction commands
The MULTI command starts queuing commands, and EXEC runs them all together.
Step 2: Identify the role of EXEC
EXEC executes all commands queued after MULTI in one atomic operation.
Final Answer:
It runs all queued commands in a transaction at once. -> Option A
Quick Check:
EXEC runs queued commands = B [OK]
Hint: Remember: MULTI queues, EXEC runs all queued commands [OK]
Common Mistakes:
Confusing EXEC with MULTI
Thinking EXEC cancels commands
Assuming EXEC checks server status
2. Which of the following is the correct sequence to run multiple commands atomically in Redis?
easy
A. SET key value, EXEC, MULTI
B. EXEC, MULTI, SET key value
C. MULTI, SET key value, EXEC
D. SET key value, MULTI, EXEC
Solution
Step 1: Recall the transaction command order
First, MULTI starts the transaction, then commands like SET queue, and finally EXEC runs them.
Step 2: Match the correct order
MULTI, SET key value, EXEC follows this order: MULTI, then SET, then EXEC.
Final Answer:
MULTI, SET key value, EXEC -> Option C
Quick Check:
Start with MULTI, end with EXEC = A [OK]
Hint: MULTI starts, EXEC ends the transaction [OK]
Common Mistakes:
Putting EXEC before MULTI
Running SET before MULTI
Mixing command order
3. Given the following Redis commands executed in order:
MULTI
SET user:1:name "Alice"
INCR user:1:visits
EXEC
What will be the result of the EXEC command?
medium
A. An error because INCR on a string key
B. ["OK", 1]
C. ["QUEUED", "QUEUED"]
D. Empty array []
Solution
Step 1: Analyze queued commands
SET user:1:name "Alice" sets a string key, returns "OK"; INCR user:1:visits increments a numeric key, returns the new value.
Step 2: Understand EXEC output
EXEC returns an array with results of each command: ["OK", 1].
Final Answer:
["OK", 1] -> Option B
Quick Check:
EXEC returns results array = C [OK]
Hint: EXEC returns results of all queued commands in order [OK]
Common Mistakes:
Expecting QUEUED responses from EXEC
Thinking INCR causes error on string key
Assuming EXEC returns empty array
4. You run the following Redis commands:
MULTI
SET key1 "value1"
GET key1
EXEC
What will be the result of the EXEC command?
medium
A. An error because GET command is not allowed inside MULTI block
B. You forgot to call MULTI before queuing commands
C. The GET command returns QUEUED, causing EXEC to fail
D. No error; EXEC always succeeds if MULTI was called
Solution
Step 1: Understand command behavior in transactions
Some commands like GET are allowed but return QUEUED during MULTI, and EXEC returns their results after execution.
Step 2: Identify that no error occurs
GET is fully supported inside MULTI/EXEC blocks, so EXEC succeeds and returns ["OK", "value1"].
Final Answer:
No error; EXEC always succeeds if MULTI was called -> Option D
Quick Check:
GET inside MULTI causes no error = D [OK]
Hint: Check if commands are allowed inside MULTI before EXEC [OK]
Common Mistakes:
Assuming all commands work inside MULTI
Forgetting to call MULTI before EXEC
Expecting EXEC to never fail
5. You want to increment a counter atomically with optimistic locking (aborting if the key was modified meanwhile), using Redis transactions. Which sequence correctly uses WATCH, MULTI, and EXEC to do this safely?
hard
A.
WATCH counter
MULTI
INCR counter
EXEC
B.
MULTI
WATCH counter
INCR counter
EXEC
C.
MULTI
INCR counter
EXEC
WATCH counter
D.
INCR counter
WATCH counter
MULTI
EXEC
Solution
Step 1: Understand WATCH usage
WATCH monitors keys for changes before starting a transaction with MULTI.
Step 2: Correct command order for safe increment
First WATCH counter, then MULTI, queue INCR counter, and finally EXEC to run if no changes occurred.
Final Answer:
WATCH counter
MULTI
INCR counter
EXEC -> Option A
Quick Check:
WATCH before MULTI for safe transactions = D [OK]
Hint: Always WATCH keys before MULTI to ensure safe EXEC [OK]
Common Mistakes:
Calling WATCH after MULTI
Running INCR before WATCH
Not using WATCH for conditional updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.