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.15

4.2

2.5

4.2

2.15

4.2

To enable network compression for your MongoDB connection, install the required dependencies and specify the compression algorithms that you want to use.

Some compression algorithms require you to install an additional gem. The following table lists each algorithm and its required dependency, if any:

Compression Algorithm
Dependency

Snappy

Add the snappy gem to your application. To install, add gem 'snappy' to your Gemfile and run the bundle install command.

Zlib

No additional gem required. Your application must contain the zlib standard library extension.

Zstandard

Add the zstd-ruby gem to your application. To install, add gem 'zstd-ruby' to your Gemfile and run the bundle install command.

If you enable Snappy or Zstandard without installing the required gem, the driver raises an unmet dependency error when creating Mongo::Client.

To enable compression for the connection to your MongoDB instance, specify the algorithms you want to use in one of the following ways:

  • Add the algorithms to your connection string as a parameter

  • Specify the algorithms in the compressors option of your Mongo::Client object

To enable network compression by using the connection string, add the compressors option. You can specify one or more algorithms as a comma-separated list.

uri = "mongodb://<hostname>:<port>/?compressors=zlib,snappy"
client = Mongo::Client.new(uri)

To enable compression in your Client object, pass the compressors option to the Mongo::Client constructor.

client = Mongo::Client.new(["<hostname>:<port>"],
compressors: ["zlib", "snappy"])