mongodb-connection
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 en
Security Assessment
About mongodb-connection
A MongoDB connection-optimization skill covering client connection configuration—pools, timeouts, and instantiation patterns—across all officially supported driver languages (Node.js, Python, Java, Go, C#, Ruby, PHP, and others). It applies when working on code that creates or configures a MongoDB client, sets up connection pools, troubleshoots connection errors such as ECONNREFUSED, timeouts, or pool exhaustion, or optimizes connection-related performance. Typical scenarios include serverless functions, API endpoints, high-traffic applications, long-running tasks with concurrency, and debugging connection failures.
Its core principle is context before configuration: never add pool parameters or timeout settings without first understanding the application's environment, because arbitrary values cause performance issues and hard-to-debug problems. It explains how pooling works (connection setup of TCP, TLS, and auth costs roughly 50–500ms, and each open connection consumes about 1 MB of server RAM even when idle), the borrow/execute/return/prune lifecycle governed by `maxIdleTimeMS`, and the difference between synchronous drivers (where pool size often matches the thread pool) and asynchronous drivers (where smaller pools suffice). It accounts for the two automatic monitoring connections per replica set member via the formula Total = (minPoolSize + 2) × replica members × app instances.
Practical guidance includes calculating an initial pool size from throughput and latency (Pool Size ≈ ops/sec × avg duration + a 10–20% buffer), starting conservatively when durations vary, and considering topology: pools are created per server per client, sharded clusters typically connect through mongos routers, and secondary read preferences can add a pool per member. It recommends best practices such as creating the client once and reusing it (initializing outside the handler in serverless), not closing connections manually except at shutdown, and keeping max pool size above expected concurrency. Tuned parameter tables are provided for serverless (small pools, minPoolSize 0, short idle times), long-running OLTP servers (larger pools, pre-warmed connections, fail-fast timeouts), and OLAP/analytical workloads, with each recommended value justified by the gathered context.
FAQ
When should I use this skill?
Use it when instantiating or configuring a MongoDB client, setting up connection pools, troubleshooting connection errors like ECONNREFUSED, timeouts, or pool exhaustion, or optimizing connection performance for serverless, high-traffic, or long-running workloads.
What is its main rule about configuration?
Context before configuration: never add pool or timeout parameters without first understanding the application's environment. It asks targeted questions one at a time, starting broad, before recommending values.
How do I size a connection pool?
When performance data is available, use Pool Size ≈ (ops/sec) × (avg duration) plus a 10–20% buffer. When durations are variable, start conservatively at 10–20 connections, monitor, and adjust.
How does it recommend configuring serverless functions?
Initialize the client outside the handler to reuse connections across warm invocations, and use a small maxPoolSize (3–5), minPoolSize 0, and a short maxIdleTimeMS (10–30s) with non-zero connect and socket timeouts.
Does it account for connections beyond the pool?
Yes. Each MongoClient adds two monitoring connections per replica set member, so total potential connections are roughly instances × (maxPoolSize + 2) × replica members. It advises monitoring `connections.current` to avoid hitting server limits.
Install mongodb-connection
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
mongodb/agent-skills