A transaction is a set of commands executed in a single step
All commands in the transaction are executed as a single isolation operation and in sequence. It is not possible to make a request to another client in the middle of executing a Redis transaction.
Redis transactions are also atomic. Atomic means that either all commands are processed or none are processed.
Use transaction
#启动事务:MULTI
命令列表
执行事务:EXEC
Other commands:
DISCARD : Discard all commands issued after MULTI
WATCH (lock a key, optimistic lock): monitor the given key to determine the execution of the transaction block
UNWATCH : unlock
Remarks:
-
Use watch to lock
watch key when
starting a transaction — lock key unwatch — unlock the locked key -
If the transaction execution fails, unlock the operation
-
Compile error: The command was written incorrectly, all commands are not executed
-
Run-time error: the command cannot be executed, other commands are still executed (that is, the transaction will be executed when it enters the queue)
Redis plugin used by springboot
jedis: It is not safe to use direct connection and multiple thread operations. You can use jedis pool to solve this problem.
lettuce: thread sharing, used by default after 2.x