# On MongoDB durability and commit models

<p>I&rsquo;ve been reading and thinking a bit about
<a href="http://www.mongodb.org/">MongoDB&rsquo;s</a> consistency model a bit over the
last few weeks.  I think what follows is an accurate model of how it
behaves, and what the implications are, but I would be grateful for
any corrections:</p>

<ul>
<li>Whilst <code>mongod</code> is running, you may as well imagine that all your
data is stored in memory, and that the files it creates on disk don&rsquo;t
actually exist.  This is because the files themselves are memory
mapped, and MongoDB makes no guarantees about whether they&rsquo;re in a
consistent state.  (They&rsquo;re theoretically consistent immediately after
an fsync, and this can be forced in several ways, but I don&rsquo;t think
there&rsquo;s any easy way to snapshot the database files immediately after
the fsync.) [<strong>Update</strong>: Kristina pointed out that it is possible to fsync and then prevent the memory mapped file from being modified via the <a href="http://www.mongodb.org/display/DOCS/Backups#Backups-Fsync%2CWriteLockandBackup">fsync and lock command</a>.  Once the lock (which <a href="http://www.mongodb.org/display/DOCS/Backups#comment-61858237">blocks <em>all</em> write operations</a>, though not reads) is in place, you can safely copy/snapshot the database file.]</li>
<li>Since the data is stored in memory (including virtual memory) you
need replication to ensure durability.  However, MongoDB argue you
need replication for durability anyway (notwithstanding their plans to
add single-server durability <a href="http://jira.mongodb.org/browse/SERVER-980">in
1.8</a>).  In a blog post
(&ldquo;<a href="http://blog.mongodb.org/post/381927266/what-about-durability">What about Durability?</a>&rdquo;),
they point out:

<ul>
<li>even if your database theoretically supports single-server
durability (such as CouchDB with its crash only design), this will
only protect you if you&rsquo;ve turned off hardware buffering or have a
battery-backed RAID controller to ensure your writes really hit disk</li>
<li>having a well-formed database file on disk doesn&rsquo;t help you if
the disk itself fails, and this mode of failure is about as likely as
any other</li>
<li>for some applications, you need 100% uptime, and so the delay
required to recover from a failure (by reloading the database and
replaying the transaction log, for example) is unacceptable</li>
</ul>
</li>
<li>By default writes are not persisted immediately to the
database&mdash;not even the in-memory data structure.  Writes instead go
into a per-server queue (with
<a href="http://en.wikipedia.org/wiki/FIFO">FIFO</a> ordering?), and there&rsquo;s no
guarantee about when the write will be visible to other clients.
(Although it does seem that clients are guaranteed to <a href="http://stackoverflow.com/questions/3211444/what-commit-styles-are-available-in-mongodb/3213961#3213961">read their own
writes</a>.)</li>
<li>To change the default write behaviour, you: (a) issue the write as
normal and then (b) issue the confusingly-named
<a href="http://www.mongodb.org/display/DOCS/Last+Error+Commands"><code>getLastError()</code></a>
command.  <code>getLastError()</code> blocks until the write is either committed
to the in-memory data structure, fsynced to disk, or committed to <em>n</em>
replicas, depending on the arguments passed.  (Note that many clients
abstract away the write plus <code>getLastError()</code> call, so that the
arguments to <code>getLastError()</code> become arguments to the write.

<ul>
<li>To block until the write is committed, issue <code>getLastError()</code>
with no options.</li>
<li>To block until the data structure is fsynced to disk, issue
<code>getLastError()</code> with <code>fsync = true</code>.</li>
<li>To block until the write has been added to the write queue of
<em>n</em> replicas, issue <code>w = n</code>.  (Note that there is currently no way to
<a href="http://www.mongodb.org/display/DOCS/Replication#Replication-ReplicationAcknowledgementviagetlasterror">block until the write has been
fsynced</a>
on the replicas.)</li>
</ul>
</li>
</ul>
