$npx -y skills add mongodb/agent-skills --skill mongodb-connectionOptimize 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,
| 1 | # MongoDB Connection Optimizer |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | ## Core Principle: Context Before Configuration |
| 6 | |
| 7 | **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. |
| 8 | |
| 9 | ## Understanding How Connection Pools Work |
| 10 | |
| 11 | - Connection pooling exists because establishing a MongoDB connection is expensive (TCP + TLS + auth = 50-500ms). Without pooling, every operation pays this cost. |
| 12 | - 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. |
| 13 | |
| 14 | **Connection Lifecycle**: Borrow from pool → Execute operation → Return to pool → Prune idle connections exceeding `maxIdleTimeMS`. |
| 15 | |
| 16 | **Synchronous vs. Asynchronous Drivers**: |
| 17 | - **Synchronous** (PyMongo, Java sync): Thread blocks; pool size often matches thread pool size |
| 18 | - **Asynchronous** (Node.js, Motor): Non-blocking I/O; smaller pools suffice |
| 19 | |
| 20 | **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. |
| 21 | |
| 22 | ## Configuration Design |
| 23 | |
| 24 | **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. |
| 25 | |
| 26 | 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. |
| 27 | |
| 28 | Example: `maxPoolSize: 50` — "Based on your observed peak of 40 concurrent operations with 25% headroom for traffic bursts" |
| 29 | |
| 30 | If you provide code snippets, add inline comments explaining the rationale for each parameter choice. |
| 31 | |
| 32 | ### Calculating Initial Pool Size |
| 33 | |
| 34 | If performance data available: `Pool Size ≈ (Ops/sec) × (Avg duration) + 10-20% buffer` |
| 35 | |
| 36 | Example: `(10,000 ops/sec) × (10ms) + 20% buffer = 120 connections` |
| 37 | |
| 38 | Use when: Clear requirements, known latency, predictable traffic. |
| 39 | Don't use when: variable durations—start conservative (10-20), monitor, adjust. |
| 40 | |
| 41 | Query optimization can dramatically reduce required pool size. |
| 42 | |
| 43 | 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`. |
| 44 | |
| 45 | 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. |
| 46 | |
| 47 | #### The role of Topology: |
| 48 | - Pools are created per server per MongoClient. |
| 49 | - 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. |
| 50 | - Shards share the workload and reduce stress on each individual server, increasing cluster capacity. |
| 51 | - 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. |
| 52 | - Replica set members do not increase write capacity (only the primary handles writes). However, they can increase read cap |