Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js

FAQ

On this page

This page contains frequently asked questions and their corresponding answers.

Tip

If you can't find an answer to your problem on this page, see the Issues & Help page for next steps and more resources.

Every MongoClient instance has a built-in connection pool for each server in your MongoDB topology. Connection pools open sockets on demand to support concurrent requests to MongoDB in your application.

The maximum size of each connection pool is set by the maxPoolSize option, which defaults to 100. If the number of in-use connections to a server reaches the value of maxPoolSize, the next request to that server will wait until a connection becomes available.

In addition to the sockets needed to support your application's requests, each MongoClient instance opens two additional sockets per server in your MongoDB topology for monitoring the server's state. For example, a client connected to a three-node replica set opens six monitoring sockets. If the application uses the default setting for maxPoolSize and only queries the primary (default) node, then there can be at most 106 total connections in the connection pool. If the application uses a read preference to query the secondary nodes, those connection pools grow and there can be 306 total connections.

To support high numbers of concurrent MongoDB requests within one process, you can increase maxPoolSize.

Connection pools are rate-limited. The maxConnecting option determines the number of connections that the pool can create in parallel at any time. For example, if the value of maxConnecting is 2, the third request that attempts to concurrently check out a connection succeeds only when one the following cases occurs:

  • The connection pool finishes creating a connection and there are fewer than maxPoolSize connections in the pool.

  • An existing connection is checked back into the pool.

  • The driver's ability to reuse existing connections improves due to rate-limits on connection creation.

You can set the minimum number of concurrent connections to each server with the minPoolSize option, which defaults to 0. The driver initializes the connection pool with this number of sockets. If sockets are closed, causing the total number of sockets (both in use and idle) to drop below the minimum, more sockets are opened until the minimum is reached.

You can set the maximum number of milliseconds that a connection can remain idle in the pool by setting the maxIdleTimeMS option. Once a connection has been idle for maxIdleTimeMS, the connection pool removes and replaces it. This option defaults to 0 (no limit).

The following default configuration for a MongoClient works for most applications:

const client = new MongoClient("<connection string>");

MongoClient supports multiple concurrent requests. For each process, create a client and reuse it for all operations in a process. This practice is more efficient than creating a client for each request.

The driver does not limit the number of requests that can wait for sockets to become available, and it is the application's responsibility to limit the size of its pool to bound queuing during a load spike. Requests wait for the amount of time specified in the waitQueueTimeoutMS option, which defaults to 0 (no limit).

A request that waits more than the length of time defined by waitQueueTimeoutMS for a socket raises a connection error. Use this option if it is more important to bound the duration of operations during a load spike than it is to complete every operation.

When MongoClient.close() is called by any request, the driver closes all idle sockets and closes all sockets that are in use as they are returned to the pool. Calling MongoClient.close() closes only inactive sockets, so you cannot interrupt or terminate any ongoing operations by using this method. The driver closes these sockets only when the process completes.

Setting
Description
connectTimeoutMS

connectTimeoutMS is a connection option that sets the time, in milliseconds, for an individual connection from your connection pool to establish a TCP connection to the MongoDB server before timing out.

Tip

To modify the allowed time for MongoClient.connect to establish a connection to a MongoDB server, use the serverSelectionTimeoutMS option instead.

Default: 30000

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 specifies 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. You can pass maxTimeMS only to an individual operation or to a cursor.

To specify the optional settings for your MongoClient, declare one or more available settings in the options object of the constructor as follows:

const client = new MongoClient(uri, {
connectTimeoutMS: <integer value>,
socketTimeoutMS: <integer value>
});

To see all the available settings, see the MongoClientOptions API Documentation.

To specify maxTimeMS, chain the maxTimeMS() method with a timeout specification to an operation that returns a Cursor:

const cursor = collection.find({}).maxTimeMS(50);

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.

Starting in ​ version 4.2, the server terminates running operations such as aggregations and find operations if the client disconnects. To see a full list of operations affected by this behavior, see the Server version 4.2 release notes in the Server manual.

Other operations, such as write operations, continue to run on the ​ even if the client disconnects. This behavior can cause data inconsistencies if your application retries the operation after the client disconnects.

If you experience unexpected network behavior or if a MongoDB process fails with an error, you may not receive confirmation that the driver correctly closed the corresponding socket.

To make sure that the driver correctly closes the socket in these cases, set the socketTimeoutMS option. When a MongoDB process times out, the driver will close the socket. We recommend that you select a value for socketTimeoutMS that is two to three times as long as the expected duration of the slowest operation that your application executes.

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. To avoid excessive socket timeouts, reduce the number of connections that the driver can maintain in the connection pool by specifying the maxPoolSize option.

To specify the optional maxPoolSize setting for your MongoClient, declare it in the options object of the constructor as follows:

const client = new MongoClient(uri, {
maxPoolSize: <integer value>,
});

If you set the value of connectTimeoutMS or socketTimeoutMS to 0, your application will use the operating system's default socket timeout value.

You can prevent long-running operations from slowing down the server by specifying a timeout value. You can chain the maxTimeMS() method to an operation that returns a Cursor to set a timeout on a specific action.

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) => {});

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 .

Warning

If a firewall ignores or drops the keepAlive packets this may not work

Internal firewalls that exist between application servers and MongoDB are often misconfigured and are overly aggressive in their removal of socket connections.

If you experience unexpected network behavior, here are some things to check:

  1. The firewall should send a FIN packet when closing a socket,allowing the driver to detect that the socket is closed.

  2. The firewall should allow keepAlive probes.

In most operating systems, each connection is associated with a file descriptor. There is typically a limit set by the operating system on the number of file descriptors used by a single process. An ECONNRESET error can occur if the connection pool size surpasses the limit of file descriptors.

Consider the following operation:

1const uri = "mongodb://localhost:27017/test?maxPoolSize=5000";
2// create a new MongoClient
3const client = new MongoClient(uri, {
4 useNewUrlParser: true,
5 useUnifiedTopology: true,
6});
7
8await client.connect(err => {
9 // connection
10});

If this operation causes an ECONNRESET error, you may have run into the file descriptor limit for your Node.js process. In that case you must increase the number of file descriptors for the Node.js process. On MacOS and Linux you do this with the ulimit shell command.

ulimit -n 6000

This sets the maximum number of file descriptors for the process to 6000, allowing Node.js to connect with a pool size of 5000 sockets.

To control the maximum size of a connection pool, you can set the maxPoolSize option in the connection options. The default value of maxPoolSize is 100. If the number of in-use connections to a server reaches maxPoolSize, the next request to that server will wait until a connection becomes available. To prevent long-running operations from slowing down your application, you can increase maxPoolSize.

The driver does not limit the number of requests that can wait for sockets to become available. Requests wait for the amount of time specified in the waitQueueTimeoutMS option, which defaults to 0 (no limit). You should set this option if it is more important to stop long-running operations than it is to complete every operation.

Tip

To learn more about connection pooling, see How Does Connection Pooling Work in the Node Driver?.

The connection string passed to the driver must use exact hostnames for the servers as set in the Replica Set Config. Given the following configuration settings for your Replica Set, in order for the Replica Set discovery and failover to work the driver should be able to reach server1, server2, and server3.

{
"_id": "testSet",
"version": 1,
"protocolVersion": 1,
"members": [
{
"_id": 1,
"host": "server1:31000"
},
{
"_id": 2,
"host": "server2:31001"
},
{
"_id": 3,
"host": "server3:31002"
}
]
}

If you are unable to find the answer to your question here, try our forums and support channels listed in the Issues and Help section.

←  Undefined ValuesIssues & Help →