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.

All versions of the Kotlin Sync driver support Snappy, Zlib, and Zstandard compression.

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

Compression Algorithm
Minimum Server Version

4.2

4.2

4.2

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

The following table lists each algorithm and the dependency it requires, if any:

Compression Algorithm
Dependency

Snappy

Download snappy-java, a Java JNI wrapper for the Snappy library. To install, see snappy-java in Maven Central.

Zlib

None. The driver natively supports Zlib compression.

Zstandard

Download zstd-jni, a Java JNI wrapper for the Zstandard library. To install, see zstd-jni in Maven Central.

You can enable compression for the connection to your MongoDB instance by specifying the algorithms in one of the following ways:

  • Chain the compressorList() method to the MongoClientSettings.builder() method.

  • Use the compressors parameter in your connection URI.

The following examples demonstrate how to specify all available compressors while connecting to MongoDB. Select the MongoClientSettings or Connection URI tab to see the corresponding syntax:

val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString("<connection string>"))
.compressorList(
listOf(
MongoCompressor.createSnappyCompressor(),
MongoCompressor.createZlibCompressor(),
MongoCompressor.createZstdCompressor())
)
.build()
val mongoClient = MongoClient.create(settings)
val uri = ConnectionString("mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd")
val mongoClient = MongoClient.create(uri)

If you specify Zlib as one of your compression algorithms, you can also use the MongoCompressor.LEVEL property to specify a compression level. This option accepts an integer value between -1 and 9:

  • -1: Default compression (usually 6)

  • 0: No compression

  • 1: Fastest speed but lowest compression

  • 9: Best compression but slowest speed

The following examples demonstrate how to specify a compression level for the zlib compressor. Select the MongoClientSettings or Connection URI tab to see the corresponding syntax:

val zlib = MongoCompressor.createZlibCompressor()
val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString(uri))
.compressorList(listOf(zlib.withProperty(MongoCompressor.LEVEL, <level>)))
.build()
val mongoClient = MongoClient.create(settings)
val uri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?" +
"compressors=zlib" +
"zlibCompressionLevel=<zlib compression level>"
val mongoClient = MongoClient.create(uri)

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