0
0
Redisquery~10 mins

EXEC to execute in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - EXEC to execute
Start Transaction with MULTI
Queue Commands
Call EXEC
Execute All Queued Commands
Return Array of Results
End
This flow shows how Redis queues commands after MULTI and executes them all at once when EXEC is called.
Execution Sample
Redis
MULTI
SET key1 "Hello"
INCR counter
EXEC
This code starts a transaction, queues two commands, then executes them together returning their results.
Execution Table
StepCommandActionResultNotes
1MULTIStart transaction modeOKCommands will be queued, not executed immediately
2SET key1 "Hello"Queue commandQUEUEDCommand added to transaction queue
3INCR counterQueue commandQUEUEDCommand added to transaction queue
4EXECExecute all queued commands["OK", 1]Returns results of SET and INCR commands
5-Transaction ends-All commands executed atomically
💡 EXEC runs all queued commands and returns their results, ending the transaction
Variable Tracker
VariableStartAfter MULTIAfter SET queuedAfter INCR queuedAfter EXECFinal
transaction_modeoffonononoffoff
command_queueemptyempty[SET key1 "Hello"][SET key1 "Hello", INCR counter]emptyempty
key1nilnilnilnil"Hello""Hello"
counternilnilnilnil11
Key Moments - 3 Insights
Why don't the SET and INCR commands run immediately after MULTI?
Because after MULTI, commands are queued and not executed until EXEC is called, as shown in execution_table rows 2 and 3 where commands return QUEUED.
What does EXEC return after running the queued commands?
EXEC returns an array with the results of each command in order, as seen in execution_table row 4 returning ["OK", 1].
What happens to the transaction mode after EXEC?
Transaction mode ends and command queue clears, shown in variable_tracker where transaction_mode goes from 'on' to 'off' and command_queue becomes empty after EXEC.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the INCR command after EXEC?
AOK
BQUEUED
C1
Dnil
💡 Hint
Check the EXEC step in execution_table row 4 where the results array shows the INCR command result as 1.
At which step does the transaction mode turn off?
AAfter MULTI
BAfter EXEC
CAfter SET command
DAfter INCR command
💡 Hint
Look at variable_tracker for transaction_mode changes; it turns off after EXEC.
If you add another command after EXEC without MULTI, what happens?
AIt executes immediately
BIt queues the command
CIt returns an error
DIt ignores the command
💡 Hint
Recall that transaction_mode is off after EXEC, so commands run immediately, not queued.
Concept Snapshot
MULTI starts a transaction, queuing commands.
Commands return QUEUED until EXEC.
EXEC runs all queued commands atomically.
EXEC returns an array of results.
Transaction ends after EXEC.
Full Transcript
In Redis, the EXEC command executes all commands queued after MULTI. When you start a transaction with MULTI, commands like SET or INCR are not run immediately but queued. Each queued command returns QUEUED. When EXEC is called, Redis runs all queued commands in order and returns their results as an array. After EXEC, the transaction mode ends and commands run normally again. This ensures atomic execution of multiple commands.