MongoDB中的两阶段提交

pet*_*who 7 transactions mongodb

仔细阅读在线文档后,我仍然有很多关于MongoDB中两阶段提交的问题.

在" 从故障情景中恢复 "一节中,为什么只有两类故障?在我的思想中,失败可能发生在任何这些步骤中,所以这里应该有两个以上的类.例如,在更新帐户A后,(在" 应用两个帐户的交易"部分中)数据库服务器失败.这意味着账户A在账户B没有任何事情的情况下损失了一些钱.而且我们会有不一致的交易?

Phi*_*ipp 7

When the application or database suddenly crashes between applying the transaction to A and applying the transaction to B, there will still be a transaction with state:"pending" in the global transaction collection. Your recovery script which you run after a crash should notice this, check the two accounts, and see that there is a pending transaction in one, but not the other account. It now knows everything it needs to know to either rollback the transaction or try to complete it.

Yes, writing a recovery script which is that smart isn't easy. But transactions in a database system not designed for them is always hard. Sometimes you can work around requiring transactions in MongoDB by designing your documents in a way that fields which need to be updated together are always in the same document, but there isn't always a sane way to do this. When your use-case absolutely needs transactions, protect your sanity and use a relational database.


Sam*_*aye 4

在我看来,这些步骤中的任何一步都可能发生失败,因此这里应该有两个以上的类。

这里要记住的关键一点是,文档并不是关于两阶段提交的明确指南,实际上,两阶段提交在 MongoDB 中技术上是不可能的,如果您想执行两阶段提交,我强烈建议您使用 ACID 技术。

您必须记住,有些失败场景是您无法应对的,因为它不在服务器本身内。相反,两阶段提交的整个存在仅在客户端,因此与 TTL 监控相比,它非常脆弱。

更新帐户 A 后,数据库服务器出现故障。这意味着账户 A 损失了一些钱,而账户 B 没有发生任何事情。那么我们会出现不一致的交易吗?

为了支持 Philipps 的回答,在这种情况下,帐户 A 和 B 之间的原始交易仍将处于待处理状态,只有在更新帐户 B 后才会将其移至已完成/已完成。

这意味着事务在完成之前无法标记为已完成。

我建议使用专为您的场景设计的东西,而不是将 MongoDB 用于并非真正设计的场景。