如何在Redis中使用回滚实现事务 [英] How to implement transaction with rollback in Redis

查看:962
本文介绍了如何在Redis中使用回滚实现事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要将数据作为事务添加到Redis中的两个列表中。两个列表中的数据应保持一致。如果存在异常或系统故障,因此仅将添加的数据编程到一个列表中,则系统应该能够恢复和回滚。但是基于Redis文档,它不支持回滚。我该如何实施?我使用的语言是Java。

My program needs to add data to two lists in Redis as a transaction. Data should be consistent in both lists. If there is an exception or system failure and thus program only added data to one list, system should be able to recover and rollback. But based on Redis doc, it doesn't support rollback. How can I implement this? The language I use is Java.

推荐答案

如果需要事务回滚,建议使用Redis以外的其他方法。 Redis事务与其他数据存储不同。甚至Multi / Exec也无法满足您的需求-首先是因为没有回滚。如果要回滚,则必须下拉两个列表,然后才能还原-并希望在我们的错误情况和回滚之间,没有其他客户端也可以修改这两个列表。以理智而可靠的方式做到这一点并非易事,也不简单。对于SO来说,这可能也不是一个好问题,因为它涉及面很广,而且不是Redis专用的。

If you need transaction rollback, I recommend using something other than Redis. Redis transactions are not the same as for other datastores. Even Multi/Exec doesn't work for what you want - first because there is no rollback. If you want rollback you will have to pull down both lists so you can restore - and hope that between our error condition and the "rollback" no other client also modified either of the lists. Doing this in a sane and reliable way is not trivial, nor simple. It would also probably not be a good question for SO as it would be very broad, and not Redis specific.

现在,为什么EXEC不做可能做的事情认为。在您提出的方案中,MULTI / EXEC仅 处理以下情况:

Now as to why EXEC doesn't do what one might think. In your proposed scenario MULTI/EXEC only handles the cases of:


  1. 您设置了WATCHES以确保没有其他情况发生了更改

  2. 您的客户端在发出EXEC之前就死了

  3. Redis内存不足

由于执行EXEC命令,完全有可能出错。发出EXEC时,Redis将在队列中执行 all 命令并返回错误列表。它不会提供add-to-list-1正常工作和add-to-list-2失败的情况。您仍然会使两个列表不同步。发出MULTI之后,如果发出LPUSH,则除非您:

It is entirely possible to get errors as a result of issuing the EXEC command. When you issue EXEC, Redis will execute all commands in the queue and return a list of errors. It will not provide the case of the add-to-list-1 working and add-to-list-2 failing. You would still have your two lists out of sync. When you issue, say an LPUSH after issuing MULTI, you will always get back an OK unless you:

    $,否则您总是会得到 OK b $ b
  • a)之前添加了一个手表,并且列表中的某些内容发生了更改,或者

  • b)Redis返回了OOM条件,以响应排队的推送命令

  • a) previously added a watch and something in that list changed or
  • b) Redis returns an OOM condition in response to a queued push command

DISCARD无法像某些人认为的那样工作。 DISCARD用于代替 EXEC,而不用作回滚机制。签发EXEC后,您的交易即告完成。 Redis根本没有任何回滚机制,这与Redis的交易无关。

DISCARD does not work like some might think. DISCARD is used instead of EXEC, not as a rollback mechanism. Once you issue EXEC your transaction is completed. Redis does not have any rollback mechanism at all - that isn't what Redis' transaction are about.

了解Redis所谓的交易的关键是要意识到它们本质上是交易客户端连接级别的命令队列。它们不是数据库状态机。

The key to understanding what Redis calls transactions is to realize they are essentially a command queue at the client connection level. They are not a database state machine.

这篇关于如何在Redis中使用回滚实现事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆