Redis Transactions allow the execution of a group of commands in a single step, they are centered around the commands
MULTI
,EXEC
,DISCARD
andWATCH
. Redis Transactions make two important guarantees:
- All the commands in a transaction are serialized and executed sequentially. A request sent by another client will never be served in the middle of the execution of a Redis Transaction. This guarantees that the commands are executed as a single isolated operation.
- The
EXEC
command triggers the execution of all the commands in the transaction, so if a client loses the connection to the server in the context of a transaction before calling theEXEC
command none of the operations are performed, instead if theEXEC
command is called, all the operations are performed. When using the append-only file Redis makes sure to use a single write(2) syscall to write the transaction on disk. However if the Redis server crashes or is killed by the system administrator in some hard way it is possible that only a partial number of operations are registered. Redis will detect this condition at restart, and will exit with an error. Using theredis-check-aof
tool it is possible to fix the append only file that will remove the partial transaction so that the server can start again.Starting with version 2.2, Redis allows for an extra guarantee to the above two, in the form of optimistic locking in a way very similar to a check-and-set (CAS) operation. This is documented later on this page
Redis 事务允许执行一组命令 在一个步骤中,它们以命令
MULTI,
EXEC
,DISCARD
和WATCH
为中心。 Redis 交易提供两个重要保证:
- 事务中的所有命令都序列化并执行 顺序。另一个客户端发送的请求永远不会 在执行 Redis 事务的过程中提供服务。 这保证了命令作为单个命令执行 隔离操作。
执行命令
触发事务中所有命令的执行,因此 如果客户端在 事务在调用EXEC
命令之前没有任何操作 ,而不是如果调用EXEC
命令,则所有 执行操作。使用仅追加文件时,Redis 确保 使用单个 write(2) 系统调用将事务写入磁盘。 但是,如果 Redis 服务器崩溃或被系统管理员杀死 在某些困难的方式中,可能只有部分数量的操作 已注册。Redis 将在重新启动时检测到此情况,并将退出并显示错误。 使用该工具可以修复 仅附加将删除部分事务的文件,以便 服务器可以重新启动。redis-check-aof
从版本 2.2 开始,Redis 允许对 以上两个,以乐观锁定的形式以非常类似于 检查并设置 (CAS) 操作。