Introduction
Transactions let you run multiple commands together as one group. This helps keep your data safe and consistent.
Transactions let you run multiple commands together as one group. This helps keep your data safe and consistent.
MULTI COMMAND1 COMMAND2 ... EXEC
MULTI starts the transaction.
EXEC runs all queued commands together.
MULTI SET key1 value1 SET key2 value2 EXEC
MULTI INCR counter INCR counter EXEC
This transaction sets two fields for user 1 together.
MULTI SET user:1:name Alice SET user:1:age 30 EXEC
Commands inside MULTI are queued and only run when EXEC is called.
If any command fails, EXEC still runs all commands; Redis does not rollback.
Use WATCH before MULTI to monitor keys and abort if they change.
Transactions group commands to run together safely.
Use MULTI to start and EXEC to run the commands.
Redis transactions do not rollback on errors automatically.
MULTI command in Redis transactions?MULTI command starts a transaction and queues subsequent commands instead of executing them immediately.EXEC runs the queued commands, DISCARD cancels them, and no command checks status directly.MULTI starts the transaction, then commands are queued, and finally EXEC runs them.MULTI, then commands, then EXEC.MULTI SET key1 value1 INCR counter EXECWhat will be the result of the
EXEC command if counter does not exist before the transaction?SET key1 value1 sets a string key, and INCR counter increments the counter key. If counter does not exist, Redis treats it as 0 before incrementing.counter is missing, INCR will create it with value 1. Both commands succeed.MULTI SET key1 value1 INCR key1 EXECWhat is the expected outcome and why?
SET key1 value1 sets key1 to a string. INCR key1 expects an integer but key1 is a string.EXEC. It does not rollback on errors. INCR fails with a type error, but SET succeeds.lock does not exist, using Redis transactions. Which sequence correctly ensures atomicity?lock exists and increment counter only if it does not, atomically.lockWATCH lock watches the key for changes. If lock changes before EXEC, the transaction aborts, ensuring atomicity.lock unchangedlock was not modified.Pass the test and this pattern moves unknown