Redis 比 mongoDB 快多少? [英] How much faster is Redis than mongoDB?

查看:25
本文介绍了Redis 比 mongoDB 快多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们普遍提到 Redis 极快",而 mongoDB 也很快.但是,我很难找到比较两者结果的实际数字.给定相似的配置、特性和操作(并且可能显示因子如何随着不同的配置和操作而变化)等,Redis 是快 10 倍?快 2 倍?还是快 5 倍?

It's widely mentioned that Redis is "Blazing Fast" and mongoDB is fast too. But, I'm having trouble finding actual numbers comparing the results of the two. Given similar configurations, features and operations (and maybe showing how the factor changes with different configurations and operations), etc, is Redis 10x faster?, 2x faster?, 5x faster?

我只说性能.我知道 mongoDB 是一种不同的工具,具有更丰富的功能集.这不是mongoDB 是否比 Redis 更好"的争论.我在问,Redis 在多大程度上优于 mongoDB?

I'm ONLY speaking of performance. I understand that mongoDB is a different tool and has a richer feature set. This is not the "Is mongoDB better than Redis" debate. I'm asking, by what margin does Redis outperform mongoDB?

在这一点上,即使是廉价的基准测试也比没有基准测试要好.

At this point, even cheap benchmarks are better than no benchmarks.

推荐答案

以下基准的粗略结果:2x 写入,3x 读取.

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

这是一个简单的 Python 基准测试,您可以根据自己的目的进行调整,我正在研究每个基准测试在简单设置/检索值方面的表现:

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.7
import sys, time
from pymongo import Connection
import redis

# connect to redis & mongodb
redis = redis.Redis()
mongo = Connection().test
collection = 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)

使用 mongodb 1.8.1 和 redis 2.2.5 以及最新的 pymongo/redis-py 的结果:

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

$ ./cache_benchmark.py 10000
Completed mongo_set: 10000 ops in 1.40 seconds : 7167.6 ops/sec
Completed mongo_get: 10000 ops in 2.38 seconds : 4206.2 ops/sec
Completed redis_set: 10000 ops in 0.78 seconds : 12752.6 ops/sec
Completed 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!

这篇关于Redis 比 mongoDB 快多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆