Skip to content

Commit d62e22e

Browse files
committed
DOCSP-47032: Network compression
1 parent 52989b6 commit d62e22e

File tree

3 files changed

+80
-57
lines changed

3 files changed

+80
-57
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ full-version = "{+version+}.0"
2626
mdb-server = "MongoDB Server"
2727
package-name-org = "mongodb-org"
2828
api = "https://mongodb.github.io/mongo-java-driver/{+version+}"
29+
core-api = "https://mongodb.github.io/mongo-java-driver/{+version+}/apidocs/mongodb-driver-core"
2930
stable-api = "Stable API"
3031
mongocrypt-version = "{+full-version+}"
3132
nettyVersion = "io.netty:netty-all:4.1.87.Final"
Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,101 @@
1-
.. _compression:
2-
.. _network-compression:
1+
.. _java-compression:
32

4-
===================
5-
Network Compression
6-
===================
3+
========================
4+
Compress Network Traffic
5+
========================
76

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.
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
1012

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

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

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

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

1933
The driver tests against the following versions of these libraries:
2034

2135
- ``{+snappyVersion+}``
2236
- ``{+zstdVersion+}``
2337

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.
38+
If you specify multiple compression algorithms, the driver selects the first one
39+
in the list supported by your MongoDB instance.
2740

2841
.. note::
2942

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

34-
.. _enable-compression:
47+
.. _java-compression-specify:
3548

3649
Specify Compression Algorithms
3750
------------------------------
3851

3952
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::
53+
by specifying the algorithms in one of the following ways:
4554

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:
55+
- Use the ``compressors`` parameter in your connection string
56+
- Chain the ``compressorList`` method to the ``MongoClientSettings.builder()`` method
5357

54-
.. code-block:: java
58+
This example shows how to specify all compression algorithms. Select the
59+
:guilabel:`Connection String` or :guilabel:`MongoClientSettings` tab to
60+
see the corresponding syntax:
5561

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

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

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
67+
.. literalinclude:: /includes/connect/network-compression.java
68+
:start-after: start-specify-connection-string
69+
:end-before: end-specify-connection-string
70+
:language: java
6471

6572
.. tab:: MongoClientSettings
6673
:tabid: mongoclientsettings
6774

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);
75+
.. literalinclude:: /includes/connect/network-compression.java
76+
:start-after: start-specify-uri
77+
:end-before: end-specify-uri
78+
:language: java
8279

83-
.. _compression-dependencies:
80+
.. _java-compression-dependencies:
8481

8582
Compression Algorithm Dependencies
8683
----------------------------------
8784

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.
85+
The JDK natively supports `Zlib <https://zlib.net/>`__ compression. However,
86+
Snappy and Zstandard depend on open source Java implementations. To learn more
87+
about these implementations, see the following Github repositories:
88+
89+
- `snappy-java <https://github.com/xerial/snappy-java>`__
90+
- `zstd-java <https://github.com/luben/zstd-jni>`__
91+
92+
API Documentation
93+
-----------------
9494

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

98+
- `MongoClient <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoClient.html>`__
99+
- `createSnappyCompressor() <{+core-api+}/com/mongodb/MongoCompressor.html#createSnappyCompressor()>`__
100+
- `createZlibCompressor() <{+core-api+}/com/mongodb/MongoCompressor.html#createZlibCompressor()>`__
101+
- `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-uri
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-uri

0 commit comments

Comments
 (0)