Over a million developers have joined DZone.

Understanding durability & write safety in MongoDB

DZone's Guide to

Understanding durability & write safety in MongoDB

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

Durability is the "D" in the "ACID" properties popularized by traditional RDBMS. Durability is the guarantee that written data has been saved and will survive permanently. NoSQL databases like MongoDB give developers fine grained control over the durability of their write calls. This enables developers to choose different durability, safety and performance models for different classes of data. However this also places the burden on the developer to discern and understand the nuances of the different write safety options. In this blog post we will look at the different options for write safety provided in the Java driver. In MongoDB parlance this is called "Write Concern". Write concerns vary from "weak" to "strong". Weak writes concerns can lead to higher throughput but provide less data safety and strong write concerns are vice versa.

The java driver allows you to specify your write safety options using several telescoping constructors. Here is the constructor with all the options:

WriteConcern(int w, int wtimeout, boolean fsync, boolean j, boolean continueOnError)

As you can see this constructor has a lot of options - in order to make it easier for developers "Tags" are provided for common write concern values - Unacknowledged, Acknowledged, Journalled, Fsynced & Replica Acknowledged. Each tag maps to a certain invocation of the above constructor


This is the "fire and forget" mode. The MongoDB driver does not attempt to acknowledge the receipt of write operations. E.g. If your mongodb service is down and you are using this mode all the errors are silently ignored and your data is lost. Obviously you should be only using this mode for low value data where write throughput is more important that loss of certain amount of data. This mode can be specified as follows:

new WriteConcern(0) / WriteConcern.UNACKNOWLEDGED


This is the default write mode for MongoDB. In this mode the MongoDB driver attempts to acknowledge the receipt of write operations on the server.  This will allow the driver to catch any network errors, duplicate keys errors etc. However this does not guarantee that data is saved on the disk. If the mongodb server crashes after acknowledging the write but before committing it to disk the data is lost. This mode can be specified as follows:

new WriteConcern(1) / WriteConcern.ACKNOWLEDGED


In this mode the mongodb server acknowledges the write only after committing the data to the journal.  In this server even if the server crashes, on server restart the data is reapplied from the journal. Obviously journalling needs to be enabled for this to work. All production systems should have journalling enabled, our other blog post Should you enable MongoDB journalling? has more details. In a replica set scenario the journalling write concerns only apply to the primary. By default the journal is committed to disk every 100 ms. When you specify a write with the journalled option the journal is committed to disk in 30ms. So if you specify j:true for every write your throughput will be a maximum of 1000/30 = 33.3 writes/sec. If you want better throughput you will need to batch your updates and set j:true for the last update of the batch. This mode can be specified as follows:

WriteConcern( 1, 0, false, true ) / WriteConcern.JOURNALLED


In this mode the mongodb server acknowledges the write only after the write is written to disk. This mode can be specified as follows:

new WriteConcern(true) / WriteConcern.FSYNCED

Replica acknowledged

The previous write safety modes apply only to a single server. When you run replica sets you have the option of controlling on how many replicas your write needs to be written before it is considered successful. E.g. With a write concern of "w:2" the write needs to be written to one primary and atleast one secondary before it is considered successful. This reduces the throughput but gives you better safety. If you are not aware of the number of replicas beforehand you can use the WriteConcern.MAJORITY tag to ensure that the data is saved in the majority of the replicas. This is the safest option in mongodb. If you are going to use this option also make sure to set the "wtimeout" value to indicate how long the command should wait before returning failure

new Majority()/ WriteConcern.MAJORITY

The following tags have been deprecated (or plan to be) - ERRORS_IGNORED, NORMAL, SAFE, FSYNC_SAFE, JOURNAL_SAFE, REPLICAS_SAFE. Please use the newer options instead of these options.

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

java ,nosql ,tips and tricks ,mongodb ,durability ,write safety ,write concern

Published at DZone with permission of Dharshan Rangegowda, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}