Bird
Raised Fist0
Redisquery~10 mins

DISCARD to abort in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - DISCARD to abort
Start Transaction with MULTI
↓
Queue Commands
↓
DECIDE to Abort?
↓
Run DISCARD
↓
Clear Queue
↓
End Transaction
This flow shows how a Redis transaction starts, queues commands, then either aborts with DISCARD or executes with EXEC.
Execution Sample
Redis
MULTI
SET key1 value1
SET key2 value2
DISCARD
Starts a transaction, queues two SET commands, then aborts all queued commands with DISCARD.
Execution Table
StepCommandActionTransaction Queue StateOutput
1MULTIStart transactionEmpty queueOK
2SET key1 value1Queue command['SET key1 value1']QUEUED
3SET key2 value2Queue command['SET key1 value1', 'SET key2 value2']QUEUED
4DISCARDAbort transaction and clear queueEmpty queueOK
5-No commands executedEmpty queue(nil)
💡 DISCARD clears the queued commands and ends the transaction without executing them.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Transaction QueueEmptyEmpty['SET key1 value1']['SET key1 value1', 'SET key2 value2']EmptyEmpty
Key Moments - 2 Insights
Why does DISCARD clear the queued commands instead of executing them?
DISCARD is designed to abort the transaction. As shown in execution_table step 4, it clears the queue so no commands run.
What happens if you run EXEC after DISCARD?
After DISCARD, the queue is empty (see variable_tracker final state), so EXEC would execute nothing and return nil.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the state of the transaction queue?
AEmpty queue
BContains one command
CContains two commands
DTransaction ended
💡 Hint
Check the 'Transaction Queue State' column at step 3 in execution_table.
At which step does the transaction queue get cleared?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where DISCARD is run and queue becomes empty in execution_table.
If DISCARD was replaced by EXEC at step 4, what would happen?
AQueue would be cleared without execution
BCommands would be executed
CTransaction would abort
DError would occur
💡 Hint
EXEC runs all queued commands; see concept_flow branch where No leads to EXEC.
Concept Snapshot
DISCARD aborts a Redis transaction.
Use MULTI to start queuing commands.
Commands are queued but not run until EXEC.
DISCARD clears the queue and ends the transaction.
No commands run after DISCARD.
Useful to cancel a transaction safely.
Full Transcript
In Redis, when you start a transaction with MULTI, commands are queued but not executed immediately. You can either execute all queued commands with EXEC or abort the transaction with DISCARD. DISCARD clears all queued commands and ends the transaction without running them. This is useful if you decide not to proceed with the transaction. The execution table shows starting MULTI, queuing two SET commands, then DISCARD clears the queue. The variable tracker confirms the queue is empty after DISCARD. If you run EXEC after DISCARD, nothing happens because the queue is empty. This flow helps you safely cancel transactions in Redis.

Practice

(1/5)
1. What does the DISCARD command do in a Redis transaction?
easy
A. It starts a new transaction.
B. It cancels all commands queued in the current transaction.
C. It saves the current database state to disk.
D. It executes all commands queued in the current transaction.

Solution

  1. Step 1: Understand the role of DISCARD in Redis transactions

    DISCARD is used to cancel all commands queued after MULTI before EXEC is called.
  2. Step 2: Compare DISCARD with other transaction commands

    Unlike EXEC which executes queued commands, DISCARD aborts them, so no changes happen.
  3. Final Answer:

    It cancels all commands queued in the current transaction. -> Option B
  4. Quick Check:

    DISCARD cancels queued commands = D [OK]
Hint: DISCARD always cancels queued commands without executing them [OK]
Common Mistakes:
  • Confusing DISCARD with EXEC which executes commands
  • Thinking DISCARD saves changes instead of aborting
  • Assuming DISCARD starts a transaction
2. Which of the following is the correct syntax to abort a Redis transaction after MULTI?
easy
A. EXEC
B. ABORT
C. ROLLBACK
D. DISCARD

Solution

  1. Step 1: Recall Redis transaction commands

    After MULTI, you can either execute with EXEC or abort with DISCARD.
  2. Step 2: Identify the correct abort command

    DISCARD is the official command to abort queued commands; ABORT and ROLLBACK do not exist in Redis.
  3. Final Answer:

    DISCARD -> Option D
  4. Quick Check:

    Abort command after MULTI = DISCARD [OK]
Hint: Use DISCARD to abort, EXEC to execute queued commands [OK]
Common Mistakes:
  • Using EXEC instead of DISCARD to abort
  • Typing ABORT or ROLLBACK which are invalid Redis commands
  • Confusing DISCARD with starting a transaction
3. Consider the following Redis commands executed in order:
MULTI
SET key1 value1
DISCARD
GET key1
What will be the output of the GET key1 command?
medium
A. nil
B. "value1"
C. Error: EXEC without MULTI
D. "key1"

Solution

  1. Step 1: Analyze the transaction commands

    After MULTI, SET key1 value1 is queued but not executed yet.
  2. Step 2: Effect of DISCARD on queued commands

    DISCARD aborts the transaction, so SET key1 value1 is never applied.
  3. Step 3: Result of GET key1

    Since key1 was never set, GET key1 returns nil.
  4. Final Answer:

    nil -> Option A
  5. Quick Check:

    DISCARD aborts SET, so GET returns nil [OK]
Hint: DISCARD cancels queued SET, so key remains unset [OK]
Common Mistakes:
  • Assuming SET runs immediately after MULTI
  • Expecting EXEC to run automatically without calling it
  • Confusing output with command names
4. You run the following Redis commands:
MULTI
SET key1 value1
DISCARD
EXEC
What will happen when you run EXEC after DISCARD?
medium
A. EXEC returns nil without error
B. EXEC runs and applies SET key1 value1
C. EXEC returns an error because no transaction is active
D. EXEC starts a new transaction

Solution

  1. Step 1: Understand DISCARD effect on transaction state

    DISCARD aborts the transaction and clears the queue, ending the transaction state.
  2. Step 2: Behavior of EXEC after DISCARD

    Since no transaction is active after DISCARD, calling EXEC causes an error because EXEC requires an active MULTI block.
  3. Final Answer:

    EXEC returns an error because no transaction is active -> Option C
  4. Quick Check:

    EXEC after DISCARD errors due to no active transaction [OK]
Hint: After DISCARD, no active transaction; EXEC causes error [OK]
Common Mistakes:
  • Expecting EXEC to run queued commands after DISCARD
  • Thinking EXEC silently returns nil after DISCARD
  • Assuming EXEC starts a new transaction
5. You want to safely update multiple keys in Redis but abort if a condition fails. Which sequence correctly uses DISCARD to abort the transaction if the condition is false?
WATCH key1
MULTI
IF condition IS FALSE
DISCARD
ELSE
SET key1 newValue
SET key2 newValue2
EXEC
END
What is the correct way to use DISCARD here?
hard
A. Call DISCARD immediately after MULTI if condition is false, skipping EXEC.
B. Call DISCARD after EXEC to undo changes if condition is false.
C. Call DISCARD before WATCH to abort the transaction.
D. Call DISCARD after SET commands to cancel them.

Solution

  1. Step 1: Understand transaction flow with WATCH and MULTI

    WATCH monitors keys, MULTI starts queuing commands, EXEC executes them.
  2. Step 2: Use DISCARD to abort before EXEC if condition fails

    If the condition is false, DISCARD cancels queued commands before EXEC runs, preventing changes.
  3. Step 3: Why other options are incorrect

    DISCARD after EXEC or after SET commands is invalid; DISCARD before WATCH does not abort a transaction.
  4. Final Answer:

    Call DISCARD immediately after MULTI if condition is false, skipping EXEC. -> Option A
  5. Quick Check:

    DISCARD aborts queued commands before EXEC [OK]
Hint: Use DISCARD before EXEC to abort if condition fails [OK]
Common Mistakes:
  • Calling DISCARD after EXEC which is too late
  • Trying to DISCARD before WATCH which does nothing
  • Assuming DISCARD cancels commands after they run
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