Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-33282: Network compression #298

Merged
merged 7 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/fundamentals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Fundamentals

/fundamentals/connection
Connect to MongoDB Atlas from AWS Lambda <https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/>
/fundamentals/network-compression
/fundamentals/stable-api
/fundamentals/context
/fundamentals/auth
Expand Down
97 changes: 97 additions & 0 deletions source/fundamentals/network-compression.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
.. _golang-network-compression:

===================
Network Compression
===================

In this guide, you can learn how to enable **network compression** by using
the {+driver-short+}. You can specify a driver option to compress messages,
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved
which reduces the amount of data passed over the network between MongoDB and
your application.

The {+driver-short+} supports the following compression algorithms:

1. `Snappy <https://google.github.io/snappy/>`__: available in MongoDB 3.4 and later.

2. `Zlib <https://zlib.net/>`__: available in MongoDB 3.6 and later.

3. `Zstandard <https://github.com/facebook/zstd/>`__: available in MongoDB 4.2 and later.

If you specify multiple compression algorithms, the driver selects the
first one in the list supported by your MongoDB instance.
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved

.. note::

When using the Snappy or Zstandard compression algorithm, you must
:ref:`add explicit dependencies <compression-dependencies>`.
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved

.. _enable-compression:

Specify Compression Algorithms
------------------------------

You can enable compression for the connection to your MongoDB instance
by specifying the algorithms in one of two ways:
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved

1. Adding the ``compressors`` parameter to your connection string.
2. Using the ``SetCompressors()`` method to specify a ``ClientOptions`` instance.
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved

.. tabs::

.. tab:: Connection String
:tabid: connection-string

To enable compression using the connection string, add the
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved
``compressors`` parameter to your connection string. You can
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved
specify one or more compression algorithms separated by commas:

.. code-block:: go
:emphasize-lines: 1

opts := options.Client().ApplyURI("mongodb://localhost:27017/?compressors=snappy,zlib,zstd")
client, _ := mongo.Connect(context.TODO(), opts)

.. tab:: SetCompressors()
:tabid: mongoclientoptions

To enable compression using `SetCompressors()
<{+api+}/mongo/options#ClientOptions.SetCompressors>`__, pass
the compression algorithm you want to use to the ``SetCompressors()``
method. You can specify one or more compression algorithms separated
by commas:
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: go
:emphasize-lines: 1

opts := options.Client().SetCompressors([]string{"snappy", "zlib", "zstd"})
client, _ := mongo.Connect(context.TODO(), opts)

Specify compression algorithms by using the following strings:

- ``"snappy"`` for `Snappy <https://google.github.io/snappy/>`__ compression
- ``"zlib"`` for `Zlib <https://zlib.net/>`__ compression
- ``"zstd"`` for `Zstandard <https://github.com/facebook/zstd/>`__ compression

.. _compression-dependencies:

Compression Algorithm Dependencies
----------------------------------

To add the Snappy compression algorithm to your application, run the
following code:

.. code-block:: bash

go get github.com/golang/snappy

To add the Zstandard compression algorithm to your application, run the
following code:

.. code-block:: bash

go get -u github.com/klauspost/compress

.. note::

To use the Zlib compression algorithm, import the built-in Go package
``compress/zlib`` in your application files.
ccho-mongodb marked this conversation as resolved.
Show resolved Hide resolved
Loading