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 PHP library uses the PHP extension to compress network traffic. The following table lists the minimum PHP extension and MongoDB Server version required 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.
Install Dependencies
Because the PHP extension uses the MongoDB C driver to implement network compression, you must install the required dependencies for the C driver. To view these dependencies, see Install Dependencies in the C driver documentation.
Specify Compression Algorithms
To enable compression for the connection to your MongoDB deployment, use the compressors connection option and specify the compression algorithms you want to use. You can do this in two ways:
Pass the algorithms as an argument to the
MongoDB\Clientconstructor.Specify the algorithms in your connection string.
The following example shows how to specify Snappy, Zlib, and Zstandard as the compressors for a connection. Select the MongoDB\Client or Connection URI tab to see the corresponding code:
$client = new MongoDB\Client( 'mongodb://<hostname>:<port>', ['compressors' => 'snappy,zstd,zlib'], );
$uri = 'mongodb://<hostname>:<port>/?compressors=snappy,zstd,zlib'; $client = new MongoDB\Client($uri);
Specify the Zlib Compression Level
If you specify Zlib as one of your compression algorithms, you can also use the zlibCompressionLevel option to specify a compression level. This option accepts an integer value between -1 and 9:
-1: (Default). zlib uses its default compression level (usually
6).0: No compression.
1: Fastest speed but lowest compression.
9: Best compression but slowest speed.
The following example specifies the zlib compression algorithm and a zlibCompressionLevel value of 1. Select the MongoDB\Client or Connection URI tab to see the corresponding code:
$uriOptions = [ 'compressors' => 'zlib', 'zlibCompressionLevel' => 1, ]; $client = new MongoDB\Client( 'mongodb://<hostname>:<port>', $uriOptions, );
$uri = 'mongodb://<hostname>:<port>/?compressors=zlib&zlibCompressionLevel=1'; $client = new MongoDB\Client($uri);
API Documentation
To learn more about the MongoDB\Client class, see MongoDB\Client in the library API documentation.
To view a full list of URI options that you can pass to a MongoDB\Client, see the MongoDB\Driver\Manager::__construct parameters in the extension API documentation.