Overview
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.
Compression Algorithms
MongoDB drivers support the following compression algorithms:
Network Compression Benefits
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.
Version Compatibility
The following table lists the minimum supported driver and MongoDB server version for each compression algorithm:
Enable Network Compression
To enable network compression for your MongoDB connection, install the required dependencies and specify the compression algorithms that you want to use.
Compression Algorithm Dependencies
The following table lists each algorithm and the dependency it requires, if any:
Compression Algorithm | Dependency |
|---|---|
Snappy | Download |
Zlib | None. The driver natively supports Zlib compression. |
Zstandard | Download |
Specify Compression Algorithms
You can enable compression on your connection by specifying the algorithms in the following ways:
Adding the
compressorsparameter to yourConnectionStringinstanceCalling the
compressorList()method from theMongoClientSettingsbuilder
To enable compression on your connection in a ConnectionString instance, specify the compressors parameter. You can specify one or more of the following values for the compressors parameter:
The following example shows how to specify Snappy, Zlib, and Zstandard as the compression algorithms for a connection:
// Replace the placeholders with values from your MongoDB deployment's connection string val connectionString = ConnectionString("mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd") // Create a new client with your settings val mongoClient = MongoClient.create(connectionString)
To enable compression using within your MongoClientSettings, call the compressorList() builder method and pass one or more MongoCompressor instances as a parameter.
You can specify compression algorithms by calling the following methods from MongoCompressor:
createSnappyCompressor()for Snappy compressioncreateZlibCompressor()for Zlib compressioncreateZstdCompressor()for Zstandard compression
The following example shows how to specify Snappy, Zlib, and Zstandard as the compression algorithms for a connection:
// Replace the placeholder with your MongoDB deployment's connection string val uri = "<connection string>" val settings = MongoClientSettings.builder() .applyConnectionString(ConnectionString(uri)) .compressorList( listOf( MongoCompressor.createSnappyCompressor(), MongoCompressor.createZlibCompressor(), MongoCompressor.createZstdCompressor()) ) .build() // Create a new client with your settings val mongoClient = MongoClient.create(settings)