Advertisement
Beginner Redis, Transactions

How do you cancel a queued Redis transaction before it executes, and what command is used for this?

Short answer

You use the DISCARD command, which cancels the current transaction and clears all commands that were queued after MULTI was called, returning the connection to its normal state without executing any of the queued commands.

Save Redis mock interview
Advertisement
AModel answer
You use the DISCARD command, which cancels the current transaction and clears all commands that were queued after MULTI was called, returning the connection to its normal state without executing any of the queued commands.
Example

MULTI SET temp_key 'value' DISCARD

Real-world example
An application starts building a transaction but decides midway through, based on some application logic, that the operation should not proceed, so it calls DISCARD to safely cancel everything queued so far.
Likely follow-ups
  • Does DISCARD affect commands that were already executed outside the transaction?
  • What is the difference between DISCARD and simply not calling EXEC?
Related topics