11

first, here is what is said in Pymongo Documentation

By default, PyMongo starts a request for each thread when the thread first runs an operation on MongoDB. This guarantees **read-your-writes consistency. Within a request, the thread will continue to use the same socket exclusively, and no other thread will use this socket, until the thread calls end_request() or it terminates. At that point, the socket is returned to the connection pool for use by other threads.

so when using an async library to Mongodb (like Asyncmongo, Motor), will the user have a consistency like the one in blocking calls or an eventual consistency?

Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65

2 Answers2

8

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.

William Z
  • 10,989
  • 4
  • 31
  • 25
  • i think that you give all possibilities :D am using mongodb with tornado, and in tornado it's an event loop, so it's a hard way to get threads, so what about the asynch libraries (that dont use threads) – Abdelouahab Pp Sep 24 '12 at 21:26
  • 1
    Umm ... it depends on the asynch library you're using. The same rules apply: you're only guaranteed to read what you wrote once the getLastError() command returns successfully. Once you've gotten a successful return from getLastError(), then any thread that initiates a find() will see the written data. Synchronization between threads is left as an exercise for the reader. – William Z Sep 24 '12 at 22:21
  • i think it's the answer since it is speaking in the general way, so i've only to see how Motor or Asyncmongo works :D thank you again – Abdelouahab Pp Sep 24 '12 at 22:23
  • 2
    For Motor, you need to have a callback function, and you will be able to read what you've written once the callback function completes without error. Ref: http://emptysquare.net/motor/pymongo/api/motor/differences.html#acknowledged-writes – William Z Sep 24 '12 at 22:26
  • am starting to learn the async calls, so this is why i've asked this question, to give me more 'energy' to learn it, and you give me a lot :D thank you again – Abdelouahab Pp Sep 24 '12 at 22:29
3

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.

A. Jesse Jiryu Davis
  • 23,641
  • 4
  • 57
  • 70