|
5 | 5 | .. default-domain:: mongodb
|
6 | 6 |
|
7 | 7 | Frequently Asked Questions
|
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | + |
| 16 | +What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTimeMS" ? |
| 17 | +-------------------------------------------------------------------------------------------- |
| 18 | + |
| 19 | +.. list-table:: |
| 20 | + :header-rows: 1 |
| 21 | + |
| 22 | + * - Setting |
| 23 | + - Default Value |
| 24 | + - Description |
| 25 | + * - connectTimeoutMS |
| 26 | + - 30000 |
| 27 | + - ``connectTimeoutMS`` is a `connection-setting <https://mongodb.github.io/node-mongodb-native/3.5/reference/connecting/connection-settings/>`_ option that sets the time in milliseconds to establish a connection to the MongoDB server before timing out. |
| 28 | + * - socketTimeoutMS |
| 29 | + - 360000 |
| 30 | + - ``socketTimeoutMS`` is a ``connection-settings`` option that sets the number of milliseconds a socket stays inactive after the driver has successfully connected before closing the connection. |
| 31 | + * - maxTimeMS |
| 32 | + - N/A |
| 33 | + - :node-api:`maxTimeMS() </Cursor.html#maxTimeMS>` is a :manual:`cursor method </reference/method/js-cursor>` that specifies the max time limit for processing an operation on a cursor. If an operation runs over the time allotted it will return a timeout error. |
| 34 | + |
| 35 | +How Can I Prevent the Driver From Hanging During Connection or From Spending Too Long Trying to Reach Unreachable Replica Sets? |
| 36 | +------------------------------------------------------------------------------------------------------------------------------- |
| 37 | + |
| 38 | +To prevent the driver from hanging during connection or to prevent the |
| 39 | +driver from spending too long trying to reach unreachable replica sets, |
| 40 | +you can set the ``connectTimeoutMS`` option of your |
| 41 | +``connection-settings``. Generally, you should ensure that the |
| 42 | +``connectTimeoutMS`` setting is not lower than the longest network |
| 43 | +latency you have to a member of the set. If one of the secondary members |
| 44 | +is on the other side of the planet and has a latency of 10000 |
| 45 | +milliseconds, setting the ``connectTimeoutMS`` to anything lower will |
| 46 | +prevent the driver from ever connecting to that member. |
| 47 | + |
| 48 | +Should I Use "socketTimeoutMS" as a Way of Preventing Long-Running Operations From Slowing Down the Server? |
| 49 | +------------------------------------------------------------------------------------------------------------- |
| 50 | + |
| 51 | +No, you should **not** use ``socketTimeoutMS`` to end operations that |
| 52 | +may run for too long and slow down the application. Attempting to do so |
| 53 | +may not achieve the intended result. |
| 54 | + |
| 55 | +Closing the socket causes a reconnect of the driver's connection pool, |
| 56 | +introducing latency to any other queued up operations. Chronically slow |
| 57 | +operations will, therefore, cause a large number of reconnect requests, |
| 58 | +negatively impacting throughput and performance. |
| 59 | + |
| 60 | +Also, closing the socket does not terminate the operation; it will |
| 61 | +continue to run on the MongoDB server, which could cause data |
| 62 | +inconsistencies if the application retries the operation on failure. |
| 63 | + |
| 64 | +However, there are important use cases for ``socketTimeoutMS`` - |
| 65 | +consider the following cases: |
| 66 | + |
| 67 | +- A MongoDB process erroring out |
| 68 | +- A misconfigured firewall causing a socket connection without sending a ``FIN`` packet |
| 69 | + |
| 70 | +In those cases, there is no way to detect that the connection has died. |
| 71 | +Setting the ``socketTimeoutMS`` is essential to ensure that the sockets |
| 72 | +are closed correctly. A good rule of thumb is to set ``socketTimeoutMS`` |
| 73 | +to two to three times the length of the slowest operation that runs |
| 74 | +through the driver. |
| 75 | + |
| 76 | +How Can I Prevent Sockets From Timing out Before They Become Active? |
| 77 | +-------------------------------------------------------------------- |
| 78 | +Having a large connection pool does not always reduce reconnection |
| 79 | +requests. Consider the following example: |
| 80 | + |
| 81 | +An application has a connection pool size of 5 sockets and has the |
| 82 | +``socketTimeoutMS`` option set to 5000 milliseconds. Operations occur, |
| 83 | +on average, every 3000 milliseconds, and reconnection requests are |
| 84 | +frequent. Each socket times out after 5000 milliseconds, which means |
| 85 | +that all sockets must do something during those 5000 milliseconds to |
| 86 | +avoid closing. |
| 87 | + |
| 88 | +One message every 3000 milliseconds is not enough to keep the sockets |
| 89 | +active, so several of the sockets will time out after 5000 milliseconds. |
| 90 | +Reduce the ``poolSize`` in the connection settings to fix the problem. |
| 91 | + |
| 92 | +What Does a Value of "0" mean for "connectTimeoutMS" and "socketTimeoutMS"? |
| 93 | +--------------------------------------------------------------------------- |
| 94 | + |
| 95 | +Setting ``connectTimeoutMS`` and ``socketTimeoutMS`` to the value ``0`` has |
| 96 | +causes the application to use the operating system's default socket timeout value. |
| 97 | + |
| 98 | +How Can I Prevent Long-Running Operations From Slowing Down the Server? |
| 99 | +----------------------------------------------------------------------- |
| 100 | + |
| 101 | +By using ``maxTimeMS()`` you can prevent long-running operations from |
| 102 | +slowing down the server. This method allows the MongoDB server to cancel |
| 103 | +operations that run for more than the set value of ``maxTimeMS``. |
| 104 | + |
| 105 | +The following example demonstrates how to use MaxTimeMS with a find |
| 106 | +operation: |
| 107 | + |
| 108 | +.. literalinclude:: /code-snippets/faq/maxTimeMS-example.js |
| 109 | + :language: javascript |
| 110 | + :linenos: |
| 111 | + |
| 112 | +What Does the "keepAlive" Setting Do? |
| 113 | +--------------------------------------- |
| 114 | + |
| 115 | +``keepAlive`` is a ``connection-setting`` that sets the number of |
| 116 | +milliseconds to wait before initiating a :wikipedia:`TLS keepAlive |
| 117 | +<Keepalive#TCP_keepalive>` on a TCP Socket. The ``keepAlive`` option |
| 118 | +will keep a socket alive by sending periodic probes to MongoDB. However, |
| 119 | +this only works if the operating system supports ``SO_KEEPALIVE``\ . |
| 120 | + |
| 121 | +.. warning:: |
| 122 | + If a firewall ignores or drops the ``keepAlive`` packets this may not work |
| 123 | + |
| 124 | +What Can I Do If I'm Experiencing Unexpected Network Behavior? |
| 125 | +-------------------------------------------------------------- |
| 126 | +Internal firewalls that exist between application servers and MongoDB |
| 127 | +are often misconfigured and are overly aggressive in their removal of |
| 128 | +socket connections. |
| 129 | + |
| 130 | +If you experience unexpected network behavior, here |
| 131 | +are some things to check: |
| 132 | + |
| 133 | +#. The firewall should send a ``FIN packet`` when closing a socket,allowing the driver to detect that the socket is closed. |
| 134 | +#. The firewall should allow ``keepAlive`` probes. |
| 135 | + |
| 136 | +What Can I Do If I'm Getting "ECONNRESET" When Calling "client.connect()"? |
| 137 | +------------------------------------------------------------------------------ |
| 138 | + |
| 139 | +In most operating systems, each connection is associated with a `file |
| 140 | +descriptor |
| 141 | +<https://www.computerhope.com/jargon/f/file-descriptor.htm>`_. There is |
| 142 | +typically a limit set by the operating system on the number of file |
| 143 | +descriptors used by a single process. An ``ECONNRESET`` error can occur |
| 144 | +if the connection pool size surpasses the limit of ``file descriptors``. |
| 145 | + |
| 146 | +Consider the following operation: |
| 147 | + |
| 148 | +.. literalinclude:: /code-snippets/faq/econnresetWithClientConnect-example.js |
| 149 | + :language: javascript |
| 150 | + :linenos: |
| 151 | + |
| 152 | +If this operation causes an ``ECONNRESET`` error, you may have run into |
| 153 | +the ``file descriptor`` limit for your Node.js process. In that case you |
| 154 | +must increase the number of ``file descriptors`` for the Node.js process. On |
| 155 | +MacOS and Linux you do this with the `ulimit |
| 156 | +<https://ss64.com/bash/ulimit.html>`_ shell command. |
| 157 | + |
| 158 | +.. code-block:: sh |
| 159 | + |
| 160 | + ulimit -n 6000 |
| 161 | + |
| 162 | +This sets the maximum number of ``file descriptors`` for the process to |
| 163 | +6000, allowing Node.js to connect with a pool size of 5000 sockets. |
| 164 | + |
| 165 | +How Can I Prevent a Slow Operation From Delaying Other Operations? |
| 166 | +------------------------------------------------------------------ |
| 167 | + |
| 168 | +A slow operation may delay your other operations that occur after it, if |
| 169 | +the ``poolSize`` has not been set in the `connection settings |
| 170 | +<https://mongodb.github.io/node-mongodb-native/3.5/reference/connecting/connection-settings/>`_. |
| 171 | +MongoDB is synchronous and uses a single execution thread per socket, |
| 172 | +meaning that MongoDB will execute one single operation per socket at any |
| 173 | +point in time. Any other operation sent to that socket will have to wait |
| 174 | +until the current operation is finished. If you have a slow-running |
| 175 | +operation that holds up other operations, the best solution is to create |
| 176 | +a separate connection pool for the slow operation, isolating it from |
| 177 | +other, faster operations. |
| 178 | + |
| 179 | +.. note:: |
| 180 | + If the number of operations is greater than the set ``poolSize`` and |
| 181 | + a slow operation occurs, subsequent operations will be delayed. |
| 182 | + |
| 183 | +How Can I Ensure My Connection String Is Valid for a Replica Set? |
| 184 | +----------------------------------------------------------------- |
| 185 | + |
| 186 | +The connection string passed to the driver must use exact hostnames for |
| 187 | +the servers as set in the :manual:`Replica Set Config </reference/replica-configuration/>`. |
| 188 | +Given the following configuration settings for your `Replica Set`, in |
| 189 | +order for the `Replica Set` discovery and :manual:`failover |
| 190 | +</reference/glossary/#term-failover>` to work the driver should be able |
| 191 | +to reach ``server1``, ``server2``, and ``server3``. |
| 192 | + |
| 193 | + .. code-block:: JSON |
| 194 | + |
| 195 | + { |
| 196 | + "_id": "testSet", |
| 197 | + "version": 1, |
| 198 | + "protocolVersion": 1, |
| 199 | + "members": [ |
| 200 | + { |
| 201 | + "_id": 1, |
| 202 | + "host": "server1:31000" |
| 203 | + }, |
| 204 | + { |
| 205 | + "_id": 2, |
| 206 | + "host": "server2:31001" |
| 207 | + }, |
| 208 | + { |
| 209 | + "_id": 3, |
| 210 | + "host": "server3:31002" |
| 211 | + } |
| 212 | + ] |
| 213 | + } |
0 commit comments