- Reference >
- Database Commands >
- fsync
fsync¶
-
fsync¶ The
fsynccommand forces themongodprocess to flush all pending writes to the storage layer.mongodis 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 thesyncdelayinterval, which is 60 seconds by default.The
fsyncoperation is synchronous by default, to runfsyncasynchronously, use the following form:The connection will return immediately. You can check the output of
db.currentOp()for the status of thefsyncoperation.The primary use of
fsyncis 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:Note
You may continue to perform read operations on a database that has a
fsynclock. 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:After loading this function into your
mongoshell session you can call it as follows:This function will return
trueif the database is currently locked andfalseif the database is not locked. To unlock the database, make a request for an unlock using the following command:New in version 1.9.0: The
db.fsyncLock()anddb.fsyncUnlock()helpers in the shell.In the
mongoshell, you may use thedb.fsyncLock()anddb.fsyncUnlock()wrappers for thefsynclock and unlock process:Note
fsynclock 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
mongodhas 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 withdb.fsyncLock(). Disable profiling usingdb.setProfilingLevel()as follows in themongoshell: