Network Compression
On this page
Overview
In this guide, you can learn how to configure network compression for your connection to MongoDB by using the Rust driver.
Network compression is a feature that allows you to compress and decompress messages sent between your application and MongoDB, reducing the total amount of data passed over the network.
The driver supports the following compressors:
Snappy: available in MongoDB 3.4 and later
Zlib: available in MongoDB 3.6 and later
Zstandard: available in MongoDB 4.2 and later
Note
Compressor Selection
If you specify multiple compressors to use on your connection, the driver selects the first one that is supported by the MongoDB instance that the driver is connected to.
This guide includes the following sections:
Add Compression Feature Flags describes how to add feature flags to your application for different compressors
Enable Network Compression describes how to enable network compression on your
Client
instanceAdditional Information provides links to resources and API documentation for types and methods mentioned in this guide
Add Compression Feature Flags
To use a compressor, add the relevant feature flag to your
mongodb
dependency's feature list in your project's Cargo.toml
file.
Select the tab for your preferred compressor to see how to add the
necessary feature flag to your mongodb
dependency:
[dependencies.mongodb] version = "3.1.0" features = ["snappy-compression"]
[dependencies.mongodb] version = "3.1.0" features = ["zlib-compression"]
[dependencies.mongodb] version = "3.1.0" features = ["zstd-compression"]
Tip
To specify multiple compressors, you must add the feature flag for
each compressor to your mongodb
dependency.
Enable Network Compression
You can enable compression on your Client
instance by specifying
compressors in the following ways:
Adding the
compressors
parameter to your connection string. To see an example that enables compression this way, see the Connection String section.Setting the
compressors
field of aClientOptions
instance. You can then pass the options to thewith_options()
method when instantiating aClient
. To see an example that enables compression this way, see the ClientOptions section.
Connection String
To enable compression by using a connection string, specify
the compressors
parameter. You can specify one or more of the
following values for the compressors
parameter:
"snappy"
for Snappy compression"zlib"
for Zlib compression"zstd"
for Zstandard compression
The following example shows how to specify Snappy, Zlib, and Zstandard as the compressors for a connection:
let uri = "mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd"; let client = Client::with_uri_str(uri).await?;
To learn more about setting client options, see the guide on Connection Options.
ClientOptions
To enable compression within your ClientOptions
instance, set
the compressors
field, and then pass the options when creating a
client.
The compressors
field takes a value of
type Vec<Compressor>
. The Compressor
type has the following
possible values:
Compressor::Snappy
Compressor::Zstd { level: <integer> }
Compressor::Zlib { level: <integer> }
For the compressors that have a level
field, set the value to
None
to indicate the default level. The following table describes the
default and accepted compression levels for Zlib and Zstandard:
Compressor | Default Level | Accepted Levels |
---|---|---|
Zlib | 6 | Integers from 0 to 9 or None |
Zstandard | 3 | Integers from 1 to 22 or None |
A higher level
value results in more compression, which is slower.
The following example shows how to specify Snappy, Zlib, and Zstandard as the compressors for a connection:
let uri = "<connection string>"; let mut client_options = ClientOptions::parse_async(uri).await?; let compressors = vec![ Compressor::Snappy, Compressor::Zstd { level: Some(1) }, Compressor::Zlib { level: None } ]; client_options.compressors = Some(compressors); let client = Client::with_options(client_options)?;
Additional Information
For more information about the concepts in this guide, see the following documentation:
Connection String Compression Options in the Server manual
API Documentation
To learn more about any of the methods or types mentioned in this guide, see the following API documentation: