Types of Write Concern in MongoDB – Performance Vs Persistence

[This post will discuss about different types of write concern in MongoDB – Performance Vs Persistence.]
Controlling the write MongoDB Official Logooperations in an application is always an important aspect. Today almost all the frameworks that interact with datastores provide mechanism to let you know whether your write operation is successful or not. Now when we talk about in the context of MongoDB , it also provides you an ability called as Write Concern to control write operations.
Write Concern gives you the flexibility to set the acknowledgement level that means whether you want to know or wait for the outcome of your write operation or not.

Here, write operation means insert,update and delete.

For example In your application you don’t want to track each and every write operation whether it is successful or not because may be data that you are writing is not important for you like some logging data or on other side you want to make sure, data that you are sending in write operation should be written on the disk and you need an acknowledgment from the server.
Depending on the need of your application you can choose one of the Write Concern available in MongoDB. More stronger is the write concern ,more guaranteed is the write success and less the performance of your write operation.
Below is a list of Write Concerns that are available in mongo-java-driver since version 2.10.0 with least powerful at the top.

  1. ERRORS_IGNORED : All the write operations under this write concern are like fire and forget requests that means client here doesn’t wait for acknowledgement. In this case it will just go to the driver writes their and comeback and it won’t tell you anything after that. It is quite possible after that data might be lost because of any exception or errors. Moreover it also suppresses all the server side exceptions even network related errors. This write concern is only recommended for the data that is not crucial to your application like some temporary logs.
  2. UNACKNOWLEDGED : It works in almost same way as ERRORS_IGNORED with only difference it raises an exception for all the network related issues but not for the server side errors. Again, this write concern can’t be relied upon for the successful write operations.
  3. ACKNOWLEDGED : This is the default Write Concern for Mongo shell and all the available client drivers. Clients write operations under this write concern will wait till the acknowledgment is received from the primary server. MongoDB server acknowledges as soon as it receives the data but at this moment data is not written to the disk. Exceptions will be raised for all the network related issues and server errors.
  4. JOURNALED : This is an advanced version of ACKNOWLEDGED write concern where client will wait for acknowledgement until the data is written to the Journal file. Journal file is used for data recovery in the cases when mongo server crashes. It is much more reliable than ACKNOWLEDGED write concern but it impacts the performance.
    Note : This write concern can only be used if Journaling is enabled in your mongo server instance.
  5. FSYNCED : A more powerful write concern than JOURNALED ,where client will wait for the acknowledgement until the data is written to the disk. This write concern ensures that data is successfully persisted in mongodb server. It raises exceptions for all the network related issue and server errors.
  6. REPLICA_ACKNOWLEDGED : If you are storing the data on multiple replicas in a replica set,then this write concern will ensure that data has been received by primary server and at least one of the secondary server.

There are other write concerns also but most of them have been replaced by those we have discussed above and will deprecate in future.
Again choosing the correct write concern is very important for the consistency and performance of your application. Mostly Performance Vs Persistence depends on the type of data you are dealing with.

Write Concern can be set at connection level or at a particular individual operation level also.
Connection Level:

MongoClient serverConnection = new MongoClient("localhost",27017);
serverConnection.setWriteConcern(WriteConcern.FSYNCED);

Operation Level :

someCollection.update(searchObject, modifiedObject,false,false,WriteConcern.FSYNCED);

Here , I have set the write concern to FSYNCED both at connection and operation level.

I have done some performance testing also for these Write Concerns and I will write a separate post on my findings.

Image Credits : http://mongodb.org

Types of Write Concern in MongoDB – Performance Vs Persistence

Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

One thought on “Types of Write Concern in MongoDB – Performance Vs Persistence

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>