MongoDB Storage Engine Journaling
Tags: mongodb, nosql, storageengines, wiredtiger
Published: 2017-07-21 13:13:08

---
title: MongoDB Storage Engine Journaling
published: true
description: A discussion of journaling in different storage engines with MongoDB. MMAPv1, Wired Tiger, and memory storage are discussed as it relates to journaling.
tags: MongoDB, NoSQL, Storage Engines, WiredTiger
canonical_url: https://www.kenwalger.com/blog/nosql/mongodb/mongodb-storage-engine-journaling/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/lkg856kqvt9g3z067jjg.png
---
I came across a question the other day as it relates to journaling in <a href="https://www.mongodb.com">MongoDB</a>. SpecificallyÂ how it is handled in the different supported storage engines and is it necessary to use. There was an interesting discussion on this topic so I thought I would generate some thoughts and explanations.

To start with, some questions arise. What exactly is a MongoDB journal? Why is journaling important? For the sake of this post, I'm going to be relating this information to 64-bit builds of mongod and based on the <a href="https://www.mongodb.com/download-center#community">3.4 version</a> of the database.
<h3>What is Journaling?</h3>
Much like one uses a <a href="https://www.amazon.com/gp/product/B00J7SDKSA/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B00J7SDKSA&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=0626591f4e7de8fcb9928ede75f28ed3" target="_blank" rel="noopener">journal</a><img style="border: none !important; margin: 0px !important;" src="//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B00J7SDKSA" alt="" width="1" height="1" border="0" /> to record thoughts and daily events, MongoDB uses a journal to ensure data integrity. This is accomplished through writing dataÂ <em>first</em> to the journal files and <em>then</em>Â to the core data files. In the event of an untimely server shutdown, the data canÂ be restored to a consistent state.

This is accomplished through MongoDB's <a href="https://www.kenwalger.com/blog/nosql/mongodb-crud-with-python/">write operation</a> durabilityÂ guarantee. If your <a href="https://www.kenwalger.com/blog/nosql/mongodb-cli-tools/">mongod</a>Â process stops in an unexpected manner, data from the journal will be used to re-apply the write operations when it is restarted. MongoDB creates, when journaling is enabled, a subdirectory for the journal data called <code>journal</code>.Â This resides under the <code><a href="https://docs.mongodb.com/manual/reference/configuration-options/#storage.dbPath">dbPath</a></code> directory and contains the write ahead logs.

Since each different storage engine in MongoDB implements crash resiliency and data persistenceÂ <em>slightly</em> differently, let's see how journaling is utilized.
<h3>Storage Engine Implementations</h3>
There are three different storage engines that are predominately used with MongoDB. <a href="https://docs.mongodb.com/manual/core/mmapv1/">MMAPv1</a>, <a href="https://docs.mongodb.com/manual/core/wiredtiger/">WiredTiger</a>, and <a href="https://docs.mongodb.com/manual/core/inmemory/">In-Memory</a>. They each have their own strengths and weaknesses. Those differences are beyond the scope of this post, but I would like to look at how journaling is implemented in each.
<h6>MMAPv1</h6>
Starting in version 3.2 of MongoDB, MMAPv1 is no longer the default storage engine. However, it is still in use and in certain circumstances is a better option. Therefore, it is still good to understand how journaling works with this storage engine in its default configuration.

In a nutshell, when a write command is issued, the operation is applied to an internal private view, then written to the journal. Once the data has been updated in the journal the changes are applied to an internal shared view and then written to disk.

In MMAPv1, the journal is updated every 100 milliseconds in batch processes called group commits. Data is written to disk, though, every 60 seconds in the process flushing the shared view to disk. Depending on the quantity and availability of system memory, the flushing of data may occur more often.

Where then does the importance of the journal come in? Well, in the case of an unexpected shutdown of the mongod process the journal can be used to restore the data. Without journaling on a standalone server, there is a more lengthy and involved <a href="https://docs.mongodb.com/manual/reference/program/mongodump/#cmdoption-repair">repair</a> process involved.

On systems using a properly configured <a href="https://docs.mongodb.com/manual/replication/">replica set</a>, data recoveryÂ <em>may</em> be simplified without a journal over using the repair process. It is still not as clean as with journaling enabled, however.
<h6>WiredTiger</h6>
The WiredTiger storage engine takes a different approach to write operation data concurrency. WiredTiger uses checkpoints in conjunction with a journal. These <a href="https://docs.mongodb.com/manual/core/wiredtiger/#storage-wiredtiger-checkpoints">checkpoints</a> allow for data to be recovered after the last checkpoint.

When a write operation is called, a snapshot is taken of the data. When data is written to disk (every 60 seconds by default), the data is written across all data files and becomes durable. This becomes a new checkpoint and can be used as a recovery point.

This allows for WiredTiger to be covered from the last checkpoint without a journal. Pretty slick. However, if an unexpected shutdown occurs between checkpoints and journaling is disabled, data will be lost. The journal in WiredTiger, therefore, utilizes a write-ahead log similar to MMAPv1 between checkpoints for data durability.

So journaling and replica sets are still important pieces of a server environment when using WiredTiger. It just is implemented in a slightly different way than MMAPv1.
<h6>In-Memory</h6>
For those that are running an Enterprise version of MongoDB, there is a storage engine that stores data in memory. Because memory is stored in memory, the data is non-persistent. The concept of a journal does not apply in this situation.
<h3>Study Question</h3>
I have seen questions similar to "Why is the journal unnecessary for WiredTiger" listed in various study guides. As we have learned, it is indeed notÂ <strong>required</strong> for data consistency. At least not in the same fashion as it is for MMAP. That being said, I might argue that "unnecessary" is a bit of a misleading word. WiredTiger's data consistency model is just different than MMAP. Journaling may not be "necessary" perhaps, but I wouldn't run a system without it.
<h3>Wrap Up</h3>
All of these details of journaling can be a lot to think about and potentially manage. This is one of the great advantages of <a href="https://www.kenwalger.com/blog/nosql/mongodb/mongodb-atlas/">MongoDB Atlas</a>, in that these internal matters are handled by them. If you are running and/or managing a MongoDB server, it is a best practice to leave journaling on for data integrity. Further, it is recommended to have your system use a replica set at a minimum as data recovery is often simplified even more.

There are several MongoDB specific terms in this post. I created a <a href="https://www.echoskillstore.com/MongoDB-Dictionary/45103">MongoDB Dictionary</a> skill for the <a href="https://www.amazon.com/gp/product/B01DFKC2SO/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B01DFKC2SO&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=f9e513223de2525a72b95cf9561db55b" rel="noopener noreferrer">Amazon Echo</a>Â line of products. Check it out and you can say "Alexa, ask MongoDB for the definition of a journal?" and get a helpful response.

<hr />


<em>Follow me on Twitter <a href="https://www.twitter.com/kenwalger">@kenwalger</a> to get the latest updates on my postings, or see the original post on my <a href="https://www.kenwalger.com/blog" target="_blank">blog</a>.</em>