Transactions, Scripting, and PipelinesLesson 5.1
Redis transactions with MULTI and EXEC: atomic command batches
MULTI, EXEC, DISCARD, command queuing, all-or-nothing execution, error handling in transactions, no rollback on runtime errors
Transactions in Redis
Redis transactions group commands into a block. Commands between MULTI and EXEC are queued and executed atomically โ no other client can interleave commands during execution.
Basic transaction
MULTI
INCR balance:alice
DECR balance:bob
EXEC
# โ [1, -1] (array of results in order)Discarding a transaction
MULTI
INCR balance:alice
DISCARD # clears the queue, rolls back nothing (nothing was written)Important limitation: no rollback
Redis transactions do NOT roll back on runtime errors. If one command fails mid-EXEC (e.g., calling INCR on a non-integer), Redis continues executing the remaining commands. Only syntax errors during queuing abort the entire transaction.
MULTI
SET x 10
INCR not_a_number_key # will fail at runtime
SET y 20
EXEC
# SET x โ OK (executed)
# INCR โ error (failed)
# SET y โ OK (still executed)This differs from SQL transactions. Redis sacrifices ACID rollback for simplicity and speed. Use WATCH (next lesson) when you need conditional atomicity.
