One of coolest features of MongoDB that we discussed and used in earlier chapters, is its support for fast asynchronous writes. MongoDB achieves this speed by using memory-mapped files. A memory-mapped file is a data structure that represents a file in main memory the same way it is stored on the disk. Processes accessing such a file can treat it as a part of memory, speeding up the I/O performance. MongoDB uses memory-mapped files to perform disk I/O operations. When a document is loaded in the application, MongoDB transparently loads it from the disk to memory. Any writes to the document results in writing to the appropriate address in memory. MongoDB flushes the data in memory back to the disk every 60 seconds (which is the default interval between two successive flushes. It is configurable through the --syncdelay
command-line option).
The problem with this approach is that if the system crashes even 1 millisecond before the flush, we will lose all the data since...