Thursday, February 12, 2015

MongoDB Journal concepts

                 People started exploring MongoDB.The one of the best feature that mongodb has unique when compared to other databases is Journal.In this section we are going to see what is a mongodb journal,how to enable or disable it and how it works.

What is a Journal in Mongodb ?
            A sequential, binary transaction log used to bring the database into a valid state in the event of a hard shutdown. Journaling writes data first to the journal and then to the core data files. MongoDB enables journaling by default for 64-bit builds of MongoDB version 2.0 and newer. Journal files are pre-allocated and exist as files in the data directory.
                    Journal can also bring the database to a valid state even when it shutdowns.Journal first write the data into it and then to the core files.
How to enable Journal?
           MongoDB version 2.0 and newer versions have journal enabled by default.To enable journaling, start mongod with the --journal command line option.For example:
./mongod --port 27017 --journal
            If no journal files exist, when mongod starts, it must pre allocate new journal files. During this operation, the mongod is not listening for connections until preallocation completes: for some systems this may take a several minutes. During this period your applications and the mongo shell are not available.
How to disable Journal?
            Disabling journal is not advised in the production systems.Suppose due to some power failure the mongod has gone unclean shutdown then in that case the data could be backup only if it journaling is enabled.To disable journaling, start mongod with the --nojournal command line option.For example:
./mongod --port 27017 --nojournal
Views in Journaling:
            The shared view stores modified data for upload to the MongoDB data files. The shared view is the only view with direct access to the MongoDB data files. When running with journaling,mongod asks the operating system to map your existing on-disk data files to the shared view virtual memory view. The operating system maps the files but does not load them. MongoDB later loads data files into the shared view as needed.
            The private view stores data for use with read operations.The private view is the first place MongoDB applies new write operations. Upon a journal commit, MongoDB copies the changes made in the private view to the shared view,where they are then available for uploading to the database data files.
            The journal is an on-disk view that stores new write operations after MongoDB applies the operation to the private view but before applying them to the data files. The journal provides durability. If the mongod instance were to crash without having applied the writes to the data files, the journal could replay the writes to the shared view for eventual upload to the data files.
How mongodb works without journaling?
                  Generally the mongodb consists of two views such as Shared view and Private view.If the mongodb works journaling disabled it uses only the Shared view.When the mongod server starts it maps the data files to the shared view by memory mapping.If you need to read a memory at some memory address then you can get that address but you cannot actually access that address.Also if you make any changes in the memory the operating system will flush them for every 60 seconds.

How Journaling works?
              The mongodb with journaling enabled has two views such as Shared view and Private view.In journaling private view is not connected to the data file so that the operating system cannot flush out any changes from private view to the disk.When a write operation is carried way then the mongod writes it to the private view.

            Then the mongod will make the change to the journal with a small description what change has made within.The journal will keep the list of what all the changes made in the file.
             If there is any power failure or unclean shutdown in the system then the mongod then it will move the changes to the shared view.

             With a small comparative speed the shared view will flush them to the data file.Hence if there is any crash in the mongod then it is easy to repair or recover the lost data.

              Finally,the remap takes place between the Shared view and Private view so that the data in them will always remain in sync.
This is how journal in the mongodb works.Hope it helps :)




0 comments:

Post a Comment