MongoDB uses an on disk journal to guarantee write operations and to provide crash resiliency. MongoDB creates a journal for every write that includes the exact disk location and the bytes that changed in the write. Thus if you have a crash in the server, the journal can be used to replay any writes that have not yet been written to the data files.
MongoDB uses memory mapped files to write your data to disk. By default MongoDB data files are flushed to disk every 60 s. MongoDB also uses memory mapped files for the journal. By default the journal is flushed to disk every 100 ms. Since the final data files are flushed to disk every 60s the journal does not need to track writes for more than 60s. For more details of the mechanics of the journalling refer to the official documentation.
Journalling Write Concern
>db.data.insert({"name":"testentry"}); >db.runCommand({"getLastError":1, "j":true});
When you turn on journalling you also have to ability to specify a write concern of ‘Journalled’ for your MongoDB operations. This implies that mongod confirms the write operation only after committing to the journal. However this has a drawback – when you specify “j”:true with getLastError MongoD will wait about 1/3 of the journalcommitinternal before committing the journal data. The default journalcommitinterval is 100ms – so mongod will wait 30ms and commit the data. This essentially means that on a single thread you can only get about 33.3 writes/sec. The recommended practice is to batch your writes. For example if you have 50 writes use the “j”:true setting only on the last write – this will acknowledge that all the previous 50 writes are done.
Summary
Every production MongoDB instance should run with journalling enabled. If you dont have journalling enabled and your server or mongod process crashes MongoDB will not be ensure data integrity. You will need to run a “repair” operation on the database which depending on the amount of data could take a few hours to complete. Turn it off only if you really know what you are doing. At MongoDirector all our instances follow the mongodb best practice configuration and have journalling turned on by default.
This article is originally published at Mongo Director Blog, re-posted here with authors permission and as part of the JBC program.