Write Concern MongoDB Performance Comparison

In the last post we have MongoDB Official Logoseen the difference among all the available write concerns in MongoDB. We have also seen that which write concern to choose in which scenario that mostly depends on the type of data you are dealing with.

In this post we will see a performance comparison of all the write concerns , in this test i have inserted 100K records for each type of write concerns and captured the time taken for them.

Below are the stats that I got after executing my test case.

Write Concern Performance Stats

Type
Total Time Taken for writing 100,000 records in secs
Writes per second
ERRORS_IGNORED15.096626
UNACKNOWLEDGED15.406491
ACKNOWLEDGED51.911926
JOURNALED4011.3625
FSYNCED3545.2328

It is evident from the stats as we are coming down in the hierarchy the insertion time increasing with the assurance of data is persisted successfully on the disk.
As we have seen in the previous post that ERRORS_IGNORED and UNACKNOWLEDGED are the fire and forgot request and they are fastest one but they have their own downside that acknowledgement won’t be available.Also the insertion time for these 2 write concerns is almost same, with UNACKNOWLEDGED it is little more than ERRORS_IGNORED as it doesn’t suppress network errors and raised them as exceptions.
If we see the time taken under ACKNOWLEDGED write concern it is approximately 3 times higher in comparison to ERRORS_IGNORED and UNACKNOWLEDGED.For JOURNALED and FSYNCED it is around 80 and 70 times higher in comparison to ACKNOWLEDGED respectively.

Note : In the case of JOURNALED and FSYNCED , as per my understanding and the MongoDB doc ,write concern of type FSYSNCED should be the slowest as it waits until the data is not written to the disk and in the case of JOURNALED it waits till it writes to the JOURNAL. I executed the case 4 times for 100K records and every time JOURNALED was the slowest.I am not sure whether this is correct behavior and I am going to ask about it on the relevant forums and will update my findings.

Test Class :

package com.techidiocy.performance.tests;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Logger;

import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;

public class WriteConcernPerformanceTest {
	
	private static final Logger log = Logger.getAnonymousLogger();
	private static final String DATABASE_NAME="performanceDB";
	
	private static final int HUNDRED_THOUSAND=1000;

	@Test
	public void test() throws UnknownHostException {		
		errorIgnored();
		unacknowledged();
		acknowledged();
		journaled();
		fsynced();
	}	
	
	private void errorIgnored() throws UnknownHostException{
		DBCollection collection = getCollection(DATABASE_NAME, WriteConcern.ERRORS_IGNORED);		
		log.info("ERRORS_IGNORED Insertion Time --"+insertRecords(collection));
	}
	
	private void unacknowledged() throws UnknownHostException{
		DBCollection collection = getCollection(DATABASE_NAME, WriteConcern.UNACKNOWLEDGED );
		log.info("UNACKNOWLEDGED Insertion Time --"+insertRecords(collection));
	}
	
	private void acknowledged() throws UnknownHostException{
		DBCollection collection = getCollection(DATABASE_NAME, WriteConcern.ACKNOWLEDGED );
		log.info("ACKNOWLEDGED Insertion Time --"+insertRecords(collection));
	}
	
	private void journaled() throws UnknownHostException{
		DBCollection collection = getCollection(DATABASE_NAME, WriteConcern.JOURNALED );
		log.info("JOURNALED Insertion Time --"+insertRecords(collection));
	}
	
	private void fsynced() throws UnknownHostException{
		DBCollection collection = getCollection(DATABASE_NAME, WriteConcern.FSYNCED );
		log.info("FSYNCED Insertion Time --"+insertRecords(collection));
	}
	
	private long insertRecords(DBCollection collection) {
		long startTime=System.currentTimeMillis();
		for(int i=0;i<HUNDRED_THOUSAND;i++){
			DBObject myObject = new BasicDBObject();
			myObject.put("someKey", "someValue");
			collection.insert(myObject);
		}
		long endTime=System.currentTimeMillis();
		collection.drop();
		return (endTime-startTime);
	}


	private DBCollection getCollection(String databaseName,WriteConcern writeConcern) throws UnknownHostException{
		MongoClient serverConnection = new MongoClient("localhost",27017);
		serverConnection.setWriteConcern(writeConcern);
		return serverConnection.getDB(databaseName).getCollection("yoMongo");
	}	

}

Image Credits : http://mongodb.org


 

Write Concern MongoDB Performance Comparison

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
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>