Skip to content

Commit deccc77

Browse files
authored
DOCSP-47032: Network compression (#622)
* DOCSP-47032: Network compression * fixes * typo * LM feedback * change file name * tech feedback
1 parent a6db0c1 commit deccc77

File tree

5 files changed

+82
-58
lines changed

5 files changed

+82
-58
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ full-version = "{+version+}.0"
2727
mdb-server = "MongoDB Server"
2828
package-name-org = "mongodb-org"
2929
api = "https://mongodb.github.io/mongo-java-driver/{+version+}"
30+
core-api = "https://mongodb.github.io/mongo-java-driver/{+version+}/apidocs/mongodb-driver-core"
3031
stable-api = "Stable API"
3132
mongocrypt-version = "{+full-version+}"
3233
nettyVersion = "io.netty:netty-all:4.1.87.Final"

source/connection.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ sections:
3232
- :ref:`Connect to MongoDB <java-connect-to-mongodb>`
3333
- :ref:`View a List of Connection Options <connection-options>`
3434
- :ref:`Specify Connection Behavior with the MongoClient Class <specify-mongoclient-settings>`
35-
- :ref:`Enable Network Compression <network-compression>`
35+
- :ref:`Enable Network Compression <java-compression>`
3636
- :ref:`Enable TLS/SSL on a Connection <tls-ssl>`
3737
- :ref:`Connect to MongoDB by Using a SOCKS5 Proxy <java-connect-socks>`
3838
- :ref:`Connect to MongoDB by Using a JNDI Datasource <jndi>`
Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,102 @@
1-
.. _compression:
1+
.. _java-compression:
22
.. _network-compression:
33

4-
===================
5-
Network Compression
6-
===================
4+
========================
5+
Compress Network Traffic
6+
========================
77

8-
You can enable a driver option to compress messages which reduces the amount
9-
of data passed over the network between MongoDB and your application.
8+
.. contents:: On this page
9+
:local:
10+
:backlinks: none
11+
:depth: 1
12+
:class: singlecol
1013

11-
The driver supports the following algorithms:
14+
.. facet::
15+
:name: genre
16+
:values: reference
17+
18+
.. meta::
19+
:keywords: zlib, zstandard, zstd, snappy
1220

13-
1. `Snappy <https://google.github.io/snappy/>`__: available in MongoDB 3.4 and later.
21+
Overview
22+
--------
1423

15-
2. `Zlib <https://zlib.net/>`__: available in MongoDB 3.6 and later.
24+
In this guide, you can learn how to use the {+driver-short+} to enable network
25+
compression. The driver provides a connection option to compress messages, which
26+
reduces the amount of data passed over the network between MongoDB and your application.
1627

17-
3. `Zstandard <https://github.com/facebook/zstd/>`__: available in MongoDB 4.2 and later.
28+
The driver supports the following compression algorithms:
29+
30+
- `Snappy <https://google.github.io/snappy/>`__: Available in {+mdb-server+} v3.4 and later.
31+
- `Zlib <https://zlib.net/>`__: Available in {+mdb-server+} v3.6 and later.
32+
- `Zstandard <https://github.com/facebook/zstd/>`__: Available in {+mdb-server+} v4.2 and later.
1833

1934
The driver tests against the following versions of these libraries:
2035

2136
- ``{+snappyVersion+}``
2237
- ``{+zstdVersion+}``
2338

24-
If you specify multiple compression algorithms, the driver selects the
25-
first one in the list supported by the MongoDB instance to which it is
26-
connected.
39+
If you specify multiple compression algorithms, the driver selects the first one
40+
in the list supported by your MongoDB instance.
2741

2842
.. note::
2943

3044
Applications that require Snappy or Zstandard compression must
31-
:ref:`add explicit dependencies <compression-dependencies>` for those
32-
algorithms.
45+
add explicit dependencies for those algorithms. To learn more,
46+
see the :ref:`java-compression-dependencies` section of this guide.
3347

34-
.. _enable-compression:
48+
.. _java-compression-specify:
3549

3650
Specify Compression Algorithms
3751
------------------------------
3852

3953
You can enable compression for the connection to your MongoDB instance
40-
by specifying the algorithms in one of two ways: adding the parameter to your
41-
connection string using ``ConnectionString`` or by calling the method in the
42-
``MongoClientSettings.Builder`` class.
43-
44-
.. tabs::
54+
by specifying the algorithms in one of the following ways:
4555

46-
.. tab:: ConnectionString
47-
:tabid: connectionstring
48-
49-
To enable compression using the `ConnectionString <{+api+}/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html>`__,
50-
add the parameter ``compressors`` in the connection string passed to
51-
``MongoClients.create()``. You can specify one or more compression
52-
algorithms, separating them with commas:
56+
- Use the ``compressors`` parameter in your connection string.
57+
- Chain the ``compressorList()`` method to the ``MongoClientSettings.builder()`` method.
5358

54-
.. code-block:: java
59+
This example shows how to specify the Snappy, Zstandard, and Zlib compression
60+
algorithms. Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings`
61+
tab to see the corresponding syntax:
5562

56-
ConnectionString connectionString = new ConnectionString("mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd");
57-
MongoClient mongoClient = MongoClients.create(connectionString);
63+
.. tabs::
5864

59-
Specify compression algorithms using the following strings:
65+
.. tab:: Connection String
66+
:tabid: connectionstring
6067

61-
- "snappy" for `Snappy <https://google.github.io/snappy/>`__ compression
62-
- "zlib" for `Zlib <https://zlib.net/>`__ compression
63-
- "zstd" for `Zstandard <https://github.com/facebook/zstd/>`__ compression
68+
.. literalinclude:: /includes/connect/NetworkCompression.java
69+
:start-after: start-specify-connection-string
70+
:end-before: end-specify-connection-string
71+
:language: java
6472

6573
.. tab:: MongoClientSettings
6674
:tabid: mongoclientsettings
6775

68-
To enable compression using the `MongoClientSettings <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html>`__,
69-
pass the `compressorList() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#compressorList(java.util.List)>`__
70-
builder method a list of `MongoCompressor <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCompressor.html>`__
71-
instances. You can specify one or more compression algorithms in the list:
72-
73-
.. code-block:: java
74-
:emphasize-lines: 2-4
75-
76-
MongoClientSettings settings = MongoClientSettings.builder()
77-
.compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor(),
78-
MongoCompressor.createZlibCompressor(),
79-
MongoCompressor.createZstdCompressor()))
80-
.build();
81-
MongoClient client = MongoClients.create(settings);
76+
.. literalinclude:: /includes/connect/NetworkCompression.java
77+
:start-after: start-specify-client-settings
78+
:end-before: end-specify-client-settings
79+
:language: java
8280

83-
.. _compression-dependencies:
81+
.. _java-compression-dependencies:
8482

8583
Compression Algorithm Dependencies
8684
----------------------------------
8785

88-
The JDK supports `Zlib <https://zlib.net/>`__ compression natively, but
89-
`Snappy <https://google.github.io/snappy/>`__ and
90-
`Zstandard <https://github.com/facebook/zstd/>`__ depend on open source
91-
implementations. See
92-
`snappy-java <https://github.com/xerial/snappy-java>`__ and
93-
`zstd-java <https://github.com/luben/zstd-jni>`__ for details.
86+
The JDK natively supports `Zlib <https://zlib.net/>`__ compression. However,
87+
Snappy and Zstandard depend on open source Java implementations. To learn more
88+
about these implementations, see the following GitHub repositories:
89+
90+
- `snappy-java <https://github.com/xerial/snappy-java>`__
91+
- `zstd-java <https://github.com/luben/zstd-jni>`__
92+
93+
API Documentation
94+
-----------------
9495

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

99+
- `MongoClient <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoClient.html>`__
100+
- `createSnappyCompressor() <{+core-api+}/com/mongodb/MongoCompressor.html#createSnappyCompressor()>`__
101+
- `createZlibCompressor() <{+core-api+}/com/mongodb/MongoCompressor.html#createZlibCompressor()>`__
102+
- `createZstdCompressor() <{+core-api+}/com/mongodb/MongoCompressor.html#createZstdCompressor()>`__
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// start-specify-connection-string
2+
ConnectionString connectionString = new ConnectionString(
3+
"mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd");
4+
5+
MongoClient client = MongoClients.create(connectionString);
6+
// end-specify-connection-string
7+
8+
// start-specify-client-settings
9+
MongoClientSettings settings = MongoClientSettings.builder()
10+
.compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor(),
11+
MongoCompressor.createZlibCompressor(),
12+
MongoCompressor.createZstdCompressor()))
13+
.build();
14+
15+
MongoClient client = MongoClients.create(settings);
16+
// end-specify-client-settings

source/versioning/whats-new.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ New features of the 4.11 driver release include:
436436
- Added Atlas Search index management helpers. To learn more, see :ref:`Atlas Search Indexes <search-indexes>`.
437437

438438
- Updated Snappy and Zstd compression library dependency versions. To learn
439-
more about the current dependency versions, see :ref:`network-compression`.
439+
more about the current dependency versions, see :ref:`java-compression`.
440440
- Added ``getElapsedTime()`` methods to the following classes to monitor the
441441
duration of connection pool events:
442442

0 commit comments

Comments
 (0)