Skip to content

Commit 8693bb9

Browse files
committed
DOCSP-28558: connection pool faq + reuse client tip (#636)
* DOCSP-28558: connection pool + reuse client * first pass fixes * DB PR fixes 1 * small fixes * fixes * wording fix * capitalization (cherry picked from commit 9343c30) (cherry picked from commit 9c84549)
1 parent d94e452 commit 8693bb9

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed

source/faq.txt

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,104 @@
1+
.. _node-faq:
2+
13
===
24
FAQ
35
===
46

5-
.. default-domain:: mongodb
6-
7-
Frequently Asked Questions
8-
97
.. contents:: On this page
108
:local:
119
:backlinks: none
12-
:depth: 2
10+
:depth: 1
1311
:class: singlecol
1412

13+
This page contains frequently asked questions and their corresponding answers.
14+
15+
.. tip::
16+
17+
If you can't find an answer to your problem on this page,
18+
see the :ref:`node-issues-help` page for next steps and more
19+
resources.
20+
21+
.. _node-faq-connection-pool:
22+
23+
How Does Connection Pooling Work in the Node Driver?
24+
----------------------------------------------------
25+
26+
Every ``MongoClient`` instance has a built-in connection pool for each server
27+
in your MongoDB topology. Connection pools open sockets on demand to
28+
support concurrent requests to MongoDB in your application.
29+
30+
The maximum size of each connection pool is set by the ``maxPoolSize`` option, which
31+
defaults to ``100``. If the number of in-use connections to a server reaches
32+
the value of ``maxPoolSize``, the next request to that server will wait
33+
until a connection becomes available.
34+
35+
In addition to the sockets needed to support your application's requests,
36+
each ``MongoClient`` instance opens two additional sockets per server
37+
in your MongoDB topology for monitoring the server's state.
38+
For example, a client connected to a three-node replica set opens six
39+
monitoring sockets. If the application uses the default setting for
40+
``maxPoolSize`` and only queries the primary (default) node, then
41+
there can be at most ``106`` total connections in the connection pool. If the
42+
application uses a :ref:`read preference <read-preference>` to query the
43+
secondary nodes, those connection pools grow and there can be
44+
``306`` total connections.
45+
46+
To support high numbers of concurrent MongoDB requests
47+
within one process, you can increase ``maxPoolSize``.
48+
49+
Connection pools are rate-limited. The ``maxConnecting`` option
50+
determines the number of connections that the pool can create in
51+
parallel at any time. For example, if the value of ``maxConnecting`` is
52+
``2``, the third request that attempts to concurrently check out a
53+
connection succeeds only when one the following cases occurs:
54+
55+
- The connection pool finishes creating a connection and there are fewer
56+
than ``maxPoolSize`` connections in the pool.
57+
- An existing connection is checked back into the pool.
58+
- The driver's ability to reuse existing connections improves due to
59+
rate-limits on connection creation.
60+
61+
You can set the minimum number of concurrent connections to
62+
each server with the ``minPoolSize`` option, which defaults to ``0``.
63+
The driver initializes the connection pool with this number of sockets. If
64+
sockets are closed, causing the total number
65+
of sockets (both in use and idle) to drop below the minimum, more
66+
sockets are opened until the minimum is reached.
67+
68+
You can set the maximum number of milliseconds that a connection can
69+
remain idle in the pool by setting the ``maxIdleTimeMS`` option.
70+
Once a connection has been idle for ``maxIdleTimeMS``, the connection
71+
pool removes and replaces it. This option defaults to ``0`` (no limit).
72+
73+
The following default configuration for a ``MongoClient`` works for most
74+
applications:
75+
76+
.. code-block:: js
77+
78+
const client = new MongoClient("<connection string>");
79+
80+
``MongoClient`` supports multiple concurrent requests. For each process,
81+
create a client and reuse it for all operations in a process. This
82+
practice is more efficient than creating a client for each request.
83+
84+
The driver does not limit the number of requests that
85+
can wait for sockets to become available, and it is the application's
86+
responsibility to limit the size of its pool to bound queuing
87+
during a load spike. Requests wait for the amount of time specified in
88+
the ``waitQueueTimeoutMS`` option, which defaults to ``0`` (no limit).
89+
90+
A request that waits more than the length of time defined by
91+
``waitQueueTimeoutMS`` for a socket raises a connection error. Use this
92+
option if it is more important to bound the duration of operations
93+
during a load spike than it is to complete every operation.
94+
95+
When ``MongoClient.close()`` is called by any request, the driver
96+
closes all idle sockets and closes all sockets that are in
97+
use as they are returned to the pool. Calling ``MongoClient.close()``
98+
closes only inactive sockets, so you cannot interrupt or terminate
99+
any ongoing operations by using this method. The driver closes these
100+
sockets only when the process completes.
101+
15102
How to fix a "MongoServerSelectionError: connect ECONNREFUSED ::1:27017" error?
16103
-------------------------------------------------------------------------------
17104

@@ -41,7 +128,6 @@ options to resolve this error:
41128
family: 4,
42129
});
43130

44-
45131
What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTimeMS"?
46132
-------------------------------------------------------------------------------------
47133

source/fundamentals/connection/connect.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ You must create a client to connect to a MongoDB deployment on Atlas. To create
6464
a client, construct an instance of ``MongoClient``, passing in your
6565
URI and a ``MongoClientOptions`` object.
6666

67+
.. tip:: Reuse Your Client
68+
69+
As each ``MongoClient`` represents a pool of connections to the
70+
database, most applications only require a single instance of a
71+
``MongoClient``, even across multiple requests. To learn more about
72+
how connection pools work in the driver, see the :ref:`FAQ page
73+
<node-faq-connection-pool>`.
74+
6775
Use the ``serverApi`` option in your ``MongoClientOptions`` object to
6876
enable the {+stable-api+} feature, which forces the server to run operations
6977
with behavior compatible with the specified API version.

source/fundamentals/transactions.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ You can use the driver to perform multi-document transactions.
4646

4747
In MongoDB, multi-document transactions run within a **client session**.
4848
A client session is a grouping of related read or write operations that
49-
you want to ensure run sequentially. When combined with majority read and
49+
you want to ensure run sequentially. We recommend you reuse
50+
your client for multiple sessions and transactions instead of
51+
instantiating a new client each time.
52+
53+
When combined with majority read and
5054
write concerns, the driver can guarantee causal consistency between the
5155
operations. See the server manual guide on
5256
:manual:`Client Sessions and Causal Consistency Guarantees </core/read-isolation-consistency-recency/#std-label-sessions>`

source/issues-and-help.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _node-issues-help:
2+
13
=============
24
Issues & Help
35
=============

0 commit comments

Comments
 (0)