NOTE
1.4 Redis Transactions
What Redis transactions are, MULTI/EXEC/DISCARD, no rollback, optimistic locking with WATCH, and comparison with MySQL transactions.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is a Redis Transaction?
- Execute a group of commands in one step, with two guarantees:
- All commands in a transaction are executed sequentially. In other words, while this group of commands is being executed, other clients cannot see an intermediate state.
- Atomicity. That is, all commands are either executed or not executed. However, if one command fails during execution, commands that were already executed are not rolled back.
2. Using Redis Transactions
2.1. Execute and Cancel a Transaction
2.1.1. Execute a Transaction
# 2. Start transaction
multi
# 3. Commands
set age 30
get age
get name
# 4. Commit transaction
exec

2.1.2. Cancel a Transaction
# Start transaction
multi
# Commands
set age 30
get age
get name
# Cancel transaction
discard

2.2. The Transaction Is Actually Executed Only at exec
The following example shows that session 1 has not changed the database before exec.

2.3. Redis Transactions Do Not Roll Back
-
If there is a syntax error, the transaction is not executed, so rollback does not apply.

-
If there is a semantic error, the transaction does not roll back commands that have already been executed and continues executing later commands.

3. Use Redis Transactions to Implement Optimistic Locking
- client1
watch name
multi
set age 111
get age
# Wait for client2
exec
- client2
set name wsy
After client2 changes name, when client1 executes exec, it returns nil, meaning execution did not succeed.
4. Redis Transactions vs. MySQL Transactions
- MySQL transactions satisfy ACID, while Redis only satisfies I.
- A: for a group of Redis statements, if an error occurs while one statement is being executed, previous statements are not rolled back.
- C: consistency is guaranteed by the programmer and A + I; A is not satisfied.
- D: Redis persistence can lose 1 second of data.
| MySQL | Redis | |
|---|---|---|
| Start | start transaction |
multi |
| Statements | ordinary SQL | ordinary commands |
| Failure | rollback |
discard |
| Success | commit |
exec |
-
Difference between rollback and discard If two statements have already executed successfully and the third statement fails: After Rollback, the effects of the first two statements disappear. Discard only ends the current transaction; the effects caused by the first two statements still remain.
-
For statements after
multi, errors can occur in two situations:
-
The syntax itself is invalid. In this case,
execreports an error and none of the statements are executed. -
The syntax itself is correct, but the target object is inappropriate, for example using
zaddon a list object. Afterexec, the correct statements are executed and inappropriate statements are skipped.
5. Redis Transaction Execution Flow
- Batch operations are queued and cached before the
EXECcommand is sent. - After receiving
EXEC, transaction execution begins. If any command in the transaction fails, the remaining commands are still executed. - During transaction execution, command requests submitted by other clients are not inserted into the transaction’s command-execution sequence.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub