🔴 Redis Intermediate

How do MULTI and EXEC work in Redis transactions?

Answer

MULTI begins a Redis transaction. All commands issued after MULTI are queued but not executed immediately — the server responds with QUEUED for each. EXEC atomically executes all queued commands in order and returns an array of results. DISCARD cancels the transaction and discards the queue. Important caveats: Redis transactions provide atomicity (all commands execute in sequence without interruption) but NOT rollback — if a command fails mid-transaction (e.g., wrong type for an operation), the other commands still execute. This is unlike SQL transactions. Also, MULTI/EXEC does not provide isolation from reads in other clients during the queuing phase — use WATCH for optimistic locking. Lua scripts via EVAL are an alternative that provides stronger atomicity guarantees.