0
0
Redisquery~10 mins

MULTI command to start in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MULTI command to start
Client sends MULTI
Server replies +OK
Client queues commands
Client sends EXEC
Server executes all queued commands
Server returns all results as array
The MULTI command starts a transaction by telling Redis to queue commands. Later, EXEC runs all queued commands atomically.
Execution Sample
Redis
MULTI
SET key1 value1
INCR counter
EXEC
Starts a transaction, queues SET and INCR commands, then executes them all together.
Execution Table
StepClient CommandServer ResponseState/Action
1MULTI+OKTransaction mode started, commands will be queued
2SET key1 value1+QUEUEDCommand queued, not executed yet
3INCR counter+QUEUEDCommand queued, not executed yet
4EXEC*2All queued commands executed atomically, results returned
💡 EXEC runs all queued commands and ends the transaction mode
Variable Tracker
VariableStartAfter 1After 2After 3Final
transaction_modefalsetruetruetruefalse
command_queueempty['SET key1 value1']['SET key1 value1', 'INCR counter']['SET key1 value1', 'INCR counter']empty
Key Moments - 2 Insights
Why does the server reply +QUEUED instead of the actual command result after SET?
Because after MULTI, commands are only queued, not executed immediately. The actual results come after EXEC, as shown in execution_table row 2.
What happens if EXEC is not sent after MULTI?
Commands remain queued and are not executed. The transaction stays open, so no changes happen until EXEC or DISCARD is sent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the server response when the client sends INCR counter?
A+QUEUED
B*1
C+OK
D-ERROR
💡 Hint
Check execution_table row 3 under Server Response
At which step does the transaction mode end?
AAfter MULTI
BAfter SET command
CAfter EXEC
DAfter INCR command
💡 Hint
Look at execution_table row 4 and variable_tracker transaction_mode changes
If the client sends EXEC immediately after MULTI without any commands queued, what will happen?
AServer returns an error
BServer returns an empty array
CServer executes last command twice
DServer ignores EXEC
💡 Hint
Recall that EXEC runs all queued commands; if none queued, result is empty array
Concept Snapshot
MULTI starts a transaction in Redis.
Commands after MULTI are queued, not run.
Server replies +QUEUED for each queued command.
EXEC runs all queued commands atomically.
Transaction mode ends after EXEC or DISCARD.
Full Transcript
The MULTI command in Redis starts a transaction mode. When a client sends MULTI, the server replies with +OK and enters transaction mode. Commands sent after MULTI are not executed immediately but queued, with the server replying +QUEUED for each. When the client sends EXEC, Redis executes all queued commands atomically and returns their results as an array. The transaction mode ends after EXEC. If EXEC is not sent, commands remain queued and unexecuted. This ensures multiple commands run together safely.