Docs Menu
Docs Home
/ / /
Node.js Driver
/ /

Connection Pool Monitoring

On this page

  • Overview
  • Event Subscription Examples
  • 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.

The following sections demonstrate how to record connection pool events in your application and 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 =
const client = new MongoClient(uri);
// Replace <event name> with the name of the event you are subscribing to
const eventName = "<event name>";
// Subscribe to the event
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();

Connection pool monitoring events can aid you in debugging and understanding the behavior of your application's connection pool. The following example uses connection pool monitoring events to return a count of checked-out connections in the pool:

function connectionPoolStatus(client) {
let checkedOut = 0;
function onCheckout() {
function onCheckin() {
function onClose() {
client.removeListener('connectionCheckedOut', onCheckout);
client.removeListener('connectionCheckedIn', onCheckin);
checkedOut = NaN;
// Decreases count of connections checked out of the pool when connectionCheckedIn event is triggered
client.on('connectionCheckedIn', onCheckin);
// Increases count of connections checked out of the pool when connectionCheckedOut event is triggered
client.on('connectionCheckedOut', onCheckout);
// Cleans up event listeners when client is closed
client.on('close', onClose);
return {
count: () => checkedOut,
cleanUp: onClose

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

Event Name
Created when a connection pool is created.
Created when a connection pool is ready.
Created when a connection pool is closed before server instance destruction.
Created when a connection is created, but not necessarily when it is used for an operation.
Created after a connection has successfully completed a handshake and is ready to be used for operations.
Created when a connection is closed.
Created when an operation attempts to acquire a connection for execution.
Created when an operation fails to acquire a connection for execution.
Created when an operation successfully acquires a connection for execution.
Created when a connection is checked back into the pool after an operation is executed.
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 Monitoring
GridFS →