On this page
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 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.
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.
To configure the connection pool, set the options:
through the MongoDB URI,
programmatically when building the
in your application framework's configuration files.
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
Most drivers default to never time out. Some versions of the
Java drivers (for example, version 3.7) default to
Number of milliseconds to wait before timeout on a TCP connection.
Do not use
Setting low socket timeouts may result in operations that error before the server responds.
Maximum wait time in milliseconds that a can thread wait for
a connection to become available. A value of