Mastering Redis Transactions: Commands, Rollback Strategies, and Limitations
Redis, a non‑relational database, implements transaction-like behavior using commands such as MULTI, EXEC, DISCARD, and WATCH, allowing queued execution, optimistic locking, and various rollback granularities, though its semantics differ from traditional relational database transactions.
Redis Transaction Commands
Redis is a non‑relational database that does not natively support transactions, but it can emulate transactional behavior by queuing multiple commands and executing them atomically.
multi
The MULTI command starts a transaction block; subsequent commands are queued instead of being executed immediately.
exec
The EXEC command executes all commands that were queued after MULTI .
discard
The DISCARD command aborts the current transaction, clears the queue, and discards all pending commands.
watch
The WATCH command monitors one or more keys; if any watched key is modified before EXEC , the transaction is aborted.
Transaction Rollback Granularity
Redis provides several rollback scenarios:
1.1 Explicit discard – full rollback
Calling DISCARD aborts the transaction and rolls back all queued commands.
1.2 Automatic full rollback on syntax error
If a syntax error occurs in any queued command after MULTI , Redis aborts the entire transaction.
1.3 Watch‑based full rollback
When a watched key changes before EXEC , all queued commands are discarded.
1.4 Partial rollback
If a command fails due to a logical error (e.g., incrementing a string), Redis skips the failing command but continues executing subsequent commands, resulting in a partial success.
Summary
1) Redis, as a NoSQL database, does not support true transactions; it achieves transaction‑like behavior through WATCH , MULTI , and EXEC . 2) Redis transactions differ from relational database transactions: they cannot guarantee complete atomicity when logical errors occur, leading to partial rollbacks.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.