MongoDB文档操作是原子且隔离的,但是它们是一致的吗? [英] MongoDB Document Operations are Atomic and Isolated, but Are They Consistent?

查看:120
本文介绍了MongoDB文档操作是原子且隔离的,但是它们是一致的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序从App Engine数据存储库移植到MongoDB后端,并且对文档更新"的一致性存在疑问.我了解一个文档上的更新都是原子性的并且是隔离的,但是有没有办法保证它们在不同副本集之间是一致的"?

I'm in the process of porting my application from an App Engine Datastore to a MongoDB backend and have a question regarding the consistency of "document updates." I understand that the updates on one document are all atomic and isolated, but is there a way to guarantee that they're "consistent" across different replica sets?

在我们的应用程序中,许多用户可以(并且将)尝试通过在一次更新中向其中插入一些嵌入式文档(对象)来同时更新一个文档.我们需要确保这些更新在所有副本上都以逻辑上一致的方式发生,即,当一个用户将一些嵌入式文档放入"父文档时,其他用户无法将其嵌入式文档放入父文档中,直到我们确保他们已经阅读并接收了第一个用户的更新.

In our application, many users can (and will) be trying to update one document at the same time by inserting a few embedded documents (objects) into it during one single update. We need to ensure these updates occur in a logically consistent manner across all replicas, i.e. when one user "puts" a few embedded documents into the parent document, no other users can put their embedded documents in the parent document until we ensure they've read and received the first user's updates.

因此,我的意思是一致性,我们需要一种方法来确保如果两个用户试图同时在 上一次对一个文档执行更新,则MongoDB仅允许对其中一个进行更新.通过,并丢弃另一个(或至少防止两者都发生).我们不能在此处使用标准的分片"解决方案,因为单个更新不仅仅包括递增或递减.

So what I mean by consistency is that we need a way to ensure that if two users attempt to perform an update on one document at exactly the same time, MongoDB only allows one of those updates to go through, and discards the other one (or at least prevents both from occuring). We can't use a standard "sharding" solution here, because a single update consists of more than just an increment or decrement.

保证一个特定文档的一致性的最佳方法是什么?

What's the best way of guaranteeing the consistency of one particular document?

推荐答案

MongoDB不提供主-主复制或多版本并发.换句话说,写入总是到副本集中的同一台服务器.默认情况下,即使从次级读取也是禁用的,所以默认行为是一次只能与一台服务器通信.因此,如果您使用原子修饰符(例如$inc, $push等),则无需担心安全模式下的结果不一致.

MongoDB does not offer master-master replication or multi-version concurrency. In other words, writes always go to the same server in a replica set. By default, even reads from secondaries are disabled so the default behavior is that you communicate only with one server at a time. Therefore, you do not need to worry about inconsistent results in safe mode if you use atomic modifiers (like $inc, $push, etc.).

如果您不希望自己局限于这些原子修饰符,请按照dcrosta的建议进行比较和交换(以及 mongo文档)看起来是个好主意.所有这些都与副本集或分片无关,但是-在单服务器方案中将是相同的.

If you don't want to restrict yourself to these atomic modifiers, compare and swap as recommended by dcrosta (and the mongo docs) looks like a good idea. All this is not related to replica sets or sharding, however - it would be the same in a single-server scenario.

如果在数据库/节点故障的情况下也需要确保读取一致性,则应确保以安全模式写入大多数服务器.

If you need to ensure read consistency also in case of a database/node failure, you should make sure you're writing to the majority of servers in safe mode.

如果您允许不安全的读取,这两种方法的行为会有所不同:原子更新操作仍然可以工作(但可能会产生意外的结果),而比较交换方法则会失败.

The two approaches behave different if you allow unsafe reads: the atomic update operations would still work (but may give unexpected results), while the compare-and-swap approach would fail.

这篇关于MongoDB文档操作是原子且隔离的,但是它们是一致的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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