Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What does the MULTI command do in Redis?

easy
A. Immediately executes all commands
B. Starts a transaction block to queue commands
C. Deletes all keys in the database
D. Shows the current database size

Solution

  1. Step 1: Understand the purpose of MULTI

    The MULTI command begins a transaction block where commands are queued instead of executed immediately.
  2. Step 2: Compare with other options

    The other options describe different Redis commands or actions unrelated to MULTI.
  3. Final Answer:

    Starts a transaction block to queue commands -> Option B
  4. Quick Check:

    MULTI = start transaction block [OK]
Hint: MULTI queues commands for atomic execution [OK]
Common Mistakes:
  • Thinking MULTI executes commands immediately
  • Confusing MULTI with FLUSHDB or DBSIZE
  • Assuming MULTI deletes keys
2.

Which of the following is the correct syntax to start a transaction block in Redis?

?
easy
A. START TRANSACTION
B. EXEC
C. MULTI
D. BEGIN

Solution

  1. Step 1: Identify Redis transaction start command

    In Redis, MULTI is used to start a transaction block.
  2. Step 2: Check other options

    EXEC runs queued commands, BEGIN and START TRANSACTION are SQL commands, not Redis.
  3. Final Answer:

    MULTI -> Option C
  4. Quick Check:

    Start transaction = MULTI [OK]
Hint: MULTI starts, EXEC runs queued commands [OK]
Common Mistakes:
  • Using SQL syntax like BEGIN or START TRANSACTION
  • Confusing EXEC as start command
  • Typing lowercase commands (Redis commands are case-insensitive but usually uppercase)
3.

Consider the following Redis commands executed in order:

MULTI
SET key1 value1
SET key2 value2
EXEC
GET key1
GET key2

What will be the output of the two GET commands?

medium
A. ["value1", "value2"]
B. [null, null]
C. ["value1", null]
D. [null, "value2"]

Solution

  1. Step 1: Understand MULTI and EXEC behavior

    The commands between MULTI and EXEC are queued and executed atomically when EXEC runs. So both SET commands run together.
  2. Step 2: Check GET commands after EXEC

    After EXEC, both keys are set, so GET key1 returns "value1" and GET key2 returns "value2".
  3. Final Answer:

    ["value1", "value2"] -> Option A
  4. Quick Check:

    Queued SETs run on EXEC, GET returns set values [OK]
Hint: EXEC runs queued commands, so GET sees updated keys [OK]
Common Mistakes:
  • Assuming SET commands run immediately before EXEC
  • Expecting null because commands are queued
  • Confusing output format of GET commands
4.

What is wrong with this Redis transaction block?

MULTI
SET key1 value1
GET key1
EXEC
medium
A. MULTI must be followed by WATCH
B. EXEC must come before GET
C. SET cannot be used inside MULTI
D. GET inside MULTI queues the command, does not return value immediately

Solution

  1. Step 1: Analyze GET inside MULTI block

    Inside MULTI, commands are queued and do not return results immediately, so GET queues but does not return the value now.
  2. Step 2: Understand EXEC behavior

    EXEC runs all queued commands and returns their results together, so GET's result is only available after EXEC.
  3. Final Answer:

    GET inside MULTI queues the command, does not return value immediately -> Option D
  4. Quick Check:

    Commands queue inside MULTI, results after EXEC [OK]
Hint: GET inside MULTI queues, no immediate output [OK]
Common Mistakes:
  • Expecting GET to return value immediately inside MULTI
  • Thinking EXEC must come before GET
  • Believing SET is disallowed inside MULTI
5.

You want to update two keys user:1 and user:2 atomically in Redis. Which sequence correctly uses MULTI and EXEC to ensure both updates happen together?

?
hard
A. MULTI SET user:1 "Alice" SET user:2 "Bob" EXEC
B. SET user:1 "Alice" MULTI SET user:2 "Bob" EXEC
C. MULTI SET user:1 "Alice" EXEC SET user:2 "Bob"
D. EXEC MULTI SET user:1 "Alice" SET user:2 "Bob"

Solution

  1. Step 1: Identify correct transaction block

    MULTI SET user:1 "Alice" SET user:2 "Bob" EXEC starts with MULTI, queues both SET commands, then runs them together with EXEC.
  2. Step 2: Check other options for errors

    SET user:1 "Alice" MULTI SET user:2 "Bob" EXEC queues only one SET inside MULTI; MULTI SET user:1 "Alice" EXEC SET user:2 "Bob" runs EXEC too early; EXEC MULTI SET user:1 "Alice" SET user:2 "Bob" starts with EXEC which is invalid.
  3. Final Answer:

    MULTI SET user:1 "Alice" SET user:2 "Bob" EXEC -> Option A
  4. Quick Check:

    Queue all commands between MULTI and EXEC [OK]
Hint: Queue all commands between MULTI and EXEC for atomic update [OK]
Common Mistakes:
  • Calling EXEC before queuing all commands
  • Starting EXEC before MULTI
  • Mixing commands outside transaction block
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard