How durable is MongoDB?


How durable is MongoDB?

<strong>MongoDB durability is highly configurable.</strong>

MongoDB durability is highly configurable.


By default, data is written to memory and that memory is flushed to disk every 60 seconds or so. Also by default, writes are "fire and forget", so write are sent to the server but the drivers don't wait for any server error messages.


With 2.0 journaling is enabled. Under journaling, the journal is flushed to disk every 100ms while the actual data is still flushed every 60s. If a node with journaling goes off-line and then comes back on-line it will re-run the journal files and it should be caught up to the "last journal".


With 1.6+ you can use Replica Sets. A Replica Set has multiple "replicas" of the data, but only one Primary node that can accept writes. When you write to the Primary it writes to a special "oplog" collection. The secondary nodes ("replicas") all read from this oplog collection and update their own data.


There are several possible failure scenarios. The big one is that you can write to the Primary and then lose that Primary before the write is replicated. Some people also keep delayed secondaries to provide a window around major data losses.


So how do you ensure your writes?


When you issue a write to the database, you can attach a "WriteConcern" or a "SafeMode" (different drivers have different names).


  • Safe: send the write to the server and wait for the server to respond via "getLastError"
  • W=N: send the writes to the server and wait for N servers to respond with a success message. You typically need to use this with a wtimeout.
  • Of course, if it fails, there's no rollback. If you set N=3, you may get a failure because the write only made it to 2 nodes instead of 3 nodes. It's not always obvious what to do here.
  • fsync: send the write to the server and wait for it to fsync (that 60-second process). On 2.0+ this will just wait for the journal instead.
  • j: send the write to the server and wait for journaling to sync to disk (the 100ms process).
  • W=Tag: new feature that allows you to tag some servers and write to everything in the set tagged this way. Of course, there are some special tags and user-defined tags. Useful for things like "sync to this datacenter" or "sync to this rack".


Oh, and yes you can combine some of these (w=1 is the same safe=true), details on the write modes are here:

http://www.mongodb.org/display/D...


Hopefully this gives you some idea of what's possible. There are lots of switches and lots of trade-offs to be made.


Notice that the default settings are high-speed but low durability. If you want really high durability, you have Replica Set + Journaling and you issue writes with the j switch and W=Majority. Of course, if you do this, your writes will be much slower than the default performance.


Again, there are lots of trade-offs here, this topic is like a day-long seminar rather than a simple Quora question.

mongodb

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

MongoDB Database and java applications

Enroll for free demo to acquire the best knowledge on the schema-less database from live industry experts through MongoDB training

Which is the Best MongoDB GUI?

Our MongoDB Online Training provide you to learn about MongoDB strategies with realty. Our MongoDB Online Training also includes live sessions, live Projects, and much

Improve the MongoDB Aggregation Framework

Our MongoDB Online Training provide you to learn about MongoDB strategies with realty. Our MongoDB Online Training also includes live sessions, live Projects, and much

MongoDB – Database, Collection, and Document

Our MongoDB Online Training provide you to learn about MongoDB strategies with realty. Our MongoDB Online Training also includes live sessions, live Projects, and much

MongoDB brings database product range under one cloudy roof

Enroll for free demo to acquire the best knowledge on the schema-less database from live industry experts through MongoDB training