-
Notifications
You must be signed in to change notification settings - Fork 45.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
关于 redis 没有事务回滚? #491
Comments
看一下官方文档 https://redis.io/topics/transactions Errors inside a transactionDuring a transaction it is possible to encounter two kind of command errors: However starting with Redis 2.6.5, the server will remember that there was an error during the accumulation of commands, and will refuse to execute the transaction returning also an error during EXEC, and discarding the transaction automatically. Before Redis 2.6.5 the behavior was to execute the transaction with just the subset of commands queued successfully in case the client called EXEC regardless of previous errors. The new behavior makes it much more simple to mix transactions with pipelining, so that the whole transaction can be sent at once, reading all the replies later at once. Errors happening after EXEC instead are not handled in a special way: all the other commands will be executed even if some command fails during the transaction. This is more clear on the protocol level. In the following example one command will fail when executed even if the syntax is right: Why Redis does not support roll backs?If you have a relational databases background, the fact that Redis commands can fail during a transaction, but still Redis will execute the rest of the transaction instead of rolling back, may look odd to you. However there are good opinions for this behavior: Redis commands can fail only if called with a wrong syntax (and the problem is not detectable during the command queueing), or against keys holding the wrong data type: this means that in practical terms a failing command is the result of a programming errors, and a kind of error that is very likely to be detected during development, and not in production. |
没懂他的意思,是说redis不支持回滚,但是又保持原子性,这个我没理解 |
首先原子性的定义:事务中的命令要么全部被执行,要么全部都不执行。 然后再看官方文档关键段:
我根据Redis文档理解,认为事务过程中失败有两种可能:
综上:所以Redis 没有事务回滚 |
这里第2条有一个疑问:如果发生了这种情况,redis会使用redis-check-aof程序移除AOF中不完整事务的信息,顺利启动。但这个时候是不是事务中的命令就只执行了一半?假设事务中有4个命令,完整的场景是:
这个时候是不是就只执行了一半的命令?重启之后还会对刚刚执行了一半的这个事务做额外处理吗? |
我本地启动 redis 试了下,Mac 下的 redis-6.2.2
此时查看 aof 文件: 红框框内为 MULTI 的命令,此时模拟 aof 文件损坏,也就是将 23~25 行手动删除,此时的 aof 文件: 当 redis 正常启动后,发现 aof 文件已被修复:13~23 行被删除: 结论:redis 会根据 aof 日志恢复,但是当 aof 日志有损坏的 MULTI 事务片段时,该片段会被删除,不存在执行一半的情况。 |
谢谢 @LiWenGu 这个实验已经基本回答我的疑问了。换个方式,假设命令顺序如下: MULTI aof被修复的时候,由于上面的整个multi的指令都会被删除,所以redis server重启之后,就不存在 set age相关的操作,所以age这个key也就不存在了,这个时候也就保持了原子性。这样理解对吗? |
我觉得你理解没毛病。要么都执行要么都不执行。 |
OK 谢谢! |
不一定吧 -> https://blog.csdn.net/yangshangwei/article/details/82866216
The text was updated successfully, but these errors were encountered: