配置MongoDB Node.js驱动程序以使用Snappy压缩

3
我们最近将MongoDB副本集升级到v3.4.4;我注意到这个版本现在支持使用snappy进行压缩网络通信。我已经将副本集成员设置为启用此功能,以便它们之间可以使用这种方式进行通信,mongo shell似乎也原生支持它,但我找不到任何关于如何设置我们的Node.js客户端使用它的文档。
当然,一切仍然正常工作,因为在客户端和服务器无法就压缩协议达成一致时,将使用未压缩的通信。但出于明显的原因,能够利用这一点将是很好的。
有其他人尝试过这个或者有任何进展吗?
看起来Mongo已经在这方面提出了一个问题,但想知道是否有其他人独立取得了任何进展。
1个回答

5

启用压缩的方法有两种:

1) 驱动程序选项

在初始化MongoClient时设置驱动程序选项。根据不同的驱动程序,语法可能有所不同。例如,请参考文档

"options": {
    "compression": [
        "zstd",
        "snappy",
        "zlib"
    ]
}

2) 连接字符串

在连接字符串中添加compressors参数,例如:

mongodb://example.com/?compressors=zstd,snappy,zlib

注意

  • The examples above demonstrate how to set multiple compressors to better explain the syntax and make the answer more useful. If you only want to set only one compressor, change accordingly.

  • You can find more examples on how to set compressors in the mongoDB Node.js driver test scripts.

  • You can verify that network compression is begin used and which compression algorithm is being used in the mongo logs, as each compression / decompression generates a verbose log entry. Or you can run db.serverStatus()['network'] and observe the bytesIn / bytesOut of various compressors, e.g.:

    {
        "bytesIn" : NumberLong("1828061251"),
        "bytesOut" : NumberLong("57900955809"),
        "physicalBytesIn" : NumberLong("2720120753"),
        "physicalBytesOut" : NumberLong("32071382239"),
        "numRequests" : NumberLong("570858"),
        "compression" : {
            "snappy" : {
                "compressor" : {
                    "bytesIn" : NumberLong("2215000774"),
                    "bytesOut" : NumberLong("752759260")
                },
                "decompressor" : {
                    "bytesIn" : NumberLong("226402961"),
                    "bytesOut" : NumberLong("848171447")
                }
            }
        },
        "serviceExecutorTaskStats" : {
            "executor" : "passthrough",
            "threadsRunning" : 80
        }
    }
    

谢谢!我之前不知道 serverStatus 有这么有价值的信息。 - asgs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接