0
0
Redisquery~10 mins

Transaction execution model in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Transaction execution model
Start Transaction: MULTI
Queue Commands
Execute Commands: EXEC
All Commands Run Atomically
Transaction Ends
If DISCARD: Cancel Transaction
This flow shows how Redis transactions start with MULTI, queue commands, then execute all atomically with EXEC or cancel with DISCARD.
Execution Sample
Redis
MULTI
SET key1 value1
INCR counter
EXEC
This Redis transaction queues two commands and executes them atomically.
Execution Table
StepCommandActionState ChangeOutput
1MULTIStart transaction modeCommands queued = 0OK
2SET key1 value1Queue commandCommands queued = 1QUEUED
3INCR counterQueue commandCommands queued = 2QUEUED
4EXECExecute all queued commands atomicallyCommands executed = 2["OK", 1]
5-Transaction endsCommands queue cleared-
💡 After EXEC, all queued commands run atomically and transaction ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Commands queued001200
key1nullnullnullnullvalue1value1
counternullnullnullnull11
Key Moments - 3 Insights
Why do commands return QUEUED after MULTI but not their usual output?
Because after MULTI, commands are only queued, not executed yet. See execution_table rows 2 and 3 where output is QUEUED.
What happens if EXEC is not called after MULTI?
Commands remain queued and are not executed. The transaction stays open until EXEC or DISCARD is called.
Can commands outside MULTI/EXEC run during a transaction?
Yes, but they run separately. The transaction commands run atomically only after EXEC.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of the INCR command before EXEC?
AQUEUED
B1
COK
DError
💡 Hint
Check the output column at step 3 in the execution_table.
At which step does the transaction actually execute the queued commands?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the EXEC command in the execution_table.
If DISCARD was called instead of EXEC at step 4, what would happen to the commands queued?
AThey would run partially
BThey would execute anyway
CThey would be canceled and not run
DThey would run after DISCARD
💡 Hint
Recall the concept_flow where DISCARD cancels the transaction.
Concept Snapshot
Redis transactions start with MULTI to queue commands.
Commands return QUEUED until EXEC runs them all atomically.
EXEC executes all queued commands together.
DISCARD cancels queued commands without running.
Commands outside transaction run immediately.
Transactions ensure atomic execution of multiple commands.
Full Transcript
In Redis, a transaction begins with the MULTI command, which tells Redis to start queuing commands instead of running them immediately. Each command after MULTI returns QUEUED to show it is stored for later. When EXEC is called, all queued commands run together atomically, meaning either all succeed or none do. After EXEC, the transaction ends and the queue clears. If DISCARD is called instead, the queued commands are canceled and not executed. Commands outside the transaction run normally. This model ensures multiple commands can be executed safely as one unit.