views:

44

answers:

1

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  A: 

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/Last+Error+Commands

mdirolf
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](http://www.mongodb.org/display/DOCS/Last+Error+Commands#comment-61398549) explains how to trigger various commit styles by passing different options to getLastError().
mjs
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