For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Compress Network Traffic

MongoDB drivers can compress the messages sent to and received from MongoDB Server. This reduces the amount of data transferred for each operation. Compression can improve performance over constrained networks and lower the data transfer costs associated with running MongoDB Atlas.

Compression operates at the wire protocol level, so it applies to every operation that your application performs. If you specify more than one algorithm, the driver uses the first algorithm in your list that MongoDB Server also supports.

MongoDB drivers support the following compression algorithms:

MongoDB Atlas charges for data transferred across cloud regions, cloud providers, and between your infrastructure and MongoDB Atlas. Network compression reduces the amount of data that your application sends and receives, which can lower these data transfer costs. To learn more about pricing, see Data Transfer Costs.

Network compression also has productivity benefits. Compressed messages take less time to transfer across the network, which can improve your application's response times. Compression also reduces the bandwidth that your MongoDB traffic uses, which leaves more bandwidth available for other applications on the same network.

When you choose a compression algorithm, consider the following guidelines:

  • Use Zstandard for a balance of compression efficiency and resource usage. Zstandard is a good default choice for most applications.

  • Use Snappy for latency-sensitive applications that require minimal computational overhead.

  • Use Zlib for broad compatibility across MongoDB drivers and environments.

Tip

Monitor CPU Usage

Compression trades network bandwidth for CPU usage. After you enable network compression, monitor your application's CPU utilization to confirm that the trade-off benefits your workload. To learn more about these trade-offs, see the CPU Utilization and Network Compression in MongoDB blog post.

The following table lists the minimum supported driver and MongoDB Server version for each compression algorithm:

Compression Algorithm
Minimum Driver Version
Minimum Server Version

2.1

4.2

2.1

4.2

2.1

4.2

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.8.2"
features = ["snappy-compression"]
[dependencies.mongodb]
version = "3.8.2"
features = ["zlib-compression"]
[dependencies.mongodb]
version = "3.8.2"
features = ["zstd-compression"]

Tip

To specify multiple compressors, you must add the feature flag for each compressor to your mongodb dependency.

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 a ClientOptions instance. You can then pass the options to the with_options() method when instantiating a Client. To see an example that enables compression this way, see the ClientOptions section.

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 Specify Connection Options.

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(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)?;

For more information about the concepts in this guide, see the following documentation:

To learn more about any of the methods or types mentioned in this guide, see the following API documentation: