0
0
Redisquery~10 mins

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

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