|
| 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 | + |
0 commit comments