Mongodb VS Redis - 性能基准 - 哪个数据库应该用于缓存?

use*_*887 2 caching mongodb redis mongodb-java

我正在努力最终确定我们的应用程序需要使用的缓存层。目前我们已经入围了 2 个 Redis 和 Mongodb。我不确定使用哪一个作为缓存。因此,我想到对两者进行性能测试,然后根据结果进行比较。

现在,我已经阅读了很多关于各自优点的文章,看起来推荐的方法是使用 Mongodb 作为数据存储层,使用 Redis 作为缓存层,它位于 Web 应用程序的前面,以避免请求到达源站。

我在下面分享了结果。

但根据我的性能结果,redis 的性能与 mongodb 相差甚远。那么我们可以有把握地说 mongodb 是比 redis 更好的缓存选择吗?

请告诉我你们的想法。另外,我不是 Redis 或 mongodb 方面的专家,所以如果我在 Redis 或 mongodb 中做错了什么,请告诉我。

我可以修复并再次重做测试。

主机配置:

  • Windows 10 专业版
  • 英特尔酷睿 i5 2520M CPU 2.50Ghz
  • 内存 16GB

在此输入图像描述

Redis 版本:3.2.1(Windows 64 位版本) Mongo 版本:3.2.5(Windows 64 位版本)

Redis.java

package com.redis.mogo.perftest;

import redis.clients.jedis.Jedis;

public class RedisJava {

    public static void main(String args[]) {

          Jedis jedis = new Jedis("localhost");
          System.out.println("Connection to server sucessfully");
          System.out.println("Server is running: "+jedis.ping());
          int noOfElements = 500000;
          long startime = System.currentTimeMillis();
          for (int i=0;i<noOfElements;i++) {
                jedis.set(String.valueOf(i), "some fastastic value" +i);
              }
          System.out.println("Total Time to write" + noOfElements +"  is "+ ((System.currentTimeMillis() - startime)/1000));

           startime = System.currentTimeMillis();

          for (int i=0;i<noOfElements;i++) {
              jedis.get(String.valueOf(i));
            //  System.out.println(jedis.get(String.valueOf(i)));
          }

          jedis.close();
          System.out.println("Total Time to read " + noOfElements +"  is "+ ((System.currentTimeMillis() - startime)/1000));



    }


}
Run Code Online (Sandbox Code Playgroud)

蒙戈.java

package com.redis.mogo.perftest;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;

public class MongoJava {

    public static void main(String args[]) {

        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("perftest");

        DBCollection collection = db.getCollection("users");
          int noOfElements = 500000;

      long startime = System.currentTimeMillis();
    for (int i=0;i<noOfElements;i++) {
                BasicDBObject document = new BasicDBObject();
                document.put(String.valueOf(i), "some fastastic value" +i);
                collection.insert(document);
            }
              System.out.println("Total Time to write" + noOfElements +"  is "+ ((System.currentTimeMillis() - startime)/1000));

               startime = System.currentTimeMillis();

               int i=0;
               DBCursor cursor = collection.find();
                      while (cursor.hasNext()) {
                           String str = (String)cursor.next().get(String.valueOf(i));
                           //System.out.println(str);
                           i++;

                    }

              System.out.println("Total Time to read " + noOfElements +"  is "+ ((System.currentTimeMillis() - startime)/1000));

        collection.drop();

    }
}
Run Code Online (Sandbox Code Playgroud)

Redis 自定义配置文件:

protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "server_log.txt"
syslog-enabled yes
syslog-ident redis
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 10gb
appendonly no
appendfilename "appendonly.aof"
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
Run Code Online (Sandbox Code Playgroud)

Pas*_*rer 6

Redis 在全世界(数十万)个应用程序中用作缓存。这是它的主要用途。我从未听说过 MongoDB 被用作缓存(您可能会找到反例,我只是说它相当不寻常 - MongoDB 网站上甚至没有将其作为示例用例给出)。

这样做有很多充分的理由,首先是 Redis 从一开始就被设计为用作缓存(从第一个版本开始就可以使用Expire ),并且已经在许多生产用例中证明了它在这个角色中的可行性。

支持这一断言的一些参考文献:

我可以继续列出一个非常非常长的清单。

而且,编写相关的基准测试非常困难,对你的问题的评论再次证明了这一点。为了使它们具有相关性,您必须编写与您将在生产中使用的确切用例相匹配的基准测试,并具有类似的硬件和软件配置。写入 500k 项然后读取它们听起来不像生产用例。