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.

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB - Part 1/3

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB

What you’ll learn

  • Work with MongoDB with Clarity and Confidence
  • Use 4 tools MongoCHEF, NOSQL Manager, RoboMongo, MongoBooster easily
  • Do Regex, GridFS , Replication , Sharding, Full text search
  • Basic and Advanced CRUD operations using MongoDB
  • Import and Export data from MongoDB
  • Work MapReduce, Embedded Documents,Save&Insert , indexing, capped collections, TTL
  • Bonus section * Use java,C#,PHP,Nodejs to access MongoDB features like CRUD, GridFS
  • Bonus Section * A 50 minutes MongoDB key feature exercises
  • 100+ Quizzes 40+ Activities


Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

MongoDB - The Complete Developer’s Guide

Building A REST API With MongoDB, Mongoose, And Node.js

Node.js, ExpressJs, MongoDB and Vue.js (MEVN Stack) Application Tutorial

MEAN Stack Tutorial MongoDB, ExpressJS, AngularJS and NodeJS

MongoDB with Python Crash Course - Tutorial for Beginners

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB - Part 2/3

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB


What you’ll learn

  • Work with MongoDB with Clarity and Confidence
  • Use 4 tools MongoCHEF, NOSQL Manager, RoboMongo, MongoBooster easily
  • Do Regex, GridFS , Replication , Sharding, Full text search
  • Basic and Advanced CRUD operations using MongoDB
  • Import and Export data from MongoDB
  • Work MapReduce, Embedded Documents,Save&Insert , indexing, capped collections, TTL
  • Bonus section * Use java,C#,PHP,Nodejs to access MongoDB features like CRUD, GridFS
  • Bonus Section * A 50 minutes MongoDB key feature exercises
  • 100+ Quizzes 40+ Activities

Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

MongoDB - The Complete Developer’s Guide

Learn MongoDB : Leading NoSQL Database from scratch

Learn NoSQL Databases - Complete MongoDB Bootcamp 2019

Why We Moved From NoSQL MongoDB to PostgreSQL?

How to build GraphQL APIs with Kotlin, Spring Boot, and MongoDB?

Build a CRUD Operation using PHP & MongoBD

MongoDB with Python Crash Course - Tutorial for Beginners

Learn NoSQL Databases from Scratch - Complete MongoDB Bootcamp 2019

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB - Part 3/3

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB

What you’ll learn

  • Work with MongoDB with Clarity and Confidence
  • Use 4 tools MongoCHEF, NOSQL Manager, RoboMongo, MongoBooster easily
  • Do Regex, GridFS , Replication , Sharding, Full text search
  • Basic and Advanced CRUD operations using MongoDB
  • Import and Export data from MongoDB
  • Work MapReduce, Embedded Documents,Save&Insert , indexing, capped collections, TTL
  • Bonus section * Use java,C#,PHP,Nodejs to access MongoDB features like CRUD, GridFS
  • Bonus Section * A 50 minutes MongoDB key feature exercises
  • 100+ Quizzes 40+ Activities

Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

MongoDB - The Complete Developer’s Guide

Learn MongoDB : Leading NoSQL Database from scratch

Learn NoSQL Databases - Complete MongoDB Bootcamp 2019

Build a CRUD Operation using PHP & MongoBD

MongoDB with Python Crash Course - Tutorial for Beginners

Learn NoSQL Databases from Scratch - Complete MongoDB Bootcamp 2019

AngularJS tutorial for beginners with NodeJS, ExpressJS and MongoDB

MEAN Stack Tutorial MongoDB, ExpressJS, AngularJS and NodeJS

Creating RESTful APIs with NodeJS and MongoDB Tutorial