Read-your-own-writes consistency in Mongodb

最后都变了- 提交于 2019-12-05 03:54:59
William Z

There are a couple of points about this question.

  1. You aren't guaranteed to have read-after-write consistency unless you're using either "safe=true", "w=1" (or greater) or "j=true" with your write. You can either include these as part of the insert() or update() commands, or else use set_lasterror_options() to set these options for the connection, database, or collection that you're using.

  2. If you're allowing reads from secondary nodes, (e.g. a ReadPreference other than PRIMARY), then you will not get read-after-write semantics, but only eventual consistency.

  3. If you are using a ReadPreference of PRIMARY and you're setting the appropriate lasterror options, then you're guaranteed to get read-after-write semantics on all operations that use the same socket, that is, the same thread.

  4. If you're using multiple threads, and you are NOT reading from secondary nodes, then you're guaranteed to get read-after-write consistency as long as you issue the read in the second thread after the write completes in the first thread. You can use standard thread synchronization primitives to assure this.

I'm the author of Motor and I know a bit about AsyncMongo too. Here's Motor's documentation regarding safe writes:

http://emptysquare.net/motor/pymongo/api/motor/differences.html#acknowledged-writes

Short answer: Whatever code you execute in a callback to insert(), update(), etc., if those inserts or updates are safe, will see the data in MongoDB after the change has been applied. Any code you execute not in such a callback may run before or after MongoDB has applied the change.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!