- Reference >
- Database Commands >
- fsync
fsync¶
-
fsync
¶ The
fsync
command forces themongod
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 thesyncdelay
interval, which is 60 seconds by default.The
fsync
operation is synchronous by default, to runfsync
asynchronously, use the following form:The connection will return immediately. You can check the output of
db.currentOp()
for the status of thefsync
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: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:After loading this function into your
mongo
shell session you can call it as follows:This function will return
true
if the database is currently locked andfalse
if 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
mongo
shell, you may use thedb.fsyncLock()
anddb.fsyncUnlock()
wrappers for thefsync
lock and unlock process: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 withdb.fsyncLock()
. Disable profiling usingdb.setProfilingLevel()
as follows in themongo
shell: