Generated banner for Mongodb Connection
Generated logo for Mongodb Connection

Mongodb Connection

MongoDBOfficialPublished by MongoDB

  • Skill
  • Tool integration
  • Data and AI
  • Apache-2.0
  • No issues found

From the author

Optimize MongoDB client connection configuration (pools, timeouts, patterns) for any supported driver language. Use this skill when working/updating/reviewing on functions that instantiate or configure a MongoDB client (eg, when calling `connect()`), configuring connection pools, troubleshooting connection errors (ECONNREFUSED, timeouts, pool exhaustion), optimizing performance issues related to connections. This includes scenarios like building serverless functions with MongoDB, creating API endpoints that use MongoDB, optimizing high-traffic MongoDB applications, creating long-running tasks and concurrency, or debugging connection-related failures.
— the description by MongoDB

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

The MongoDB Connection Optimizer provides expert-level guidance on configuring and troubleshooting MongoDB client connections. It emphasizes context-aware configuration, ensuring that pool sizes, timeouts, and lifecycle settings are tailored to specific deployment environments—such as serverless, high-traffic, or analytical workloads—rather than applying generic defaults. The skill includes a structured approach to gathering necessary environmental context, calculating optimal pool sizes, and interpreting driver-level monitoring events to resolve performance bottlenecks like connection churn or pool exhaustion.

What it does

  • Provides tailored configuration recommendations for MongoDB connection pools and timeouts.
  • Guides users through context-gathering to ensure settings match specific workload requirements.
  • Explains the mechanics of connection pooling, lifecycle management, and topology impacts.
  • Offers troubleshooting strategies for common issues like ECONNREFUSED, timeouts, and pool exhaustion.
  • Advises on monitoring connection health using standard driver events and metrics.
  • Defines best practices for client reuse, particularly in serverless and high-concurrency environments.

When to use it

  • When configuring a new MongoDB client connection in any supported driver language.
  • When troubleshooting performance issues like high latency, connection timeouts, or pool exhaustion.
  • When optimizing applications for high-traffic, bursty, or analytical workloads.
  • When deploying MongoDB-backed applications to serverless environments to ensure proper client caching.
  • When auditing existing connection configurations for potential memory or performance improvements.

Advantages

  • Prevents common performance pitfalls by enforcing context-aware configuration.
  • Reduces connection churn and server-side resource overhead.
  • Provides clear, actionable steps for troubleshooting complex connection failures.
  • Helps balance application performance with server-side resource constraints.
  • Standardizes the approach to monitoring and tuning connection pools.

Usage examples

“I am getting MongoWaitQueueTimeoutError in my Node.js app. How do I fix this?”
The agent will first ask for your current deployment type (e.g., serverless vs. traditional), peak concurrency, and current maxPoolSize to determine if the pool is truly exhausted or if query optimization is needed.
“What are the recommended MongoDB connection settings for an AWS Lambda function?”
The agent will recommend a small maxPoolSize (3-5), minPoolSize of 0, and advise on initializing the MongoClient outside the handler function to ensure reuse across warm invocations.

Install

  1. Download the skill as a ZIP:

    https://autogineer.com/agent-skills/mongodb/mongodb-connection/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip mongodb-connection.zip -d .agents/skills

Documentation by MongoDB

MongoDB Connection Optimizer

You are an expert in MongoDB connection management across all officially supported driver languages (Node.js, Python, Java, Go, C#, Ruby, PHP, etc.). Your role is to ensure connection configurations are optimized for the user's specific environment and requirements, avoiding the common pitfall of blindly applying arbitrary parameters.

Core Principle: Context Before Configuration

NEVER add connection pool parameters or timeout settings without first understanding the application's context. Arbitrary values without justification lead to performance issues and harder-to-debug problems.

Understanding How Connection Pools Work

  • Connection pooling exists because establishing a MongoDB connection is expensive (TCP + TLS + auth = 50-500ms). Without pooling, every operation pays this cost.

  • Open connections consume system memory on the MongoDB server instances, ~1 MB per connection on average, even when they are not active. It is advised to avoid having idle connections.

Connection Lifecycle: Borrow from pool → Execute operation → Return to pool → Prune idle connections exceeding maxIdleTimeMS.

Synchronous vs. Asynchronous Drivers:

  • Synchronous (PyMongo, Java sync): Thread blocks; pool size often matches thread pool size

  • Asynchronous (Node.js, Motor): Non-blocking I/O; smaller pools suffice

Monitoring Connections: Each MongoClient establishes 2 monitoring connections per replica set member (automatic, separate from your pool). Formula: Total = (minPoolSize + 2) × replica members × app instances. Example: 10 instances, minPoolSize 5, 3-member set = 210 server connections. Always account for this when planning capacity.

Configuration Design

Before suggesting any configuration changes, ensure you have the sufficient context about the user's application environment to inform pool configuration (see Environmental Context below). If you don't have enough information, ask targeted questions to gather it. Ask only one question at a time, starting with broad context (deployment type, workload, concurrency) before drilling down into specifics.

When you suggest configuration, briefly explain WHY each parameter has its specific value based on the context you gathered. Use the user's environment details (deployment type, workload, concurrency) to justify your recommendations.

Example: maxPoolSize: 50 — "Based on your observed peak of 40 concurrent operations with 25% headroom for traffic bursts"

If you provide code snippets, add inline comments explaining the rationale for each parameter choice.

Calculating Initial Pool Size

If performance data available: Pool Size ≈ (Ops/sec) × (Avg duration) + 10-20% buffer

Example: (10,000 ops/sec) × (10ms) + 20% buffer = 120 connections

Use when: Clear requirements, known latency, predictable traffic. Don't use when: variable durations—start conservative (10-20), monitor, adjust.

Query optimization can dramatically reduce required pool size.

The total number of supported connections in a cluster could inform the upper limit of poolSize based on the number of MongoClient's instances employed. For example, if you have 10 instances of MongoClient using a size of 5 connecting to a 3 node replica set: 10 instances × 5 connections × 3 servers = 150 connections.

Each connection requires ~1 MB of physical RAM, so you may find that the optimal value for this parameter is also informed by the resource footprint of your application's workload.

The role of Topology:
  • Pools are created per server per MongoClient.

  • By default, clients connect to one mongos router per sharded cluster (which manages connections to the shards internally), not to individual shards; so the shard amount do not affect the pool size directly.

  • Shards share the workload and reduce stress on each individual server, increasing cluster capacity.

  • Replica members do not affect the max pool directly. If the driver communicates with multiple replica set members (for example for reads with secondary read preference), it may create a pool per member.

  • Replica set members do not increase write capacity (only the primary handles writes). However, they can increase read capacity if your application uses read preferences that allow secondary reads.

Server-Side Connection Limits:

Total potential connections = instances × (maxPoolSize + 2) × replica set members. The + 2 accounts for the two monitoring connections per replica set member, per MongoClient instance. Monitor connections.current to avoid hitting limits. See references/monitoring-guide.md for how to set up monitoring.

Self-managed Servers: Set net.maxIncomingConnections to a value slightly higher than the maximum number of connections that the client creates, or the maximum size of the connection pool. This setting prevents the mongos from causing connection spikes on the individual shards that disrupt the operation and memory allocation of the sharded cluster.

Configuration Scenarios

General best practices:

  • Create client once only and reuse across application (in serverless, initialize outside handler)

  • Don't manually close connections unless shutting down

  • Max pool size must exceed expected concurrency

  • Make use of timeouts to keep only the required connections ready as per your workload's needs

  • Use default max pool size (100) unless you have specific needs (see scenarios below)

Scenario: Serverless Environments (Lambda, Cloud Functions)

Critical pattern: Initialize client OUTSIDE handler/function scope to enable connection reuse across warm invocations.

Recommended configuration:

ParameterValueReasoning
maxPoolSize3-5Each serverless function instance has its own pool
minPoolSize0Prevent maintaining unused connections. Increase to mitigate cold starts if needed
maxIdleTimeMS10-30sRelease unused connections more quickly
connectTimeoutMS>0Set to a value greater than the longest network latency you have to a member of the set
socketTimeoutMS>0Use socketTimeoutMS to ensure that sockets are always closed
Scenario: Traditional Long-Running Servers (OLTP Workload)

Recommended configuration:

ParameterValueReasoning
maxPoolSize50+Based on peak concurrent requests (monitor and adjust)
minPoolSize10-20Pre-warmed connections ready for traffic spikes
maxIdleTimeMS5-10minStable servers benefit from persistent connections
connectTimeoutMS5-10sFail fast on connection issues
socketTimeoutMS30sPrevent hanging queries; appropriate for short OLTP operations
serverSelectionTimeoutMS5sQuick failover for replica set topology changes

MongoDB 8.0+ introduces defaultMaxTimeMS on Atlas clusters, which provides server-side protection against long-running operations.

Scenario: OLAP / Analytical Workloads

Recommended configuration:

ParameterValueReasoning
maxPoolSize10-20Fewer concurrent operations. Match your expected concurrent analytical operations
minPoolSize0-5Queries are infrequent; minimal pre-warming needed
socketTimeoutMS>0Set socketTimeoutMS to two or three times the length of the slowest operation that the driver runs.
maxIdleTimeMS10minMinimize connection churn while not keeping truly idle connections too long. Consider the timeouts of intermediate network devices
Scenario: High-Traffic / Bursty Workloads

Recommended configuration:

ParameterValueReasoning
maxPoolSize100+Higher ceiling to accommodate sudden traffic spikes
minPoolSize20-30More pre-warmed connections ready for immediate bursts
maxConnecting2 (default)Prevent thundering herd during sudden demand
waitQueueTimeoutMS2-5sFail fast when pool exhausted rather than queueing indefinitely
maxIdleTimeMS5minBalance between reuse during bursts and cleanup between spikes

Troubleshooting Connection Issues

If the user requires help to troubleshoot connection issues, determine whether this is a client config issue or infrastructure problem.

Types of issues:

  • Infrastructure or Network Issues (Out of Scope): redirect to publicly available infractructure documentation.

    • eg: DNS/SRV resolution failures, network/VPC blocking, IP not whitelisted, TLS cert issues, auth mechanism mismatches

  • Client Configuration Issues (Your Territory):

    • eg: Pool exhaustion, inappropriate timeouts, poor reuse patterns, suboptimal sizing, missing serverless caching, connection churn

Guidelines
  • Ask only one question at a time, starting with broad context (deployment type, workload, concurrency) before drilling down into specifics (current config, error messages). This approach allows you to quickly narrow down the root cause and avoid unnecessary configuration changes or excessive questions.

  • Review references/monitoring-guide.md for how to instrument and monitor the relevant parameters that can inform your troubleshooting and recommendations.

Pool Exhaustion

When operations queue, pool is exhausted.

Symptoms: MongoWaitQueueTimeoutError, WaitQueueTimeoutError or MongoTimeoutException, increased latency, operations waiting.

Solutions:

  • Increase maxPoolSize when: Wait queue has operations waiting (size > 0) + server shows low utilization

  • Don't increase when: Server is at capacity. Suggest query optimization.

Connection Timeouts (ECONNREFUSED, SocketTimeout)

Client Solutions: Increase connectTimeoutMS/socketTimeoutMS if legitimately needed

Infrastructure Issues (redirect):

  • Cannot connect via shell: Network/firewall;

  • Environment-specific: VPC/security;

  • DNS errors: DNS/SRV resolution

Connection Churn

Symptoms: Rapidly increasing connections.totalCreated server metric, high connection handling CPU

Causes: Not using pooling, not caching in serverless, maxIdleTimeMS too low, restart loops

High Latency
  • Ensure minPoolSize > 0 for traffic spikes

  • Network compression for high-latency (>50ms): compressors: ['snappy', 'zlib']

  • Nearest read preference for geo-distributed setups


Environmental Context (MANDATORY)

ALWAYS verify you have the sufficient context about the user's application environment to inform pool configuration BEFORE suggesting any configuration changes.

Parameters that inform a pool configuration
  • Server's memory limits: each connection takes 1MB against the server.

  • Number of clients and servers in a cluster: pools are per client and per server, taking memory from the cluster.

  • OLAP vs OLTP: timeout values must support the expected duration of operations.

    • Expected duration of operations: Short OLTP queries may require lower socketTimeoutMS to fail fast on hanging operations, while long-running OLAP queries may need higher values to avoid premature timeouts.

  • Server version: MongoDB 8.0+ also introduces defaultMaxTimeMS on Atlas clusters, which provides server-side protection against long-running operations.

  • Serverless vs Traditional: Serverless functions should initialize clients outside the handler to enable connection reuse across warm invocations, while traditional servers can maintain larger pools with pre-warmed connections.

  • Concurrency and traffic patterns: High concurrency and bursty traffic may require larger pools and more pre-warmed connections, while steady, low-concurrency workloads can often operate efficiently with smaller pools.

  • Operating System: Some OSes have limits on the number of open file descriptors, which can impact the maximum number of connections. It's important to consider these limits when configuring connection pools, especially for high-traffic applications.

  • Driver version: Different driver versions may have different default settings and performance characteristics. Always check the documentation for the specific driver version being used to ensure optimal configuration.

Guidelines:

  • Ask only questions relevant to the scenarios in Configuration Design Phase. Omit questions that won't lead to a clear use of the content in Configuration Design Phase.

  • If an answer not provided, make a reasonable assumption and disclose it.


Advising on Monitoring & Iteration

You must guide users to monitor the relevant parameters to their pool configuration. For detailed monitoring setup, see references/monitoring-guide.md.


When creating code

For every connection parameter you provide (in recommendations or code snippets), ensure you have enough context about the user's application environment to inform values. If not, ask targeted questions before suggesting specific values. If you get no answer, make a reasonable assumption, disclose it and comment the relevant parameters accordingly in the code.

Files

references/monitoring-guide.md

MongoDB Connection Monitoring Guide

This reference provides detailed guidance on monitoring connection pool health, interpreting metrics, and taking action based on what you observe. Consult this when users need to verify their configuration is working or troubleshoot connection-related issues.

Driver Events

All MongoDB drivers implement the Connection Monitoring and Pooling specification, which defines standard events for tracking pool lifecycle and connection state:

Pool lifecycle events:

  • ConnectionPoolCreated / ConnectionPoolClosed - Track when pools are initialized or shut down

Connection lifecycle events:

  • ConnectionCreated / ConnectionClosed - Monitor connection churn (rapid creation = pooling issues)

Check-out events:

  • ConnectionCheckOutStarted - Operation requests a connection

  • ConnectionCheckedOut / ConnectionCheckedIn - Track when connections are borrowed/returned

  • ConnectionCheckOutFailed - Critical alert signal - indicates pool exhaustion

Tip: Send ConnectionCheckOutFailed events and rapid ConnectionCreated events to your monitoring system immediately.

Access methods vary by driver. Consult your driver's documentation for how to subscribe to these standard events.


Driver-Level Metrics to Watch
Connections Created

What it is: The total number of connections the pool has established since initialization.

Events:

  • ConnectionCreatedEvent - fired when a new connection object is instantiated.

What to watch for: Rapid increases (+100 connections/hour in steady state) indicate connection churn due to network issues or misconfiguration.

Healthy pattern: Gradual increase during application startup as the pool warms up, then relatively stable. You should see increases mainly when:

  • Application restarts

  • Pool size is increased

  • Network disruptions force reconnections

Troubleshooting:

  • Rapid growth: Indicates connection churn. Check:

    • maxIdleTimeMS is not too aggressive

    • Network stability

    • Application not creating new clients repeatedly

    • Serverless functions caching clients properly


Connections In-Use

What it is: The number of connections currently borrowed from the pool and serving application requests.

Events:

  • ConnectionCheckedOutEvent - increment counter (connection borrowed)

  • ConnectionCheckedInEvent - decrement counter (connection returned)

What to watch for: Consistently high values approaching maxPoolSize signal potential pool exhaustion.

Healthy pattern: Fluctuates with application traffic while maintaining headroom. Should correlate with request volume.

Action thresholds:

  • Sustained >80% of maxPoolSize: Increase maxPoolSize by 20-30%

  • Consistently 100%: Pool is definitely exhausted; immediate action needed

  • High percentage with high wait queue times: Clear sign of undersized pool


Connections Available

What it is: The number of open but unused connections ready in the pool.

Events:

  • ConnectionCheckedInEvent - increases available count

  • ConnectionCheckedOutEvent - decreases available count

What to watch for: Consistently zero means the pool is undersized.

Healthy pattern: Some available connections (10-20% of maxPoolSize) ready to handle sudden traffic spikes without waiting for new connection establishment.

Action thresholds:

  • Always zero during traffic: Pool is too small; connections are never released

  • Very low during normal load: Consider increasing maxPoolSize or minPoolSize


Wait Queue Size

What it is: The number of operations currently waiting for an available connection because the pool is at capacity.

Event:

  • ConnectionCheckoutStartedEvent - track when threads enter wait queue.

What to watch for: Any value above zero indicates possible pool exhaustion. This is a critical metric.

Healthy pattern: Zero most of the time, or occasional spikes during peak loads.

Action thresholds:

  • Any sustained queue (>0 for >10 seconds): Immediate action required

  • Repeated queuing: Increase maxPoolSize or reduce operation duration

  • Queue correlates with specific operations: Those operations may be holding connections too long

Why this matters: If waitQueueTimeoutMS is reached, users see errors.


Wait Queue Time

What it is: The duration operations spend waiting for connections to become available.

Events – Calculate duration: (checked out time) - (checkout started time)

  • ConnectionCheckoutStartedEvent - record timestamp when entering queue

  • ConnectionCheckedOutEvent - record timestamp when successfully acquired

What to watch for: This wait time directly adds to application latency. Even moderate wait times (50-100ms) can degrade user experience.

Healthy pattern: Consistently near-zero milliseconds.

Action thresholds:

  • >50ms consistently: Pool is under pressure; investigate sizing

  • >100ms: Immediate action required; users experiencing degraded performance

  • Spikes to >waitQueueTimeoutMS: Users seeing timeout errors


Server-Level Metrics to Watch

Use db.serverStatus().connections via MongoDB shell or driver equivalent.

Available fields:

  • current - Total active client connections

  • available - Remaining capacity before hitting maxIncomingConnections

  • totalCreated - Cumulative connections created since server start

  • active - Connections currently executing operations

  • exhaustIsMaster / exhaustHello - Streaming topology monitoring connections

  • awaitingTopologyChanges - Connections waiting for topology updates

See manual: db.serverStatus() documentation

connections.current

What it is: The number of active client connections currently established to the MongoDB server.

What to watch for: Approaching maxIncomingConnections indicates server-side saturation.

Default maxIncomingConnections values per OS:

  • Windows: 1,000,000

  • Linux/Unix: (RLIMIT_NOFILE / 2) * 0.8 (MongoDB enforces this limit even if configured higher)

Healthy pattern: Stable value with headroom for growth. Should roughly match the sum of all client pool sizes across all application instances.

Action thresholds:

  • >90% of maxIncomingConnections: Server at risk of refusing new connections

  • Unexpected spikes: May indicate runaway connection creation from clients

  • Steady growth: May need to scale server tier (Atlas) or adjust configuration (self-hosted)

Calculation example: If you have 10 application instances each with maxPoolSize: 50, you could have up to 500 connections in a single-server deployment. In a 3-member replica set, potentially 1,500 total connections across all members.


connections.available

What it is: How many more connections the server can accept before hitting its configured limit.

What to watch for: Low values indicate risk of connection refusal for new clients or scaling operations.

Healthy pattern: Substantial headroom even during peak traffic. At least 20-30% of maxIncomingConnections should remain available.

Action thresholds:

  • <10% available: High risk; urgent capacity planning needed

  • <5% available: Critical; new client connections may be refused


connections.totalCreated

What it is: The cumulative total of all connections created since the MongoDB server started.

What to watch for: The rate of increase indicates connection churn. Compare snapshots over time to calculate rate.

Healthy pattern: Increases mainly during:

  • Application deployments/restarts

  • Scaling events (adding new app instances)

  • Legitimate traffic growth

Diagnosis:

  • Baseline calculation: After initial warmup, calculate connections created per hour

  • Rapid increase (much faster than app restart cadence): Indicates connection churn across one or more clients

  • Correlation with client metrics: Cross-reference with driver-level total connections to identify which clients are churning

Example: If you see totalCreated increasing by 1,000 connections/hour but you only restart apps once per day (not serverless), something is causing unnecessary connection cycling.

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Needs: Needs Node.js

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Published by
MongoDB
Publisher
MongoDB (Official)
Source
mongodb/agent-skills · skills/mongodb-connection · commit 155bb9c
Licence
Apache License 2.0 (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced
Also distributed in
mongodb/agent-skills, mongodb/agent-skills

AutoGineer is not affiliated with or endorsed by MongoDB or MongoDB.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download mongodb-connection.zip