Skip to content

Commit 1748b86

Browse files
authored
DOCSP-33282: Network compression (#298)
* DOCSP-33282: Network compression * small fixes * toc * fixing typos, rewording * review feedback * additional info * review feedback 2 + fixes
1 parent d4b1a65 commit 1748b86

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

source/fundamentals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Fundamentals
88

99
/fundamentals/connection
1010
Connect to MongoDB Atlas from AWS Lambda <https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/>
11+
/fundamentals/network-compression
1112
/fundamentals/stable-api
1213
/fundamentals/context
1314
/fundamentals/auth
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
.. _golang-network-compression:
2+
3+
===================
4+
Network Compression
5+
===================
6+
7+
In this guide, you can learn how to enable **network compression** by using
8+
the {+driver-short+}. You can specify a client option to compress messages,
9+
which reduces the amount of data passed over the network between MongoDB and
10+
your application.
11+
12+
The {+driver-short+} supports the following compression algorithms:
13+
14+
1. `Snappy <https://google.github.io/snappy/>`__: available in MongoDB 3.4 and later.
15+
16+
2. `Zlib <https://zlib.net/>`__: available in MongoDB 3.6 and later.
17+
18+
3. `Zstandard <https://github.com/facebook/zstd/>`__: available in MongoDB 4.2 and later.
19+
20+
If you specify multiple compression algorithms, the driver selects the
21+
first one in the list supported by your MongoDB deployment.
22+
23+
You must add dependencies to use the Snappy or Zstandard compression algorithm.
24+
For more information, see the :ref:`compression-dependencies` section of this
25+
guide.
26+
27+
.. _enable-compression:
28+
29+
Specify Compression Algorithms
30+
------------------------------
31+
32+
You can enable compression for the connection to your MongoDB deployment
33+
by specifying the algorithms in one of two ways:
34+
35+
- Set the compression algorithm in your connection string.
36+
- Set the compression algorithm in a ``ClientOptions`` instance.
37+
38+
.. tabs::
39+
40+
.. tab:: Connection String
41+
:tabid: connection-string
42+
43+
To enable compression by using the connection string, add the compression
44+
algorithm as the value of the ``compressors`` parameter to your connection
45+
string. You can specify one or more compression algorithms separated by
46+
commas:
47+
48+
.. code-block:: go
49+
:emphasize-lines: 1
50+
51+
opts := options.Client().ApplyURI("mongodb://localhost:27017/?compressors=snappy,zlib,zstd")
52+
client, _ := mongo.Connect(context.TODO(), opts)
53+
54+
.. tab:: ClientOptions
55+
:tabid: mongoclientoptions
56+
57+
To enable compression by specifying a `ClientOptions
58+
<{+api+}/mongo/options#ClientOptions>`__ instance, pass one or more
59+
compression algorithms to the ``SetCompressors()`` method as a string
60+
array:
61+
62+
.. code-block:: go
63+
:emphasize-lines: 1
64+
65+
opts := options.Client().SetCompressors([]string{"snappy", "zlib", "zstd"})
66+
client, _ := mongo.Connect(context.TODO(), opts)
67+
68+
Specify compression algorithms by using the following strings:
69+
70+
- ``"snappy"`` for `Snappy <https://google.github.io/snappy/>`__ compression
71+
- ``"zlib"`` for `Zlib <https://zlib.net/>`__ compression
72+
- ``"zstd"`` for `Zstandard <https://github.com/facebook/zstd/>`__ compression
73+
74+
.. _compression-dependencies:
75+
76+
Compression Algorithm Dependencies
77+
----------------------------------
78+
79+
To add the Snappy compression algorithm to your application, run the
80+
following code:
81+
82+
.. code-block:: bash
83+
84+
go get github.com/golang/snappy
85+
86+
To add the Zstandard compression algorithm to your application, run the
87+
following code:
88+
89+
.. code-block:: bash
90+
91+
go get -u github.com/klauspost/compress
92+
93+
To add the Zlib compression algorithm to your application, import the built-in
94+
``zlib`` package. You must add the following import statement to application files
95+
that instantiate a ``Client`` with Zlib compression:
96+
97+
.. code-block:: go
98+
99+
import "compress/zlib"
100+
101+
Additional Information
102+
----------------------
103+
104+
For more information about the concepts in this guide, see the following
105+
documentation:
106+
107+
- :ref:`golang-connection-uri` in the Connection Guide
108+
- :manual:`Connection String Compression Options </reference/connection-string/#compression-options>`
109+
in the Server manual
110+
- `The zlib package <https://pkg.go.dev/compress/zlib>`__ Go documentation
111+
112+
API Documentation
113+
~~~~~~~~~~~~~~~~~
114+
115+
- `SetCompressors() <{+api+}/mongo/options#ClientOptions.SetCompressors>`__
116+
- `ClientOptions <{+api+}/mongo/options#ClientOptions>`__

0 commit comments

Comments
 (0)