DISCARD to abort in Redis - Time & Space Complexity
When using Redis transactions, it's important to know how the time cost changes when you abort a transaction with DISCARD.
We want to understand how DISCARD affects the work Redis does as the transaction size grows.
Analyze the time complexity of the following Redis transaction with DISCARD.
MULTI
SET key1 value1
SET key2 value2
... (more commands) ...
DISCARD
This code starts a transaction, queues several commands, then aborts all queued commands using DISCARD.
Look for repeated actions inside the transaction before DISCARD.
- Primary operation: Queuing each command inside the transaction.
- How many times: Once per command queued before DISCARD.
As you add more commands before DISCARD, Redis queues more commands.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Queue 10 commands, then DISCARD clears all |
| 100 | Queue 100 commands, then DISCARD clears all |
| 1000 | Queue 1000 commands, then DISCARD clears all |
Pattern observation: The work to queue commands grows with the number of commands, but DISCARD clears all queued commands in a single step.
Time Complexity: O(n)
This means the time to abort with DISCARD grows linearly with the number of commands queued in the transaction.
[X] Wrong: "DISCARD instantly cancels the transaction with no extra work regardless of size."
[OK] Correct: Redis must clear all queued commands, so the time depends on how many commands were queued before DISCARD.
Understanding how DISCARD works helps you reason about transaction costs and error handling in Redis, a useful skill for real-world database tasks.
"What if we replaced DISCARD with EXEC? How would the time complexity change?"
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
