|
| 1 | +.. _java-rs-compression: |
| 2 | +.. _java-rs-network-compression: |
| 3 | + |
| 4 | +======================== |
| 5 | +Compress Network Traffic |
| 6 | +======================== |
| 7 | + |
| 8 | +.. contents:: On this page |
| 9 | + :local: |
| 10 | + :backlinks: none |
| 11 | + :depth: 1 |
| 12 | + :class: singlecol |
| 13 | + |
| 14 | +.. facet:: |
| 15 | + :name: genre |
| 16 | + :values: reference |
| 17 | + |
| 18 | +.. meta:: |
| 19 | + :keywords: zlib, zstandard, zstd, snappy |
| 20 | + |
| 21 | +The {+driver-short+} provides a connection option to compress messages, which reduces the amount |
| 22 | +of data passed over the network between MongoDB and your application. |
| 23 | + |
| 24 | +The driver supports the following algorithms: |
| 25 | + |
| 26 | +- `Snappy <https://google.github.io/snappy/>`__: available in MongoDB 3.4 and later. |
| 27 | +- `Zlib <https://zlib.net/>`__: available in MongoDB 3.6 and later. |
| 28 | +- `Zstandard <https://github.com/facebook/zstd/>`__: available in MongoDB 4.2 and later. |
| 29 | + |
| 30 | +The driver tests against the following versions of these libraries: |
| 31 | + |
| 32 | +- ``{+snappyVersion+}`` |
| 33 | +- ``{+zstdVersion+}`` |
| 34 | + |
| 35 | +If you specify multiple compression algorithms, the driver selects the first one |
| 36 | +in the list supported by your MongoDB instance. |
| 37 | + |
| 38 | +.. note:: |
| 39 | + |
| 40 | + Applications that require Snappy or Zstandard compression must |
| 41 | + :ref:`add explicit dependencies <java-rs-compression-dependencies>` for those |
| 42 | + algorithms. |
| 43 | + |
| 44 | +.. _enable-compression: |
| 45 | + |
| 46 | +Specify Compression Algorithms |
| 47 | +------------------------------ |
| 48 | + |
| 49 | +You can enable compression for the connection to your MongoDB instance |
| 50 | +by specifying the algorithms in one of two ways. Select the |
| 51 | +:guilabel:`Connection String` or :guilabel:`MongoClientSettings` tab to |
| 52 | +see the corresponding syntax: |
| 53 | + |
| 54 | +- In the ``compressors`` parameter of your connection string |
| 55 | +- In the ``compressorList`` method chained to the ``MongoClientSettings.builder()`` method |
| 56 | + |
| 57 | +The following example shows how to specify all compression algorithms: |
| 58 | + |
| 59 | +.. tabs:: |
| 60 | + |
| 61 | + .. tab:: Connection String |
| 62 | + :tabid: connectionstring |
| 63 | + |
| 64 | + .. literalinclude:: /includes/connect/network-compression.java |
| 65 | + :start-after: start-specify-connection-string |
| 66 | + :end-before: end-specify-connection-string |
| 67 | + :language: java |
| 68 | + |
| 69 | + .. tab:: MongoClientSettings |
| 70 | + :tabid: mongoclientsettings |
| 71 | + |
| 72 | + .. literalinclude:: /includes/connect/network-compression.java |
| 73 | + :start-after: start-specify-uri |
| 74 | + :end-before: end-specify-uri |
| 75 | + :language: java |
| 76 | + |
| 77 | +.. _java-rs-compression-dependencies: |
| 78 | + |
| 79 | +Compression Algorithm Dependencies |
| 80 | +---------------------------------- |
| 81 | + |
| 82 | +The JDK natively supports `Zlib <https://zlib.net/>`__ compression. However, |
| 83 | +Snappy and Zstandard depend on open source Java implementations. To learn more |
| 84 | +about these implementations, see the following Github pages: |
| 85 | + |
| 86 | +- `snappy-java <https://github.com/xerial/snappy-java>`__ |
| 87 | +- `zstd-java <https://github.com/luben/zstd-jni>`__ |
| 88 | + |
| 89 | +API Documentation |
| 90 | +----------------- |
| 91 | + |
| 92 | +To learn more about any of the methods or types discussed in this |
| 93 | +guide, see the following API documentation: |
| 94 | + |
| 95 | +- `MongoClient <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClient.html>`__ |
| 96 | +- `createSnappyCompressor() <{+api+}/mongodb-driver-core/com/mongodb/MongoCompressor.html#createSnappyCompressor()>`__ |
| 97 | +- `createZlibCompressor() <{+api+}//mongodb-driver-core/com/mongodb/MongoCompressor.html#createZlibCompressor()>`__ |
| 98 | +- `createZstdCompressor() <{+api+}/mongodb-driver-core/com/mongodb/MongoCompressor.html#createZstdCompressor()>`__ |
0 commit comments