Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js

Connection Pool Monitoring

On this page

  • Overview
  • Event Subscription Example
  • Event Descriptions
  • Example Event Documents
  • connectionPoolCreated
  • connectionPoolReady
  • connectionPoolClosed
  • connectionCreated
  • connectionReady
  • connectionClosed
  • connectionCheckOutStarted
  • connectionCheckOutFailed
  • connectionCheckedOut
  • connectionCheckedIn
  • connectionPoolCleared

This guide shows you how to monitor the driver's connection pool. A connection pool is a set of open TCP connections your driver maintains with a MongoDB instance. Connection pools help reduce the number of network handshakes your application needs to perform and can help your application run faster.

Read this guide if you need to record connection pool events in your application or want to explore the information provided in these events.

You can access one or more connection pool events using the driver by subscribing to them in your application. The following example demonstrates connecting to a replica set and subscribing to one of the connection pool monitoring events created by the MongoDB deployment:

const { MongoClient } = require("mongodb");
// Replace the following with your MongoDB deployment's connection
// string.
const uri =
"mongodb+srv://<clusterUrl>/?replicaSet=rs&writeConcern=majority";
const client = new MongoClient(uri);
// Replace <event name> with the name of the event you are subscribing to.
const eventName = "<event name>";
client.on(eventName, (event) =>
console.log("\nreceived event:\n", event)
);
async function run() {
try {
// Establish and verify connection
await client.db("admin").command({ ping: 1 });
console.log("\nConnected successfully!\n");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);

You can subscribe to any of the following connection pool monitoring events:

Event Name
Description
connectionPoolCreated
Created when a connection pool is created.
connectionPoolReady
Created when a connection pool is ready.
connectionPoolClosed
Created when a connection pool is closed, prior to server instance destruction.
connectionCreated
Created when a connection is created, but not necessarily when it is used for an operation.
connectionReady
Created after a connection has successfully completed a handshake and is ready to be used for operations.
connectionClosed
Created when a connection is closed.
connectionCheckOutStarted
Created when an operation attempts to acquire a connection for execution.
connectionCheckOutFailed
Created when an operation fails to acquire a connection for execution.
connectionCheckedOut
Created when an operation successfully acquires a connection for execution.
connectionCheckedIn
Created when a connection is checked back into the pool after an operation is executed.
connectionPoolCleared
Created when a connection pool is cleared.

The following sections show sample output for each type of connection pool monitoring event.

ConnectionPoolCreatedEvent {
time: 2023-02-13T15:54:06.944Z,
address: '...',
options: {...}
}
ConnectionPoolReadyEvent {
time: 2023-02-13T15:56:38.440Z,
address: '...'
}
ConnectionPoolClosedEvent {
time: 2023-02-13T15:56:38.440Z,
address: '...'
}
ConnectionCreatedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
connectionId: 1
}
ConnectionReadyEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
connectionId: 1
}
ConnectionClosedEvent {
time: 2023-02-13T15:56:38.439Z,
address: '...',
connectionId: 1,
reason: 'poolClosed',
serviceId: undefined
}
ConnectionCheckOutStartedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
}
ConnectionCheckOutFailedEvent {
time: 2023-02-13T15:56:38.291Z,
address: '...',
reason: ...
}
ConnectionCheckedOutEvent {
time: 2023-02-13T15:54:07.188Z,
address: '...',
connectionId: 1
}
ConnectionCheckedInEvent {
time: 2023-02-13T15:54:07.189Z,
address: '...',
connectionId: 1
}
ConnectionPoolClearedEvent {
time: 2023-02-13T15:56:38.439Z,
address: '...',
serviceId: undefined,
interruptInUseConnections: true,
}
←  Command MonitoringGridFS →