|
| 1 | +.. _kotlin-sync-compression: |
| 2 | + |
| 3 | +======================== |
| 4 | +Compress Network Traffic |
| 5 | +======================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 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 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. |
| 26 | + |
| 27 | +The driver supports the following compression algorithms: |
| 28 | + |
| 29 | +- `Snappy <https://google.github.io/snappy/>`__ |
| 30 | +- `Zlib <https://zlib.net/>`__ |
| 31 | +- `Zstandard <https://github.com/facebook/zstd/>`__ |
| 32 | + |
| 33 | +If you specify multiple compression algorithms, the driver selects the first one |
| 34 | +in the list supported by your MongoDB instance. |
| 35 | + |
| 36 | +.. note:: |
| 37 | + |
| 38 | + Applications that require Snappy or Zstandard compression must |
| 39 | + add explicit dependencies for those algorithms. To learn more, |
| 40 | + see the :ref:`kotlin-sync-compression-dependencies` section of this guide. |
| 41 | + |
| 42 | +Specify Compression Algorithms |
| 43 | +------------------------------ |
| 44 | + |
| 45 | +You can enable compression for the connection to your MongoDB instance |
| 46 | +by specifying the algorithms in one of the following ways: |
| 47 | + |
| 48 | +- Chain the ``compressorList()`` method to the ``MongoClientSettings.builder()`` method. |
| 49 | +- Use the ``compressors`` parameter in your connection URI. |
| 50 | + |
| 51 | +The following examples demonstrate how to specify all available compressors |
| 52 | +while connecting to MongoDB. Select the :guilabel:`MongoClientSettings` or :guilabel:`Connection URI` |
| 53 | +tab to see the corresponding syntax: |
| 54 | + |
| 55 | +.. include:: /includes/connect/compression-tabs.rst |
| 56 | + |
| 57 | +Specify the Zlib Compression Level |
| 58 | +---------------------------------- |
| 59 | + |
| 60 | +If you specify ``zlib`` as one of your compression algorithms, you can also use the |
| 61 | +``MongoCompressor.LEVEL`` property to specify a compression level. This option accepts |
| 62 | +an integer value between ``-1`` and ``9``: |
| 63 | + |
| 64 | +- **-1:** Default compression (usually ``6``) |
| 65 | +- **0:** No compression |
| 66 | +- **1:** Fastest speed but lowest compression |
| 67 | +- **9:** Best compression but slowest speed |
| 68 | + |
| 69 | +The following examples demonstrate how to specify a compression level for |
| 70 | +the ``zlib`` compressor. Select the :guilabel:`MongoClientSettings` or :guilabel:`Connection URI` |
| 71 | +tab to see the corresponding syntax: |
| 72 | + |
| 73 | +.. include:: /includes/connect/zlib-level-tabs.rst |
| 74 | + |
| 75 | +.. _kotlin-sync-compression-dependencies: |
| 76 | + |
| 77 | +Compression Algorithm Dependencies |
| 78 | +---------------------------------- |
| 79 | + |
| 80 | +The JDK natively supports `Zlib <https://zlib.net/>`__ compression. However, |
| 81 | +Snappy and Zstandard depend on open source Java implementations. To learn more |
| 82 | +about these implementations, see the following GitHub repositories: |
| 83 | + |
| 84 | +- `snappy-java <https://github.com/xerial/snappy-java>`__ |
| 85 | +- `zstd-jni <https://github.com/luben/zstd-jni>`__ |
| 86 | + |
| 87 | +API Documentation |
| 88 | +----------------- |
| 89 | + |
| 90 | +To learn more about any of the methods or types discussed in this |
| 91 | +guide, see the following API documentation: |
| 92 | + |
| 93 | +- `MongoClient <{+driver-api+}/-mongo-client/index.html>`__ |
| 94 | +- `createSnappyCompressor() <{+core-api+}/MongoCompressor.html#createSnappyCompressor()>`__ |
| 95 | +- `createZlibCompressor() <{+core-api+}/MongoCompressor.html#createZlibCompressor()>`__ |
| 96 | +- `createZstdCompressor() <{+core-api+}/MongoCompressor.html#createZstdCompressor()>`__ |
0 commit comments