What if you could start a big change and then just say 'stop' without any mess?
Why DISCARD to abort in Redis? - Purpose & Use Cases
Imagine you start a series of changes to your data in Redis, but halfway through, you realize something is wrong and want to cancel all those changes.
Without a way to abort, you would have to undo each change manually, which is confusing and risky.
Manually reversing each command is slow and error-prone.
You might miss some changes or leave the data in an inconsistent state.
This can cause bugs and data corruption.
The DISCARD command lets you cancel all the commands queued in a transaction before they run.
This means you can safely abort your changes and keep your data clean without extra work.
MULTI SET key1 value1 SET key2 value2 // Oops, mistake found // Manually undo SET commands
MULTI SET key1 value1 SET key2 value2 DISCARD
You can confidently start complex changes and easily cancel them if needed, keeping your data safe and consistent.
When updating multiple user settings at once, if one setting is invalid, you can discard the whole update instead of applying partial changes.
Manual undo of queued commands is risky and slow.
DISCARD aborts all queued commands in a transaction safely.
This keeps your Redis data consistent and error-free.
Practice
DISCARD command do in a Redis transaction?Solution
Step 1: Understand the role of DISCARD in Redis transactions
DISCARD is used to cancel all commands queued after MULTI before EXEC is called.Step 2: Compare DISCARD with other transaction commands
Unlike EXEC which executes queued commands, DISCARD aborts them, so no changes happen.Final Answer:
It cancels all commands queued in the current transaction. -> Option BQuick Check:
DISCARD cancels queued commands = D [OK]
- Confusing DISCARD with EXEC which executes commands
- Thinking DISCARD saves changes instead of aborting
- Assuming DISCARD starts a transaction
MULTI?Solution
Step 1: Recall Redis transaction commands
AfterMULTI, you can either execute withEXECor abort withDISCARD.Step 2: Identify the correct abort command
DISCARDis the official command to abort queued commands;ABORTandROLLBACKdo not exist in Redis.Final Answer:
DISCARD -> Option DQuick Check:
Abort command after MULTI = DISCARD [OK]
- Using EXEC instead of DISCARD to abort
- Typing ABORT or ROLLBACK which are invalid Redis commands
- Confusing DISCARD with starting a transaction
MULTI SET key1 value1 DISCARD GET key1What will be the output of the
GET key1 command?Solution
Step 1: Analyze the transaction commands
AfterMULTI,SET key1 value1is queued but not executed yet.Step 2: Effect of DISCARD on queued commands
DISCARDaborts the transaction, soSET key1 value1is never applied.Step 3: Result of GET key1
Sincekey1was never set,GET key1returnsnil.Final Answer:
nil -> Option AQuick Check:
DISCARD aborts SET, so GET returns nil [OK]
- Assuming SET runs immediately after MULTI
- Expecting EXEC to run automatically without calling it
- Confusing output with command names
MULTI SET key1 value1 DISCARD EXECWhat will happen when you run
EXEC after DISCARD?Solution
Step 1: Understand DISCARD effect on transaction state
DISCARDaborts the transaction and clears the queue, ending the transaction state.Step 2: Behavior of EXEC after DISCARD
Since no transaction is active after DISCARD, callingEXECcauses an error because EXEC requires an active MULTI block.Final Answer:
EXEC returns an error because no transaction is active -> Option CQuick Check:
EXEC after DISCARD errors due to no active transaction [OK]
- Expecting EXEC to run queued commands after DISCARD
- Thinking EXEC silently returns nil after DISCARD
- Assuming EXEC starts a new transaction
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 ENDWhat is the correct way to use
DISCARD here?Solution
Step 1: Understand transaction flow with WATCH and MULTI
WATCH monitors keys, MULTI starts queuing commands, EXEC executes them.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.Step 3: Why other options are incorrect
DISCARD after EXEC or after SET commands is invalid; DISCARD before WATCH does not abort a transaction.Final Answer:
Call DISCARD immediately after MULTI if condition is false, skipping EXEC. -> Option AQuick Check:
DISCARD aborts queued commands before EXEC [OK]
- Calling DISCARD after EXEC which is too late
- Trying to DISCARD before WATCH which does nothing
- Assuming DISCARD cancels commands after they run
Ready to prove it?
Pass the test and this pattern moves unknown
