A multi-key transaction in Redis is a way to execute multiple commands on different keys as a single atomic operation, ensuring all commands succeed or none do.
Click to reveal answer
beginner
Which Redis commands start and execute a multi-key transaction?
The MULTI command starts a transaction, and EXEC executes all queued commands atomically.
Click to reveal answer
intermediate
How does Redis ensure consistency when multiple keys are involved in a transaction?
Redis queues all commands after MULTI and runs them together with EXEC. If any watched key changes, the transaction aborts to ensure consistency.
Click to reveal answer
intermediate
What role does the WATCH command play in multi-key transactions?
WATCH monitors one or more keys for changes. If any watched key changes before EXEC, the transaction aborts to prevent inconsistent updates.
Click to reveal answer
advanced
Can Redis transactions be rolled back automatically if a command fails?
No, Redis transactions do not support automatic rollback. All commands are queued and executed atomically, but if a command fails during execution, previous commands are not undone.
Click to reveal answer
Which command starts a multi-key transaction in Redis?
AEXEC
BMULTI
CWATCH
DDISCARD
✗ Incorrect
MULTI starts the transaction by queuing commands.
What happens if a watched key changes before EXEC is called?
ARedis automatically retries the transaction
BThe transaction continues normally
CThe transaction aborts and EXEC returns null
DThe watched key is reset
✗ Incorrect
If a watched key changes, Redis aborts the transaction to avoid inconsistent updates.
Which command executes all queued commands in a Redis transaction?
AEXEC
BDISCARD
CWATCH
DMULTI
✗ Incorrect
EXEC runs all commands queued after MULTI atomically.
Can Redis transactions include commands on multiple keys?
AYes, Redis supports multi-key transactions
BNo, transactions are limited to one key
COnly if keys are in the same hash slot
DOnly with Lua scripts
✗ Incorrect
Redis transactions can include commands on multiple keys, executed atomically.
What does the DISCARD command do in Redis transactions?
AStarts watching keys
BExecutes the queued commands
CLocks keys for the transaction
DCancels all queued commands and ends the transaction
✗ Incorrect
DISCARD cancels the transaction and clears queued commands.
Explain how Redis uses MULTI, EXEC, and WATCH commands to maintain consistency in multi-key transactions.
Think about how Redis prevents conflicts when multiple clients update keys.
You got /4 concepts.
Describe the limitations of Redis transactions regarding rollback and error handling.
Consider what happens if a command inside EXEC fails.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using MULTI and EXEC commands in Redis transactions?
easy
A. To group multiple commands so they execute atomically for data consistency
B. To speed up the execution of individual Redis commands
C. To create backups of the Redis database automatically
D. To delete multiple keys at once
Solution
Step 1: Understand MULTI command
The MULTI command starts a transaction block in Redis, grouping commands.
Step 2: Understand EXEC command
The EXEC command executes all commands queued after MULTI atomically, ensuring consistency.
Final Answer:
To group multiple commands so they execute atomically for data consistency -> Option A
Quick Check:
Atomic execution = Data consistency [OK]
Hint: MULTI starts, EXEC runs all commands atomically [OK]
Common Mistakes:
Thinking MULTI speeds up commands individually
Confusing transactions with backups
Assuming EXEC deletes keys
2. Which of the following is the correct syntax to start a Redis transaction?
easy
A. EXEC
B. START
C. BEGIN
D. MULTI
Solution
Step 1: Identify Redis transaction start command
Redis uses MULTI to begin a transaction block.
Step 2: Check other options
EXEC ends the transaction, BEGIN and START are not Redis commands.
Final Answer:
MULTI -> Option D
Quick Check:
Start transaction = MULTI [OK]
Hint: Start with MULTI, end with EXEC [OK]
Common Mistakes:
Using EXEC to start instead of end
Using SQL-like BEGIN or START commands
Confusing transaction commands with key commands
3. Consider the following Redis commands executed in a transaction:
MULTI
SET key1 "value1"
SET key2 "value2"
EXEC
What will be the state of key1 and key2 after EXEC?
medium
A. key1 and key2 will both have their new values set atomically
B. Only key1 will be set, key2 will remain unchanged
C. Neither key1 nor key2 will be set because EXEC fails
D. key1 will be set, but key2 will be deleted
Solution
Step 1: Understand MULTI and EXEC behavior
Commands between MULTI and EXEC are queued and executed atomically.
Step 2: Analyze the commands
Both SET key1 "value1" and SET key2 "value2" will be applied together when EXEC runs.
Final Answer:
key1 and key2 will both have their new values set atomically -> Option A
Quick Check:
Atomic set of keys = Both keys updated [OK]
Hint: EXEC applies all queued commands together [OK]
Common Mistakes:
Thinking commands run immediately after MULTI
Assuming partial execution on EXEC failure
Confusing EXEC with DISCARD
4. You wrote this Redis transaction:
MULTI
SET key1 "val1"
GET key1
EXEC
But the GET key1 command returns null inside the transaction. Why?
medium
A. SET command failed, so GET returns null
B. GET command syntax is incorrect
C. GET inside MULTI queues the command and returns null until EXEC runs
D. EXEC was not called, so commands did not run
Solution
Step 1: Understand command queuing in MULTI
Inside a transaction, commands are queued and do not execute immediately.
Step 2: Effect on GET command
GET returns null because the SET has not executed yet; actual results come after EXEC.
Final Answer:
GET inside MULTI queues the command and returns null until EXEC runs -> Option C
Quick Check:
Queued commands return null before EXEC [OK]
Hint: GET returns null inside MULTI before EXEC [OK]
Common Mistakes:
Thinking GET runs immediately inside MULTI
Assuming SET failed without checking
Forgetting to call EXEC
5. You want to update two related keys account:balance and account:points atomically in Redis. Which approach ensures both keys update together or not at all?
hard
A. Use GET to check keys, then SET commands separately
B. Use MULTI, queue both SET commands, then EXEC to apply atomically
C. Use WATCH on both keys, then run SET commands without MULTI
D. Run two separate SET commands without MULTI
Solution
Step 1: Understand atomic updates need transactions
To update multiple keys together, Redis transactions with MULTI and EXEC ensure atomicity.
Step 2: Analyze other options
Separate SET commands risk partial updates; WATCH helps optimistic locking but needs MULTI for atomicity.
Final Answer:
Use MULTI, queue both SET commands, then EXEC to apply atomically -> Option B
Quick Check:
Atomic multi-key update = MULTI + EXEC [OK]
Hint: MULTI + EXEC ensures all-or-nothing updates [OK]
Common Mistakes:
Running SET commands separately risking partial updates
Using WATCH without MULTI for atomicity
Relying on GET before SET without transaction
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.