How does setting the WriteConcern flag to SAFE in the java driver affects MongoDB performances?
How does setting the WriteConcern flag to SAFE in the java driver affects MongoDB
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Slows it down significantly (as you would expect).
Without the “SAFE” flag, MongoDB drivers operate in “Fire & Forget” mode. So the
updatecommand 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
updatecommand and thegetLastError()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.