{"id":437,"date":"2017-07-14T10:22:42","date_gmt":"2017-07-14T17:22:42","guid":{"rendered":"http:\/\/www.kenwalger.com\/blog\/?p=437"},"modified":"2017-07-14T10:33:00","modified_gmt":"2017-07-14T17:33:00","slug":"mongodb-storage-engine-journaling","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/mongodb-storage-engine-journaling\/","title":{"rendered":"MongoDB Storage Engine Journaling"},"content":{"rendered":"<p>I came across a question the other day as it relates to journaling in <a href=\"https:\/\/www.mongodb.com\">MongoDB<\/a>. Specifically\u00a0how 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.<\/p>\n<p>To start with, some questions arise. What exactly is a MongoDB journal? Why is journaling important? For the sake of this post, I&#8217;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.<\/p>\n<h3>What is Journaling?<\/h3>\n<p>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 loading=\"lazy\" decoding=\"async\" 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\u00a0<em>first<\/em> to the journal files and <em>then<\/em>\u00a0to the core data files. In the event of an untimely server shutdown, the data can\u00a0be restored to a consistent state.<\/p>\n<p>This is accomplished through MongoDB&#8217;s <a href=\"http:\/\/www.kenwalger.com\/blog\/nosql\/mongodb-crud-with-python\/\">write operation<\/a> durability\u00a0guarantee. If your <a href=\"http:\/\/www.kenwalger.com\/blog\/nosql\/mongodb-cli-tools\/\">mongod<\/a>\u00a0process 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>.\u00a0This 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.<\/p>\n<p>Since each different storage engine in MongoDB implements crash resiliency and data persistence\u00a0<em>slightly<\/em> differently, let&#8217;s see how journaling is utilized.<\/p>\n<h3>Storage Engine Implementations<\/h3>\n<p>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.<\/p>\n<h6>MMAPv1<\/h6>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>On systems using a properly configured <a href=\"https:\/\/docs.mongodb.com\/manual\/replication\/\">replica set<\/a>, data recovery\u00a0<em>may<\/em> be simplified without a journal over using the repair process. It is still not as clean as with journaling enabled, however.<\/p>\n<h6>WiredTiger<\/h6>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<h6>In-Memory<\/h6>\n<p>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.<\/p>\n<h3>Study Question<\/h3>\n<p>I have seen questions similar to &#8220;Why is the journal unnecessary for WiredTiger&#8221; listed in various study guides. As we have learned, it is indeed not\u00a0<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 &#8220;unnecessary&#8221; is a bit of a misleading word. WiredTiger&#8217;s data consistency model is just different than MMAP. Journaling may not be &#8220;necessary&#8221; perhaps, but I wouldn&#8217;t run a system without it.<\/p>\n<h3>Wrap Up<\/h3>\n<p>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=\"http:\/\/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.<\/p>\n<p>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>\u00a0line of products. Check it out and you can say &#8220;Alexa, ask MongoDB for the definition of a journal?&#8221; and get a helpful response.<\/p>\n<hr \/>\n<p><em>Follow me on Twitter <a href=\"https:\/\/www.twitter.com\/kenwalger\">@kenwalger<\/a> to get the latest updates on my postings.<\/em><\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F437&#038;t=MongoDB%20Storage%20Engine%20Journaling&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F437&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fi0.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F07%2Ffeature-e1499974173210.png%3Ffit%3D125%252C125%26ssl%3D1&#038;p&#091;title&#093;=MongoDB%20Storage%20Engine%20Journaling\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F437&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F437&#038;title=MongoDB%20Storage%20Engine%20Journaling\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F437&#038;title=MongoDB%20Storage%20Engine%20Journaling\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=MongoDB%20Storage%20Engine%20Journaling&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F437\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>I came across a question the other day as it relates to journaling in MongoDB. Specifically\u00a0how 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. &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/mongodb-storage-engine-journaling\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MongoDB Storage Engine Journaling&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[86,84,85,83],"yst_prominent_words":[601,599,99,593,605,597,596,602,577,574,608,87,607,603,606,578,604,584,600,598],"class_list":["post-437","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-journaling","tag-mmapv1","tag-storage-engines","tag-wiredtiger","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/07\/feature-e1499974173210.png?fit=125%2C125&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-73","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=437"}],"version-history":[{"count":5,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":467,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/437\/revisions\/467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media\/455"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=437"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}