1
1
.. _node-connection-pools:
2
2
3
- ================
4
- Connection Pools
5
- ================
3
+ ========================================
4
+ Manage Connections with Connection Pools
5
+ ========================================
6
6
7
7
.. facet::
8
8
:name: genre
@@ -31,62 +31,167 @@ are created by {+driver-short+}.
31
31
32
32
.. _node-faq-connection-pool:
33
33
34
- Connection Pool Overview
35
- -------------------------
36
-
37
- Every ``MongoClient`` instance has a built-in connection pool for each server
38
- in your MongoDB topology. Connection pools open sockets on demand to
39
- support concurrent requests to MongoDB in your application.
40
-
41
- The maximum size of each connection pool is set by the ``maxPoolSize`` option, which
42
- defaults to ``100``. If the number of in-use connections to a server reaches
43
- the value of ``maxPoolSize``, the next request to that server will wait
44
- until a connection becomes available.
45
-
46
- In addition to the sockets needed to support your application's requests,
47
- each ``MongoClient`` instance opens two more sockets per server
48
- in your MongoDB topology for monitoring the server's state.
49
- For example, a client connected to a three-node replica set opens six
50
- monitoring sockets. If the application uses the default setting for
51
- ``maxPoolSize`` and only queries the primary (default) node, then
52
- there can be at most ``106`` total connections in the connection pool. If the
53
- application uses a :ref:`read preference <read-preference>` to query the
54
- secondary nodes, those connection pools grow and there can be
55
- ``306`` total connections.
34
+ Configure Connection Pools
35
+ --------------------------
36
+
37
+ Every ``MongoClient`` instance has a built-in connection pool for each server in
38
+ your MongoDB topology. If you do not configure the ``minPoolSize`` option,
39
+ connection pools open sockets on demand to support concurrent requests to
40
+ MongoDB in your application.
41
+
42
+ You can specify the following connection pool settings in your ``MongoClient``
43
+ instance:
44
+
45
+ .. list-table::
46
+ :widths: 30 70
47
+ :header-rows: 1
48
+
49
+ * - Setting
50
+ - Description
51
+
52
+ * - ``maxPoolSize``
53
+ - | The maximum number of concurrent connections that the pool maintains.
54
+ If the number of in-use connections to a server reaches the specified
55
+ value, the next request to that server waits until a connection becomes
56
+ available.
57
+ |
58
+ | **Default**: ``100``
59
+
60
+ * - ``maxConnecting``
61
+ - | The maximum number of connections that each pool can establish
62
+ concurrently.
63
+
64
+ * - ``minPoolSize``
65
+ - | The minimum number of concurrent connections that the pool maintains.
66
+ |
67
+ | **Default**: ``0``
68
+
69
+ * - ``maxIdleTimeMS``
70
+ - | The maximum number of milliseconds that a connection can remain idle in
71
+ the pool.
72
+ |
73
+ | **Default**: ``0`` (no limit)
74
+
75
+ * - ``waitQueueTimeoutMS``
76
+ - | The maximum number of milliseconds that a request can wait for a socket
77
+ to become available.
78
+ |
79
+ | **Default**: ``0`` (no limit)
80
+
81
+
82
+ .. _node-connection-pool-max-pool-size:
83
+
84
+ maxPoolSize
85
+ ~~~~~~~~~~~
86
+
87
+ In addition to the sockets needed to support your application's requests, each
88
+ ``MongoClient`` instance opens up to two connections per server in your
89
+ MongoDB topology for monitoring the server's state.
90
+
91
+ For example, a client connected to a three-node replica set opens six monitoring
92
+ sockets. If the application uses the default setting for ``maxPoolSize`` and
93
+ only queries the primary (default) node, then there can be at most ``106`` open
94
+ sockets and ``100`` connections in the connection pool. If the application uses
95
+ a :ref:`read preference <read-preference>` to query the secondary nodes, those
96
+ connection pools grow and there can be ``306`` total connections including the
97
+ open monitoring sockets.
56
98
57
99
To support high numbers of concurrent MongoDB requests
58
100
within one process, you can increase ``maxPoolSize``.
59
101
60
- Connection pools are rate-limited. The ``maxConnecting`` option
61
- determines the number of connections that the pool can create in
62
- parallel at any time. For example, if the value of ``maxConnecting`` is
63
- ``2``, the third request that attempts to concurrently check out a
64
- connection succeeds only when one the following cases occurs:
102
+ The following code creates a ``MongoClient`` instance with a maximum connection
103
+ pool size of ``200`` by specifying the ``maxPoolSize`` option in the
104
+ ``options`` object:
105
+
106
+ .. code-block:: javascript
107
+
108
+ const { MongoClient } = require('mongodb');
109
+
110
+ const uri = '<connection-string>';
111
+ const client = new MongoClient(uri, {
112
+ maxPoolSize: 200
113
+ });
114
+
115
+ .. _node-connection-pool-max-connecting:
116
+
117
+ maxConnecting
118
+ ~~~~~~~~~~~~~
119
+
120
+ Connection pools rate-limit connection establishment. The ``maxConnecting``
121
+ option determines the number of connections that the pool can create in parallel
122
+ at any time. For example, if the value of ``maxConnecting`` is ``2``, the third
123
+ request that attempts to concurrently check out a connection succeeds only when
124
+ one the following cases occurs:
65
125
66
126
- The connection pool finishes creating a connection and there are fewer
67
127
than ``maxPoolSize`` connections in the pool.
68
128
- An existing connection is checked back into the pool.
69
- - The driver's ability to reuse existing connections improves due to
70
- rate-limits on connection creation.
71
129
72
- You can set the minimum number of concurrent connections to
73
- each server with the ``minPoolSize`` option, which defaults to ``0``.
74
- The driver initializes the connection pool with this number of sockets. If
75
- sockets are closed, causing the total number
76
- of sockets (both in use and idle) to drop below the minimum, more
77
- sockets are opened until the minimum is reached.
130
+ The following code creates a ``MongoClient`` instance with a maximum number of
131
+ ``2`` connections to be established concurrently per pool by specifying the
132
+ ``maxConnecting`` option in the ``options`` object:
133
+
134
+ .. code-block:: javascript
135
+
136
+ const { MongoClient } = require('mongodb');
137
+
138
+ const uri = '<connection-string>';
139
+ const client = new MongoClient(uri, {
140
+ maxConnecting: 2
141
+ });
142
+
143
+ .. _node-connection-pool-min-pool-size:
144
+
145
+ minPoolSize
146
+ ~~~~~~~~~~~
147
+
148
+ You can set the minimum number of connections to each server with the
149
+ ``minPoolSize`` option. The driver ensures that there are always at least the
150
+ number of connections set by the ``minPoolSize`` option in the connection pool.
151
+ If sockets are closed, causing the total number of sockets (both in use and
152
+ idle) to drop below the minimum, more sockets are opened until the minimum is
153
+ reached.
154
+
155
+ The following code creates a ``MongoClient`` instance with a minimum connnection
156
+ pool size of ``10`` by specifying the ``minPoolSize`` option in the ``options``
157
+ object:
158
+
159
+ .. code-block:: javascript
160
+
161
+ const { MongoClient } = require('mongodb');
162
+
163
+ const uri = '<connection-string>';
164
+ const client = new MongoClient(uri, {
165
+ minPoolSize: 10
166
+ });
167
+
168
+ .. _node-connection-pool-max-idle-time:
169
+
170
+ maxIdleTimeMS
171
+ ~~~~~~~~~~~~~
78
172
79
173
You can set the maximum number of milliseconds that a connection can
80
174
remain idle in the pool by setting the ``maxIdleTimeMS`` option.
81
175
Once a connection has been idle for ``maxIdleTimeMS``, the connection
82
176
pool removes and replaces it. This option defaults to ``0`` (no limit).
83
177
84
- The following default configuration for a ``MongoClient`` works for most
85
- applications:
178
+ The following code creates a ``MongoClient`` instance with a maximum idle time
179
+ of ``10000`` milliseconds (10 seconds) by specifying the ``maxIdleTimeMS``
180
+ setting in the ``options`` object:
181
+
182
+ .. code-block:: javascript
183
+
184
+ const { MongoClient } = require('mongodb');
185
+
186
+ const uri = '<connection-string>';
187
+ const client = new MongoClient(uri, {
188
+ maxIdleTimeMS: 10000
189
+ });
190
+
191
+ .. _node-connection-pool-wait-queue-timeout:
86
192
87
- .. code-block:: js
88
-
89
- const client = new MongoClient("<connection string>");
193
+ waitQueueTimeoutMS
194
+ ~~~~~~~~~~~~~~~~~~
90
195
91
196
``MongoClient`` supports multiple concurrent requests. For each process,
92
197
create a client and reuse it for all operations in a process. This
@@ -103,39 +208,72 @@ A request that waits more than the length of time defined by
103
208
option if it is more important to bound the duration of operations
104
209
during a load spike than it is to complete every operation.
105
210
106
- When ``MongoClient.close()`` is called by any request, the driver
107
- closes all idle sockets and closes all sockets that are in
108
- use as they are returned to the pool. Calling ``MongoClient.close()``
109
- closes only inactive sockets and does not directly terminate
110
- any ongoing operations. The driver closes any in-use sockets only when
111
- the associated operations complete. However, the ``MongoClient.close()``
112
- method does close existing sessions and transactions, which might indirectly
113
- affect the behavior of ongoing operations and open cursors.
211
+ The following code creates a ``MongoClient`` instance with a maximum wait queue
212
+ timeout of ``10000`` milliseconds (10 seconds) by declaring it in the
213
+ ``options`` object:
214
+
215
+ .. code-block:: javascript
216
+
217
+ const { MongoClient } = require('mongodb');
218
+
219
+ const uri = '<connection-string>';
220
+ const client = new MongoClient(uri, {
221
+ waitQueueTimeoutMS: 10000
222
+ });
223
+
224
+ Closing Connections
225
+ --------------------
226
+
227
+ When any request calls ``MongoClient.close()``, the {+driver-short+} performs
228
+ the following actions:
229
+
230
+ - Closes all idle sockets in the connection pool
231
+ - Closes all sockets that are in use as they are returned to the pool
232
+ - Closes all sockets that are in use only when the associated operations
233
+ complete
234
+
235
+ Calling ``MongoClient.close()`` closes only inactive sockets and does not
236
+ directly terminate any ongoing operations.
237
+
238
+ .. note::
239
+
240
+ The ``MongoClient.close()`` method does close existing sessions and
241
+ transactions, which might indirectly affect the behavior of ongoing
242
+ operations and open cursors.
114
243
115
244
Avoid Socket Timeouts
116
245
---------------------
117
246
118
247
Having a large connection pool does not always reduce reconnection
119
- requests. Consider the following example:
248
+ requests. Consider the following example scenario :
120
249
121
- An application has a connection pool size of 5 sockets and has the
122
- ``socketTimeoutMS`` option set to 5000 milliseconds. Operations occur,
123
- on average, every 3000 milliseconds, and reconnection requests are
124
- frequent. Each socket times out after 5000 milliseconds, which means
125
- that all sockets must do something during those 5000 milliseconds to
126
- avoid closing.
250
+ - An application has a connection pool size of 5 sockets and has the
251
+ ``socketTimeoutMS`` option set to 5000 milliseconds.
252
+ - Operations occur, on average, every 3000 milliseconds, and reconnection
253
+ requests are frequent.
254
+ - Each socket times out after 5000 milliseconds, which means that all sockets
255
+ must do something during those 5000 milliseconds to avoid closing.
127
256
128
- One message every 3000 milliseconds is not enough to keep the sockets
129
- active, so several of the sockets will time out after 5000 milliseconds.
130
- To avoid excessive socket timeouts, reduce the number of connections
131
- that the driver can maintain in the connection pool by specifying the
132
- ``maxPoolSize`` option.
257
+ In this scenario, each socket times out after 5000 milliseconds, requiring
258
+ activity within this timeout period to avoid closure. However, one message every
259
+ 3000 milliseconds isn't enough to keep all sockets active, causing several of
260
+ them to time out.
133
261
134
- To specify the optional ``maxPoolSize`` setting for your ``MongoClient``, declare
135
- it in the ``options`` object of the constructor as follows:
262
+ To avoid excessive socket timeouts, reduce the number of connections that the
263
+ driver can maintain in the connection pool by specifying the ``maxPoolSize``
264
+ option. To learn how to set the ``maxPoolSize`` option, see the
265
+ :ref:`node-connection-pool-max-pool-size` section.
136
266
137
- .. code-block:: javascript
267
+ API Documentation
268
+ -----------------
138
269
139
- const client = new MongoClient(uri, {
140
- maxPoolSize: <integer value>,
141
- });
270
+ For more information about creating a ``MongoClient`` object with the
271
+ {+driver-short+} and specifying options, see the following API documentation:
272
+
273
+ - `MongoClient <{+api+}/classes/MongoClient.html>`__
274
+ - `MongoClientOptions <{+api+}/interfaces/MongoClientOptions.html>`__
275
+ - `maxPoolSize <{+api+}/interfaces/MongoClientOptions.html#maxPoolSize>`__
276
+ - `maxConnecting <{+api+}/interfaces/MongoClientOptions.html#maxConnecting>`__
277
+ - `minPoolSize <{+api+}/interfaces/MongoClientOptions.html#minPoolSize>`__
278
+ - `maxIdleTimeMS <{+api+}/interfaces/MongoClientOptions.html#maxIdleTimeMS>`__
279
+ - `waitQueueTimeoutMS <{+api+}/interfaces/MongoClientOptions.html#waitQueueTimeoutMS>`__
0 commit comments