简体   繁体   中英

MongoDB performance in SAFE mode

在Java驱动程序中将WriteConcern标志设置为SAFE如何影响MongoDB的性能?

Slows it down significantly (as you would expect).

Without the "SAFE" flag, MongoDB drivers operate in "Fire & Forget" mode. So the update command is sent to the server and then the driver continues on. If there is an error with the write or the server dies before the change happens, the client knows nothing about it.

With the "SAFE" flag, the drivers do both the update command and the getLastError() command. That second command will not complete until the update actually happens on the DB. At the very least, you are sending two commands instead of one ( so it's 50% slower ).

In my experience it's actually 5x-20x slower. Of course, this makes sense because actually writing the data is the slow part of this whole piece.

Note that without the SAFE flag, certain exceptions will never happen. For example, you will never get a duplicate key exception.

If you plan to use MongoDB as a typical database (analogous to say MySQL), you need to use at least "SAFE" mode and replica sets. Otherwise, you need "JOURNAL" mode with a single box. I you use JOURNAL mode, you will notice that performance starts to look at lot like regular SQL.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM