Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring the mongodb node.js driver to use snappy compression

We've recently upgraded our MongoDB replica set to v3.4.4; I notice that this release now supports compressed network communication with snappy. I've got the set members set to enable this, so that they communicate with each other this way, and the mongo shell seems to support it natively, but I can't find any documentation on how to set up our Node.js clients to use it.

Everything still works fine, of course, since uncompressed communications are used where the client and the server can't agree on a compression protocol, but it would be good to be able to take advantage of this for obvious reasons.

Has anyone else played with this or had any luck with it?

It does appear that Mongo has an open ticket on this, but wondered if anyone else had made any progress independently of that.

like image 308
Thomas J. Girsch Avatar asked Oct 23 '25 01:10

Thomas J. Girsch


1 Answers

You have two options to enable compression:

1) Driver option

Set the driver option when you initialize the MongoClient. Depending on the driver the syntax may differ. See docs, for example:

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

2) Connection string

Add the compressors argument to your connection string, for example:

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

Note

  • 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
        }
    }
    
like image 194
Manuel Avatar answered Oct 25 '25 15:10

Manuel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!