FAQ¶
Frequently Asked Questions
What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTimeMS"?¶
Setting | Description |
---|---|
connectTimeoutMS |
Tip To modify the allowed time for MongoClient.connect to establish a
connection to a MongoDB server, use the Default: 10000 |
socketTimeoutMS | socketTimeoutMS specifies the amount of time the driver waits
for an inactive socket before closing it. The default value is to
never time out the socket. This option applies only to sockets that
have already been connected. |
maxTimeMS | maxTimeMS is the maximum
amount of time the server should wait for an operation to complete
after it has reached the server. If an operation runs over the
specified time limit, it returns a timeout error. |
To specify the optional settings for your MongoClient
, declare one or
more of them in the options
object of the constructor as follows:
const client = new MongoClient(uri, { connectTimeoutMS: <integer value>, socketTimeoutMS: <integer value>, maxTimeMS: <integer value>, });
How Can I Prevent the Driver From Hanging During Connection or From Spending Too Long Trying to Reach Unreachable Replica Sets?¶
To prevent the driver from hanging during connection or to prevent the
driver from spending too long trying to reach unreachable replica sets,
you can set the connectTimeoutMS
option of your
connection options.
Generally, you should ensure that the
connectTimeoutMS
setting is not lower than the longest network
latency you have to a member of the set. If one of the secondary members
is on the other side of the planet and has a latency of 10000
milliseconds, setting the connectTimeoutMS
to anything lower will
prevent the driver from ever connecting to that member.
Should I Use "socketTimeoutMS" as a Way of Preventing Long-Running Operations From Slowing Down the Server?¶
No, you should not use socketTimeoutMS
to end operations that
may run for too long and slow down the application. Attempting to do so
may not achieve the intended result.
Closing the socket causes a reconnect of the driver's connection pool, introducing latency to any other queued up operations. Chronically slow operations will, therefore, cause a large number of reconnect requests, negatively impacting throughput and performance.
Also, closing the socket does not terminate the operation; it will continue to run on the MongoDB server, which could cause data inconsistencies if the application retries the operation on failure.
However, there are important use cases for socketTimeoutMS
-
consider the following cases:
- A MongoDB process erroring out
- A misconfigured firewall causing a socket connection without sending a
FIN
packet
In those cases, there is no way to detect that the connection has died.
Setting the socketTimeoutMS
is essential to ensure that the sockets
are closed correctly. A good rule of thumb is to set socketTimeoutMS
to two to three times the length of the slowest operation that runs
through the driver.
How Can I Prevent Sockets From Timing out Before They Become Active?¶
Having a large connection pool does not always reduce reconnection requests. Consider the following example:
An application has a connection pool size of 5 sockets and has the
socketTimeoutMS
option set to 5000 milliseconds. Operations occur,
on average, every 3000 milliseconds, and reconnection requests are
frequent. Each socket times out after 5000 milliseconds, which means
that all sockets must do something during those 5000 milliseconds to
avoid closing.
One message every 3000 milliseconds is not enough to keep the sockets
active, so several of the sockets will time out after 5000 milliseconds.
Reduce the poolSize
in the connection settings to fix the problem.
To specify the optional poolSize
setting for your MongoClient
, declare
it in the options
object of the constructor as follows:
const client = new MongoClient(uri, { poolSize: <integer value>,
});
What Does a Value of "0" mean for "connectTimeoutMS" and "socketTimeoutMS"?¶
If you set the value of connectTimeoutMS
or socketTimeoutMS
to
0
, your application will use the operating system's default socket
timeout value.
How Can I Prevent Long-Running Operations From Slowing Down the Server?¶
You can prevent long-running operations from slowing down the server by
specifying a timeout value. You can use the maxTimeMS
option setting in
your MongoClient
constructor to apply this to all the operations
called by the client, or chain the maxTimeMS()
method to an operation that
returns a Cursor
to apply to that specific one.
To specify the optional maxTimeMS
setting for your MongoClient
,
declare it in the options
object of the constructor as follows:
const client = new MongoClient(uri, { connectTimeoutMS: <integer value>, socketTimeoutMS: <integer value>, maxTimeMS: <integer value>, });
The following example shows how you can chain the maxTimeMS()
method
to an operation that returns a Cursor
:
// Execute a find command await collection .find({ $where: "sleep(100) || true" }) .maxTimeMS(50) .count((err, count) => {});
What Does the "keepAlive" Setting Do?¶
keepAlive
is a connection-setting
that sets the number of
milliseconds to wait before initiating a TLS keepAlive on a TCP Socket. The keepAlive
option
will keep a socket alive by sending periodic probes to MongoDB. However,
this only works if the operating system supports SO_KEEPALIVE