2

In some sense, the default "save" operation appears to be asynchronous in MongoDB. It seems that when a client saves a document, it will typically receive a successful response immediately from the server, even if the operation has not yet been applied to the database (let alone committed to disk).

(I'm basing this model for how save() behaves on the PHP client documentation, which contains an optional parameter--safe--which I think does ensure that the save has been processed by the server, rather than merely received by the server, and a blog post comment by Mathias Stearn in which he says "By default 'commited' just means applied to the mmaped structure, but it can also mean replicated or flushed to disk.")

I can see that there are several levels of "success" a save() might reach, including: (a) sent by client; (b) received by server; (c) committed by server to internal datastructures; (d) sent by server to n replicas; and (e) committed to disk.

  • What does MongoDB do by default?
  • How can this be tuned? (I couldn't find an equivalent for PHP's safe mode in the mongo shell, for example.)

Also, is there ever any difference in behaviour toward the client issuing the save(), and any other client? i.e. does MongoDB support read-your-own-writes, or similar?

1 Answer 1

2

All of these options are done w/ the getLastError command. That is basically what "safe mode" does under the hood. This page should be helpful: http://www.mongodb.org/display/DOCS/getLastError+Command

3
  • 1
    Somewhat counter-intuitive, but it appears that yes, without any options, getLastError blocks and waits for the previously issued command to be committed to memory. A comment on the "Last Error Commands" page explains how to trigger various commit styles by passing different options to getLastError().
    – mjs
    Jul 10, 2010 at 9:01
  • 1
    actually - any read operation will block and wait for previously issued writes (on the same connection) to be committed to memory. a getLastError w/ no options achieves the same by virtue of being a read.
    – mdirolf
    Jul 10, 2010 at 13:47
  • @mdirolf can you reference some documentation that states that? This page (blog.mongodb.org/post/498145601/…) seems to imply that it is possible with Mongo to get stale data in a read (though I recognize you left this answer a long time ago).
    – Josh
    Aug 7, 2012 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.