• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Enable MongoDB Journalling

April 25, 2014 //  by Krishna Srinivasan//  Leave a Comment

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.

Category: MongoDBTag: MongoDB Journal

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Count Files In A Directory Using Java
Next Post: Spring Data MongoDB Tutorial »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact