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.
Previous
What is the difference between Redis Sentinel and Redis Cluster?
Next
What is WATCH and optimistic locking in Redis?
More Redis Questions
View all →- Intermediate What is Redis Cluster and how does it shard data?
- Intermediate What is the difference between Redis Sentinel and Redis Cluster?
- Intermediate What is WATCH and optimistic locking in Redis?
- Intermediate What is Lua scripting in Redis and when should you use it?
- Intermediate What are Redis eviction policies?