DISCARD without MULTI
Production Risk
Low. This is a simple protocol violation that is easily caught and fixed.
This error occurs when a client sends a DISCARD command but is not currently in a transaction state. A transaction must first be initiated with the MULTI command to be discardable.
- 1A client library bug where DISCARD is called improperly.
- 2A client connection was dropped and reconnected, losing the transaction context, but the application logic tried to DISCARD anyway.
- 3Manual execution of DISCARD in a CLI session without first typing MULTI.
A client sends a DISCARD command on a fresh connection.
DISCARD
expected output
(error) ERR DISCARD without MULTI
Fix 1
Only call DISCARD within a MULTI block
WHEN When you need to cancel a transaction that has been started
MULTI INCR mycounter DISCARD
Why this works
The DISCARD command is only valid when a transaction has been initiated by MULTI. It flushes the queue of commands and exits the transaction state.
Fix 2
Use a client library's error handling for transactions
WHEN In application code
// pseudocode for a library
const tx = client.multi();
try {
tx.incr('foo');
// something fails here
throw new Error();
} catch (e) {
tx.discard(); // Library correctly calls discard
}Why this works
Good client libraries will automatically handle calling DISCARD if an error occurs during the queueing phase of a transaction, preventing orphaned transactions and improper calls.
✕ Use DISCARD to clear errors
DISCARD only works for cancelling a MULTI block. It has no other function.
Transaction state machine in connection handling.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev