Connection Pools
Overview
In this guide, you can learn about how the .NET/C# Driver uses connection pools to manage connections to a MongoDB deployment and how you can configure connection pool settings in your application.
A connection pool is a cache of open database connections maintained by the .NET/C# Driver. When your application requests a connection to MongoDB, the .NET/C# Driver seamlessly gets a connection from the pool, performs operations, and returns the connection to the pool for reuse.
Connection pools help reduce application latency and the number of times new connections
are created by the .NET/C# Driver. The following diagram illustrates a high-level view
of how the MongoClient
manages a connection pool:
Configuring Connection Pools
You can specify the following connection pool settings in your MongoClient
object or in
your connection URI:
Setting | Description |
---|---|
| The maximum amount of time that the .NET/C# Driver waits when establishing a new
connection before timing out. Data Type: TimeSpan Default: 30 seconds Connection URI Example: connectTimeoutMS=0 |
| The maximum number of connections that each pool can establish concurrently.
If this limit is reached, further requests wait until a connection is established
or another in-use connection is checked back into the pool. Data Type: integer Default: 2 Connection URI Example: maxConnecting=3 |
| The maximum time that a connection can remain idle in the pool. When a connection
exceeds this limit, the .NET/C# Driver closes the connection and removes it from
the pool. Data Type: TimeSpan Default: 10 minutes Connection URI Example: maxIdleTimeMS=60000 |
| The maximum time that a connection can be pooled. When a connection exceeds this
limit, the .NET/C# Driver closes the connection and removes it from the pool. Data Type: TimeSpan Default: 30 minutes Connection URI Example: maxLifeTimeMS=50000 |
| The maximum number of concurrent connections that the pool maintains.
If the maximum pool size is reached, further requests wait until a connection
becomes available. Data Type: integer Default: 100 Connection URI Example: maxPoolSize=150 |
| The minimum number of concurrent connections that the pool maintains. If
the number of open connections falls below this value due to network errors,
the .NET/C# Driver attempts to create new connections to maintain this minimum. Data Type: integer Default: 0 Connection URI Example: minPoolSize=3 |
| The length of time that the .NET/C# Driver waits for a response from the server
before timing out. Data Type: TimeSpan Default: OS default Connection URI Example: socketTimeoutMS=100000 |
| How long a thread waits for a connection to become available in the connection pool
before timing out. Data Type: TimeSpan Default: 2 minutes Connection URI Example: waitQueueTimeoutMS=100000 |
The following code creates a client with a maximum connection pool size of 50
by using the
MaxConnectionPoolSize
parameter:
var settings = MongoClientSettings.FromConnectionString("<connection URI>"); settings.MaxConnectionPoolSize = 50; var client = new MongoClient(settings);
The following code creates a client with the same configuration as the preceding example, but uses a connection URI:
var settings = MongoClientSettings.FromConnectionString("<hostname>?maxPoolSize=50"); var client = new MongoClient(settings);
Additional Information
To learn more about connection pools, see Connection Pool Overview in the MongoDB Server manual.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: