Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Connection Pool Overview

On this page

  • What is a Connection Pool?
  • Create and Use a Connection Pool
  • Sharded Cluster Connection Pooling
  • Connection Pool Configuration Settings

This document describes how to use a connection pool to manage connections between applications and MongoDB instances.

A connection pool is a cache of open, ready-to-use database connections maintained by the driver. Your application can seamlessly get connections from the pool, perform operations, and return connections back to the pool. Connection pools are thread-safe.

A connection pool helps reduce application latency and the number of times new connections are created.

A connection pool creates connections at startup. Applications do not need to manually return connections to the pool. Instead, connections return to the pool automatically.

Some connections are active and some are inactive but available. If your application requests a connection and there’s an available connection in the pool, a new connection does not need to be created.

Most drivers provide an object of type MongoClient.

Use one MongoClient instance per application unless the application is connecting to many separate clusters. Each MongoClient instance manages its own connection pool to the MongoDB cluster or node specified when the MongoClient is created. MongoClient objects are thread-safe in most drivers.

Note

Store your MongoClient instance in a place that is globally accessible by your application.

To use a connection pool with LDAP, see LDAP Connection Pool Behavior.

mongos routers have connection pools for each node in the cluster. The availability of connections to individual nodes within a sharded cluster affects latency. Operations must wait for a connection to be established.

You can specify connection pool settings in these locations:

  • The MongoDB URI

  • Your application's MongoClient instance

  • Your application framework's configuration files

Setting
Description
connectTimeoutMS

Most drivers default to never time out. Some versions of the Java drivers (for example, version 3.7) default to 10.

Default: 0 for most drivers. See your driver documentation.

Maximum number of connections a pool may be establishing concurrently.

maxConnecting is supported for all drivers except the Rust Driver.

Raising the value of maxConnecting allows the client to establish connection to the server faster, but increases the chance of connection storms. If the value of maxConnecting is too low, your connection pool may experience heavy throttling and increased tail latency for clients checking out connections.

Default: 2

The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed.

Default: See your driver documentation.

Maximum number of connections opened in the pool. When the connection pool reaches the maximum number of connections, new connections wait up until to the value of waitQueueTimeoutMS.

Default: 100

Minimum number of connections opened in the pool. The value of minPoolSize must be less than the value of maxPoolSize.

Default: 0

Maximum number of outbound connections each TaskExecutor connection pool can open to any given mongod instance.

Default: 2 64 - 1

Parameter only applies to sharded deployments.

Optional override for ShardingTaskExecutorPoolMaxSize to set the maximum number of outbound connections each TaskExecutor connection pool can open to a configuration server.

When set to:

  • -1, ShardingTaskExecutorPoolMaxSize is used. This is the default.

  • an integer value greater than -1, overrides the maximum number of outbound connections each TaskExecutor connection pool can open to a configuration server.

Parameter only applies to sharded deployments.

Default: -1

New in version 6.0.

Minimum number of outbound connections each TaskExecutor connection pool can open to any given mongod instance.

Default: 1

Parameter only applies to sharded deployments.

Optional override for ShardingTaskExecutorPoolMinSize to set the minimum number of outbound connections each TaskExecutor connection pool can open to a configuration server.

When set to:

  • -1, ShardingTaskExecutorPoolMinSize is used. This is the default.

  • an integer value greater than -1, overrides the minimum number of outbound connections each TaskExecutor connection pool can open to a configuration server.

Parameter only applies to sharded deployments.

Default: -1

New in version 6.0.

Number of milliseconds to wait before timeout on a TCP connection.

Do not use socketTimeoutMS as a mechanism for preventing long-running server operations.

Setting low socket timeouts may result in operations that error before the server responds.

Default: 0, which means no timeout.

Maximum wait time in milliseconds that a can thread wait for a connection to become available. A value of 0 means there is no limit.

Default: 0

←  Database Profiler OutputTuning Your Connection Pool Settings →