Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB performance in SAFE mode

How does setting the WriteConcern flag to SAFE in the java driver affects MongoDB performances?

like image 453
Rand Avatar asked Jan 31 '26 20:01

Rand


1 Answers

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.

like image 175
Gates VP Avatar answered Feb 02 '26 09:02

Gates VP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!