I've been using MongoDB for some time, and saw that fsync
waits for data to be flushed to disk. Ok, so i thought it was the solution for safety of the data.
It worked well by takes long, longer than SQL alternative. Then I saw that I can put the syncdelay
to 0
, then speed came back, but I thought how it would be in the future with many many concurrent requests. So I removed fsync
option from the updates and inserts and removed the syncdelay
configuration option.
To test if the data was being written I quickly checked Rockmongo after I made an update and the data was actually there, super fast!
So really, what is fsync
for if it makes the writes slow and without it the writes happen, and fast anyway?
Per Mongo documentation:
The primary use of
fsync
is to flush and lock the database for backups.
also
The
fsync
operation blocks all other write operations while its running.
The blocking appears to be the reason.
fsync
is technically an admin command that forces a flush of all data to disk. You shouldn't have to use it in your code, not normally at least. It's used to lock the database for backups and so on.
Data safety in MongoDB comes from replication/sharding/journaling, not from forcing writes. That kind of defeats the purpose of the thing.
The Java driver wraps this 'write-and-sync' concept in the WriteConcern
class, which I've never really liked much. You shouldn't have to decide which part of your data is more or less important, but rather just trust the tool to do its job.
Also, if you set syncdelay
to zero make sure you turn journaling off. See this .
As the other answers have said, the fsync
command forces a flush and is normally used right before you lock data files for a point in time snapshot.
There is an "fsync" write concern option on getLastError
that will wait to return into all pending data has been flushed to disk. You normally wouldn't use this though, the "j" option (which returns as soon as journaling has happened) is much faster to return and still ensures durable writes. You can pass either through an update/insert command as the safe option in your driver of choice to let it automatically run the getLastError
command for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.