Why journaling use so much space in mongodb?

I have a mongodb database that uses approximately 1GB of space. I was surprised when my server ran out of space, and then I realized that the journal uses 3GB.

The documentation says that "Once MongoDB applies all the write operations in the journal files, it deletes these files", but that does not seems to be the case, since the database is synced and journaling is 3 times bigger than whole database.

How can I have those journaling files deleted and configure the system to use less space?


Solution 1:

The documentation you quote is correct, if you run the db.fsyncLock() command you will flush all changes to disk and the journal files will be deleted. As soon as you run db.fsyncUnlock() and another write arrives, those journal files will reappear:

Here's a quick example on a test system:

> db.foo.insert({"id" : 1})

Here's what the journal directory looks like:

$ls /usr/local/var/mongodb/journal/
j._1 lsn
$

Now, let's lock it up and flush everything to disk:

> db.fsyncLock()
{
    "info" : "now locked against writes, use db.fsyncUnlock() to unlock",
    "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
    "ok" : 1
}

The journal directory is now empty:

$ls /usr/local/var/mongodb/journal/
$

Unlock, insert:

> db.fsyncUnlock()
{ "ok" : 1, "info" : "unlock completed" }
> db.foo.insert({"id" : 1})

Now we have journal files again:

$ls /usr/local/var/mongodb/journal/
j._2 lsn
$

Hence, if you are not locking, flushing the databases to disk and constantly have writes coming in you will always have journal files.

Similarly, when you shut down a mongod instance cleanly, the journal files will be deleted once the flush to disk has completed successfully.

If you would like to make the journal files smaller, you can look into running with the --smallfiles option. This will give you 3 x 128MB files rather than 3 x 1GB files.