|
| 1 | +.. _node-faq: |
| 2 | + |
1 | 3 | ===
|
2 | 4 | FAQ
|
3 | 5 | ===
|
4 | 6 |
|
5 |
| -.. default-domain:: mongodb |
6 |
| - |
7 |
| -Frequently Asked Questions |
8 |
| - |
9 | 7 | .. contents:: On this page
|
10 | 8 | :local:
|
11 | 9 | :backlinks: none
|
12 |
| - :depth: 2 |
| 10 | + :depth: 1 |
13 | 11 | :class: singlecol
|
14 | 12 |
|
| 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 | + |
15 | 102 | How to fix a "MongoServerSelectionError: connect ECONNREFUSED ::1:27017" error?
|
16 | 103 | -------------------------------------------------------------------------------
|
17 | 104 |
|
@@ -41,7 +128,6 @@ options to resolve this error:
|
41 | 128 | family: 4,
|
42 | 129 | });
|
43 | 130 |
|
44 |
| - |
45 | 131 | What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTimeMS"?
|
46 | 132 | -------------------------------------------------------------------------------------
|
47 | 133 |
|
|
0 commit comments