Bird
Raised Fist0
Redisquery~10 mins

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

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

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

  1. Step 1: Understand the role of MULTI

    The MULTI command starts a transaction and queues subsequent commands instead of executing them immediately.
  2. Step 2: Differentiate MULTI from other commands

    EXEC runs the queued commands, DISCARD cancels them, and no command checks status directly.
  3. Final Answer:

    To start a transaction and queue commands for atomic execution -> Option C
  4. 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

  1. Step 1: Recall the transaction command order

    First, MULTI starts the transaction, then commands are queued, and finally EXEC runs them.
  2. Step 2: Verify the sequence

    Only MULTI, commands, EXEC follows the correct order: MULTI, then commands, then EXEC.
  3. Final Answer:

    MULTI, commands, EXEC -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    ["OK", 1] -> Option B
  4. 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

  1. 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.
  2. Step 2: Understand Redis transaction error handling

    Redis queues commands and executes all on EXEC. It does not rollback on errors. INCR fails with a type error, but SET succeeds.
  3. Final Answer:

    EXEC returns an error for INCR because key1 is not an integer -> Option A
  4. Quick Check:

    Type error in transaction = error on EXEC [OK]
Hint: Redis transactions don't rollback on command errors [OK]
Common Mistakes:
  • Assuming transaction rolls back on error
  • Expecting INCR to convert string automatically
  • Thinking only first command runs
5. You want to increment a counter only if a key lock does not exist, using Redis transactions. Which sequence correctly ensures atomicity?
hard
A. WATCH lock MULTI INCR counter EXEC
B. MULTI SET lock 1 INCR counter EXEC
C. MULTI INCR counter GET lock EXEC
D. MULTI GET lock INCR counter EXEC

Solution

  1. Step 1: Understand the need for atomic check and increment

    You must check if lock exists and increment counter only if it does not, atomically.
  2. Step 2: Use WATCH to monitor lock

    WATCH lock watches the key for changes. If lock changes before EXEC, the transaction aborts, ensuring atomicity.
  3. Step 3: MULTI queues commands and EXEC runs them if lock unchanged

    This sequence ensures the increment happens only if lock was not modified.
  4. Final Answer:

    WATCH lock MULTI INCR counter EXEC -> Option A
  5. Quick Check:

    WATCH + MULTI = atomic check and update [OK]
Hint: Use WATCH before MULTI to ensure atomic conditional updates [OK]
Common Mistakes:
  • Not using WATCH for conditional logic
  • Checking key inside MULTI without WATCH
  • Assuming MULTI alone ensures atomic condition
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