Introduction
DISCARD lets you stop a group of commands you started with MULTI, so nothing changes in the database.
DISCARD
MULTI SET key1 value1 DISCARD
MULTI INCR counter DISCARD
MULTI
SET name "Alice"
GET name
DISCARD
GET nameDISCARD command do in a Redis transaction?MULTI?MULTI, you can either execute with EXEC or abort with DISCARD.DISCARD is the official command to abort queued commands; ABORT and ROLLBACK do not exist in Redis.MULTI SET key1 value1 DISCARD GET key1What will be the output of the
GET key1 command?MULTI, SET key1 value1 is queued but not executed yet.DISCARD aborts the transaction, so SET key1 value1 is never applied.key1 was never set, GET key1 returns nil.MULTI SET key1 value1 DISCARD EXECWhat will happen when you run
EXEC after DISCARD?DISCARD aborts the transaction and clears the queue, ending the transaction state.EXEC causes an error because EXEC requires an active MULTI block.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?Pass the test and this pattern moves unknown