A transaction in Redis is a group of commands executed as a single, atomic operation, ensuring all commands run sequentially without interruption.
Click to reveal answer
beginner
What Redis command starts a transaction?
The MULTI command starts a transaction in Redis, queuing subsequent commands for atomic execution.
Click to reveal answer
intermediate
How does Redis ensure commands in a transaction are executed atomically?
Redis queues commands after MULTI and executes them all at once with EXEC, preventing other clients from running commands in between.
Click to reveal answer
intermediate
What happens if a command inside a Redis transaction fails before EXEC?
Commands are only queued after MULTI; errors in commands are reported only when EXEC runs. If a command is invalid, EXEC still runs all queued commands including the invalid one, but returns an error for that command.
Click to reveal answer
advanced
What is the role of the WATCH command in Redis transactions?
WATCH monitors keys for changes. If any watched key changes before EXEC, the transaction aborts to avoid conflicts.
Click to reveal answer
Which command starts a Redis transaction?
AEXEC
BMULTI
CWATCH
DBEGIN
✗ Incorrect
The MULTI command starts a transaction by queuing commands.
What does the EXEC command do in Redis?
AExecutes all queued commands atomically
BStarts a transaction
CCancels the transaction
DMonitors keys for changes
✗ Incorrect
EXEC runs all commands queued after MULTI atomically.
If a watched key changes before EXEC, what happens?
AThe key is deleted
BTransaction executes normally
CRedis throws an error immediately
DTransaction aborts
✗ Incorrect
The transaction aborts to avoid conflicts if a watched key changes.
Can Redis transactions rollback partially if a command fails?
ANo partial rollback; commands run atomically but errors are reported after execution
BNo, all commands run or none run
CYes, only failed commands are skipped
DYes, it rolls back all commands
✗ Incorrect
Redis executes all queued commands atomically; errors are reported after execution but no partial rollback occurs.
What is the purpose of the DISCARD command in Redis?
AExecute queued commands
BStart watching keys
CCancel the transaction and clear queued commands
DCommit changes permanently
✗ Incorrect
DISCARD cancels the transaction and clears all queued commands.
Explain how Redis transactions ensure atomic execution of multiple commands.
Think about how commands are queued and then executed together.
You got /4 concepts.
Describe the role of the WATCH command in Redis transactions and how it helps avoid conflicts.
Consider how Redis detects changes to keys before committing a transaction.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of the MULTI command in Redis transactions?
easy
A. To discard all queued commands in a transaction
B. To immediately execute all queued commands
C. To start a transaction and queue commands for atomic execution
D. To check the status of the current transaction
Solution
Step 1: Understand the role of MULTI
The MULTI command starts a transaction and queues subsequent commands instead of executing them immediately.
Step 2: Differentiate MULTI from other commands
EXEC runs the queued commands, DISCARD cancels them, and no command checks status directly.
Final Answer:
To start a transaction and queue commands for atomic execution -> Option C
Quick Check:
MULTI = start transaction [OK]
Hint: MULTI always starts queuing commands, not executing them [OK]
Common Mistakes:
Confusing MULTI with EXEC
Thinking MULTI executes commands immediately
Assuming MULTI discards commands
2. Which of the following is the correct sequence to execute a Redis transaction?
easy
A. EXEC, MULTI, commands
B. commands, MULTI, EXEC
C. commands, EXEC, MULTI
D. MULTI, commands, EXEC
Solution
Step 1: Recall the transaction command order
First, MULTI starts the transaction, then commands are queued, and finally EXEC runs them.
Step 2: Verify the sequence
Only MULTI, commands, EXEC follows the correct order: MULTI, then commands, then EXEC.
Final Answer:
MULTI, commands, EXEC -> Option D
Quick Check:
Order = MULTI -> commands -> EXEC [OK]
Hint: MULTI starts, EXEC ends transaction execution [OK]
Common Mistakes:
Starting with EXEC instead of MULTI
Running commands before MULTI
Mixing order of commands and EXEC
3. Consider the following Redis transaction commands:
MULTI
SET key1 value1
INCR counter
EXEC
What will be the result of the EXEC command if counter does not exist before the transaction?
medium
A. ["OK", "(error) counter is not an integer"]
B. ["OK", 1]
C. Syntax error
D. Transaction discarded
Solution
Step 1: Analyze commands in the transaction
SET key1 value1 sets a string key, and INCR counter increments the counter key. If counter does not exist, Redis treats it as 0 before incrementing.
Step 2: Understand Redis transaction behavior
Redis queues commands and executes them atomically. Since counter is missing, INCR will create it with value 1. Both commands succeed.
Final Answer:
["OK", 1] -> Option B
Quick Check:
INCR missing key = 1 [OK]
Hint: INCR creates missing keys with value 1 [OK]
Common Mistakes:
Expecting error if key is missing
Thinking transaction rolls back on error
Confusing syntax error with runtime error
4. You run this Redis transaction:
MULTI
SET key1 value1
INCR key1
EXEC
What is the expected outcome and why?
medium
A. EXEC returns an error for INCR because key1 is not an integer
B. Both commands succeed; key1 becomes 'value1' then incremented
C. Transaction is discarded due to type error
D. Only SET executes; INCR is ignored
Solution
Step 1: Examine commands and data types
SET key1 value1 sets key1 to a string. INCR key1 expects an integer but key1 is a string.