Navigation
This version of the documentation is archived and no longer supported.

fsync

fsync

The fsync command forces the mongod process to flush all pending writes to the storage layer. mongod is always writing data to the storage layer as applications write more data to the database. MongoDB guarantees that it will write all data to disk within the syncdelay interval, which is 60 seconds by default.

{ fsync: 1 }

The fsync operation is synchronous by default, to run fsync asynchronously, use the following form:

{ fsync: 1, async: true }

The connection will return immediately. You can check the output of db.currentOp() for the status of the fsync operation.

The primary use of fsync is to lock the database during backup operations. This will flush all data to the data storage layer and block all write operations until you unlock the database. Consider the following command form:

{ fsync: 1, lock: true }

Note

You may continue to perform read operations on a database that has a fsync lock. However, following the first write operation all subsequent read operations wait until you unlock the database.

To check on the current state of the fsync lock, use db.currentOp(). Use the following JavaScript function in the shell to test if the database is currently locked:

serverIsLocked = function () {
                     var co = db.currentOp();
                     if (co && co.fsyncLock) {
                         return true;
                     }
                     return false;
                 }

After loading this function into your mongo shell session you can call it as follows:

serverIsLocked()

This function will return true if the database is currently locked and false if the database is not locked. To unlock the database, make a request for an unlock using the following command:

db.getSiblingDB("admin").$cmd.sys.unlock.findOne();

New in version 1.9.0: The db.fsyncLock() and db.fsyncUnlock() helpers in the shell.

In the mongo shell, you may use the db.fsyncLock() and db.fsyncUnlock() wrappers for the fsync lock and unlock process:

db.fsyncLock();
db.fsyncUnlock();

Note

fsync lock is only possible on individual shards of a sharded cluster, not on the entire sharded cluster. To backup an entire sharded cluster, please read Sharded Cluster Backup Considerations.

If your mongod has journaling enabled, consider using another method to back up your database.

Note

The database cannot be locked with db.fsyncLock() while profiling is enabled. You must disable profiling before locking the database with db.fsyncLock(). Disable profiling using db.setProfilingLevel() as follows in the mongo shell:

db.setProfilingLevel(0)
←   flushRouterConfig geoNear  →