Skip to content

Commit 09d6e1b

Browse files
authored
DOCSP-47057: Network compression (#254)
* DOCSP-47057: Network compression * delete * edits * MM feedback
1 parent 6bc8578 commit 09d6e1b

File tree

3 files changed

+153
-3
lines changed

3 files changed

+153
-3
lines changed

source/connect/connection-options.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ Specify Connection Options
2020
.. toctree::
2121

2222
Stable API </connect/connection-options/stable-api>
23-
24-
.. TODO:
2523
Compress Network Traffic </connect/connection-options/network-compression>
26-
Connection Pools </connect/connection-options/connection-pools>
2724

2825
Overview
2926
--------
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
.. _php-network-compression:
2+
3+
========================
4+
Compress Network Traffic
5+
========================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: zlib, zstandard, zstd, snappy
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to configure **network compression** for
24+
your connection to MongoDB.
25+
26+
Network compression is a feature that allows you to compress and
27+
decompress messages sent between your application and MongoDB, reducing
28+
the total amount of data passed over the network.
29+
30+
The {+library-short+} supports the following compressors:
31+
32+
1. `Snappy <https://google.github.io/snappy/>`__
33+
#. `Zlib <https://zlib.net/>`__
34+
#. `Zstandard <https://github.com/facebook/zstd/>`__
35+
36+
.. note:: Compressor Selection
37+
38+
If you specify multiple compressors to use on your connection, the
39+
driver selects the first one that is supported by the MongoDB
40+
deployment that the {+library-short+} is connected to.
41+
42+
.. _php-enable-compression:
43+
44+
Enable Network Compression
45+
--------------------------
46+
47+
To enable compression for the connection to your MongoDB deployment, use the
48+
``compressors`` connection option and specify the compression algorithms you want to use.
49+
You can do this in two ways:
50+
51+
- Pass the algorithms as an argument to the ``MongoDB\Client`` constructor.
52+
- Specify the algorithms in your connection string.
53+
54+
The following example shows how to specify Snappy, Zlib, and
55+
Zstandard as the compressors for a connection. Select the :guilabel:`MongoDB\\Client`
56+
or :guilabel:`Connection URI` tab to see the corresponding code:
57+
58+
.. tabs::
59+
60+
.. tab:: MongoDB\\Client
61+
:tabid: Client
62+
63+
.. literalinclude:: /includes/connect/network-compression.php
64+
:language: php
65+
:dedent:
66+
:start-after: start-enable-client
67+
:end-before: end-enable-client
68+
69+
.. tab:: Connection URI
70+
:tabid: connectionstring
71+
72+
.. literalinclude:: /includes/connect/network-compression.php
73+
:language: php
74+
:dedent:
75+
:start-after: start-enable-uri
76+
:end-before: end-enable-uri
77+
78+
Specify the zlib Compression Level
79+
----------------------------------
80+
81+
If you specify ``zlib`` as one of your compression algorithms, you can also use the
82+
``zlibCompressionLevel`` option to specify a compression level. This option accepts
83+
an integer value between ``-1`` and ``9``:
84+
85+
- **-1:** (Default). zlib uses its default compression level (usually ``6``).
86+
- **0:** No compression.
87+
- **1:** Fastest speed but lowest compression.
88+
- **9:** Best compression but slowest speed.
89+
90+
The following example specifies the ``zlib`` compression algorithm and a
91+
``zlibCompressionLevel`` value of ``1``. Select the :guilabel:`MongoDB\\Client`
92+
or :guilabel:`Connection URI` tab to see the corresponding code:
93+
94+
.. tabs::
95+
96+
.. tab:: MongoDB\\Client
97+
:tabid: Client
98+
99+
.. literalinclude:: /includes/connect/network-compression.php
100+
:language: php
101+
:dedent:
102+
:start-after: start-set-level-client
103+
:end-before: end-set-level-client
104+
105+
.. tab:: Connection URI
106+
:tabid: connectionstring
107+
108+
.. literalinclude:: /includes/connect/network-compression.php
109+
:language: php
110+
:dedent:
111+
:start-after: start-set-level-uri
112+
:end-before: end-set-level-uri
113+
114+
API Documentation
115+
-----------------
116+
117+
To learn more about the ``MongoDB\Client`` class, see :phpclass:`MongoDB\Client`
118+
in the library API documentation.
119+
120+
To view a full list of URI options that you can pass to a ``MongoDB\Client``, see the
121+
:php:`MongoDB\Driver\Manager::__construct parameters </manual/en/mongodb-driver-manager.construct.php#refsect1-mongodb-driver-manager.construct-parameters>`
122+
in the extension API documentation.
123+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
// start-enable-client
4+
$client = new MongoDB\Client(
5+
'mongodb://<hostname>:<port>',
6+
['compressors' => 'snappy,zstd,zlib'],
7+
);
8+
// end-enable-client
9+
10+
// start-enable-uri
11+
$uri = 'mongodb://<hostname>:<port>/?compressors=snappy,zstd,zlib';
12+
$client = new MongoDB\Client($uri);
13+
// end-enable-uri
14+
15+
// start-set-level-client
16+
$uriOptions = [
17+
'compressors' => 'zlib',
18+
'zlibCompressionLevel' => 1,
19+
];
20+
21+
$client = new MongoDB\Client(
22+
'mongodb://<hostname>:<port>',
23+
$uriOptions,
24+
);
25+
// end-set-level-client
26+
27+
// start-set-level-uri
28+
$uri = 'mongodb://<hostname>:<port>/?compressors=zlib&zlibCompressionLevel=1';
29+
$client = new MongoDB\Client($uri);
30+
// end-set-level-uri

0 commit comments

Comments
 (0)