Src:http://www.badrit.com/blog/2013/11/18/redis-vs-mongodb-performance#.VY2-qROqpBc

MongoDB is an open source document database, and the leading NoSQL database which is written in C++ and Redis is also an open source NoSQL database but it is key-value store rather than document database. Redis is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

Here’s a simple benchmark in node.js to compare the performance between Redis and MongoDB. The benchmark compares the time of writing and reading for both. For Redis I used node.js Redis client and for MongoDB I used node.js MongoDB driver

And here’s the code

In case of redis:

var redis = require("redis") 
,	client = redis.createClient() 
,	numberOfElements = 50000; 

client.del({},function(err,reply){ 
	redisWrite(); 
}); 

function redisWrite () { 
 console.time('redisWrite'); 
   for (var i = 0; i < numberOfElements; i++) { 
	 client.set(i, "some fantastic value " + i,function(err,data){ 
	   if (--i === 0) { 
	       console.timeEnd('redisWrite'); 
	       redisRead(); 
	       } 
	 }); 
   }; 
} 

function redisRead(){ 
 client = redis.createClient(); 
 console.time('redisRead'); 
 for (var i = 0; i < numberOfElements; i++) { 
       client.get(i, function (err, reply) { 
         if (--i === 0) { 
	     console.timeEnd('redisRead'); 
	     } 
       }); 
 } 
}

In case of MongoDB:

var MongoClient = require('mongodb').MongoClient 
, numberOfElements=50000;    

MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) { 
   var collection = db.collection('benchmark'); 
   collection.ensureIndex({id:1},{} ,console.log); 
   collection.remove({}, function(err) { // to remove any element from the database at first 
   mongoWrite(collection,db); 
   }); 
}) 

function mongoWrite(collection,db){ 
  console.time('mongoWrite'); 
  for (var i = 0; i < numberOfElements; i++) { 
        collection.insert({id:i,value:"some fantastic value " + i}, function(err, docs) { 
          if(--i==0){ 
             console.timeEnd('mongoWrite'); 
             mongoRead(collection,db); 
          }      
        }); 
  }; 
} 


function mongoRead(collection,db){ 
 console.time('mongoRead'); 
 for (var i = 0; i < numberOfElements; i++) { 
       collection.findOne({id:i},function(err, results) { 
         if(--i==0){ 
            console.timeEnd('mongoRead'); 
            db.close(); 
            } 
       }); 
 } 
}

Results were measured using MongoDB 2.4.8 and Redis 2.6.16

Machine Specifications

  • Processor : 4x Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz
  • Memory : 3892MB
  • Operating System : Ubuntu 13.04
  • Kernel -Version : Linux 3.8.0-33-generic (x86_64)
Redis Read Mongo Read Redis Write Mongo Write
10 2 5 5 8
100 13 11 8 34
1,000 38 93 31 153
10,000 238 980 220 1394
50,000 958 5218 979 8713

Calculated time in milliseconds (lower is better)


Form results we can see that both Mongo and Redis have almost equal time in case of small number of entries but when this number increases, Redis has remarkable superiority over mongo.

The results will vary according to your programming language and also according to the specifications of your machine.

Advertisements