OPTIONS

fsync

Definition

fsync

Forces the mongod process to flush all pending writes from the storage layer to disk. Optionally, you can use fsync to lock the mongod instance and block write operations for the purpose of capturing backups.

As applications write data, MongoDB records the data in the storage layer and then writes the data to disk within the syncPeriodSecs interval, which is 60 seconds by default. Run fsync when you want to flush writes to disk ahead of that interval.

The fsync command has the following syntax:

{ fsync: 1, async: <Boolean>, lock: <Boolean> }

The fsync command has the following fields:

Field Type Description
fsync integer Enter “1” to apply fsync.
async Boolean Optional. Runs fsync asynchronously. By default, the fsync operation is synchronous.
lock Boolean Optional. Locks mongod instance and blocks all write operations.

Behavior

An fsync lock is only possible on individual mongod instances of a sharded cluster, not on the entire cluster. To backup an entire sharded cluster, please see Backup and Restore Sharded Clusters for more information.

If your mongod has journaling enabled, consider using another method to create a back up of the data set.

After fsync, with lock, runs on a mongod, all write operations will block until a subsequent unlock. Read operations may also block. As a result, fsync, with lock, is not a reliable mechanism for making a mongod instance operate in a read-only mode.

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)

Examples

Run Asynchronously

The fsync operation is synchronous by default To run fsync asynchronously, use the async field set to true:

{ fsync: 1, async: true }

The operation returns immediately. To view the status of the fsync operation, check the output of db.currentOp().

Lock mongod Instance

The primary use of fsync is to lock the mongod instance in order to back up the files withing mongod‘s dbPath. The operation flushes all data to the storage layer and blocks all write operations until you unlock the mongod instance.

To lock the database, use the lock field set to true:

{ fsync: 1, lock: true }

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

Check Lock Status

To check the state of the fsync lock, use db.currentOp(). Use the following JavaScript function in the shell to test if mongod instance 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 call it, with the following syntax:

serverIsLocked()

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

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

Unlock mongod Instance

To unlock the mongod instance, use db.fsyncUnlock():

db.fsyncUnlock();
←   dropIndexes clean  →