简体   繁体   English

MongoDB Java 驱动程序的 ChangeStream 性能问题

[英]MongoDB Java Driver's ChangeStream performance issue

I was having a requirement to watch on the latest documents from the mongodb.我需要观看 mongodb 的最新文档。 I have used the ChangeStream watch API to fetch the stream documents from the collection.我使用 ChangeStream 手表 API 从集合中获取 stream 文档。

The setup I have is a replica set with 3 nodes running in the same system with ports 27017,27018 and 27019. the setup doesn't have any auth setup.我拥有的设置是一个副本集,其中 3 个节点在同一系统中运行,端口为 27017、27018 和 27019。该设置没有任何身份验证设置。

mongodb.conf file: mongodb.conf 文件:

systemLog:
  destination: file
  logAppend: true
  path: /mongodb/logs/mongodb.log
storage:
  dbPath: /mongodb/data/d1
  journal:
    enabled: true
  engine: "wiredTiger"
  wiredTiger:
    engineConfig:
      cacheSizeGB: 4
net:
  port: 27017
  bindIp: localhost  

I have performed a bulk insert of the file which has 72663 documents in it.我已经对其中包含 72663 个文档的文件执行了批量插入。 And records processed per second I got out of the below program are just 8073.我从下面的程序中得到的每秒处理的记录只有 8073 条。

the Java code I had to watch on is.我必须观看的 Java 代码是。

   List<ServerAddress> serverAddress = Arrays.asList(new ServerAddress("localhost", 27019),new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27017));
   MongoClientSettings settings = MongoClientSettings.builder()
            .applyToClusterSettings(builder -> builder.hosts(serverAddress)).build();
    
    MongoClient client = MongoClients.create(settings);
    int count = 0;
    Instant start = null;
    
    MongoChangeStreamCursor<ChangeStreamDocument<Document>> dep = client.getDatabase("MyDB").getCollection("TestCollection").watch().cursor();
    
    while (true) {
        while (dep.hasNext()) {
            if (count == 1) {
                start = Instant.now();
            }
            count++;
            ChangeStreamDocument<Document> next = dep.next();
            
            if (count == 72663) {
                Instant end = Instant.now();
                Duration timeElapsed = Duration.between(start, end);
                long seconds = timeElapsed.getSeconds();
                long rec = count / seconds;
                System.out.println("records processed per second  " + rec);
            }
            
        }

Is there a way to get a better performance out of the change stream API.有没有办法从更改 stream API 中获得更好的性能。 Or is there any other API which can give me better performance in watching the documents.或者有没有其他的 API 可以让我在观看文档时有更好的表现。 Or any other replication properties which can give a better performance.或任何其他可以提供更好性能的复制属性。

I wrote and ran a benchmark .我编写并运行了一个基准测试

On a $100 consumer grade SFF desktop using i5-4460S, with the database in memory, I could obtain 17k documents written per second to zram.在使用 i5-4460S 的 100 美元消费级 SFF 桌面上,数据库位于 memory 中,我可以获得每秒写入 zram 的 17k 文档。 The database was CPU limited.数据库受 CPU 限制。

At this point the change stream performance is bound by the insert performance, and the change stream delivered the 17k changes/sec.此时,更改 stream 性能受插入性能的约束,更改 stream 提供了 17k 更改/秒。

The change stream was however bursting and the bursts were showing higher throughput than what the database could do on this hardware with sustained writes.然而,stream 的变化是突发的,并且突发显示的吞吐量比数据库在此硬件上通过持续写入所能完成的吞吐量更高。

Based on this I suggest that change stream performance exceeds the capability of the database to process writes.基于此,我建议更改 stream 性能超过数据库处理写入的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM