How much faster is Redis than mongoDB? How much faster is Redis than mongoDB? mongodb mongodb

How much faster is Redis than mongoDB?


Rough results from the following benchmark: 2x write, 3x read.

Here's a simple benchmark in python you can adapt to your purposes, I was looking at how well each would perform simply setting/retrieving values:

#!/usr/bin/env python2.7import sys, timefrom pymongo import Connectionimport redis# connect to redis & mongodbredis = redis.Redis()mongo = Connection().testcollection = mongo['test']collection.ensure_index('key', unique=True)def mongo_set(data):    for k, v in data.iteritems():        collection.insert({'key': k, 'value': v})def mongo_get(data):    for k in data.iterkeys():        val = collection.find_one({'key': k}, fields=('value',)).get('value')def redis_set(data):    for k, v in data.iteritems():        redis.set(k, v)def redis_get(data):    for k in data.iterkeys():        val = redis.get(k)def do_tests(num, tests):    # setup dict with key/values to retrieve    data = {'key' + str(i): 'val' + str(i)*100 for i in range(num)}    # run tests    for test in tests:        start = time.time()        test(data)        elapsed = time.time() - start        print "Completed %s: %d ops in %.2f seconds : %.1f ops/sec" % (test.__name__, num, elapsed, num / elapsed)if __name__ == '__main__':    num = 1000 if len(sys.argv) == 1 else int(sys.argv[1])    tests = [mongo_set, mongo_get, redis_set, redis_get] # order of tests is significant here!    do_tests(num, tests)

Results for with mongodb 1.8.1 and redis 2.2.5 and latest pymongo/redis-py:

$ ./cache_benchmark.py 10000Completed mongo_set: 10000 ops in 1.40 seconds : 7167.6 ops/secCompleted mongo_get: 10000 ops in 2.38 seconds : 4206.2 ops/secCompleted redis_set: 10000 ops in 0.78 seconds : 12752.6 ops/secCompleted redis_get: 10000 ops in 0.89 seconds : 11277.0 ops/sec

Take the results with a grain of salt of course! If you are programming in another language, using other clients/different implementations, etc your results will vary wildy. Not to mention your usage will be completely different! Your best bet is to benchmark them yourself, in precisely the manner you are intending to use them. As a corollary you'll probably figure out the best way to make use of each. Always benchmark for yourself!


Please check this post about Redis and MongoDB insertion performance analysis:

Up to 5000 entries mongodb $push is faster even when compared to Redis RPUSH, then it becames incredibly slow, probably the mongodb array type has linear insertion time and so it becomes slower and slower. mongodb might gain a bit of performances by exposing a constant time insertion list type, but even with the linear time array type (which can guarantee constant time look-up) it has its applications for small sets of data.


Good and simple benchmark

I tried to recalculate the results again using the current versions of redis(2.6.16) and mongo(2.4.8) and here's the result

Completed mongo_set: 100000 ops in 5.23 seconds : 19134.6 ops/secCompleted mongo_get: 100000 ops in 36.98 seconds : 2703.9 ops/secCompleted redis_set: 100000 ops in 6.50 seconds : 15389.4 ops/secCompleted redis_get: 100000 ops in 5.59 seconds : 17896.3 ops/sec

Also this blog post compares both of them but using node.js. It shows the effect of increasing number of entries in the database along with time.