Immutable solution version
MongoDB Atlas: connect from AWS Lambda and Vercel without pool exhaustion
Complete solution
Serverless functions scale to hundreds of concurrent executions, each wanting connections. This skill covers client reuse and small pools.
Atlas from serverless functions
The failure mode: every invocation creates a client, each client opens a pool, and the cluster hits its connection cap under load.
// client created ONCE per function instance, outside the handler
const { MongoClient } = require('mongodb');
const client = new MongoClient(process.env.MONGODB_URI, {
maxPoolSize: 5, // small: one instance needs few concurrent ops
serverSelectionTimeoutMS: 5000,
});
const ready = client.connect();
exports.handler = async (event) => {
await ready;
const col = client.db('mydb').collection('events');
await col.insertOne({ ...event, at: new Date() });
return { ok: true };
};Rules
- Create the client outside the handler so warm invocations reuse it. Creating it inside the handler is the bug.
- Keep
maxPoolSizesmall (1 to 5). Total connections = instances x pool size; hundreds of instances x 100 pool = outage. - Do not
close()the client at the end of the handler. Let the frozen instance keep it for the next warm invocation. - Await the initial connect once (a module-level promise) so concurrent warm invocations do not race multiple connects.
- Set a short
serverSelectionTimeoutMSso a cold start with a bad network path fails fast instead of burning the whole invocation timeout.
Verify
Load-test with concurrent invocations and watch the Atlas Connections metric. It should plateau near instances x maxPoolSize, not climb without bound.
Evidence
Serverless connection guidance (client reuse across invocations, small pools) follows the MongoDB connection management best practices; Atlas-side pool and timeout options per https://www.mongodb.com/docs/manual/reference/connection-string/, verified 2026-09-26.
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_LyUnYqkVrz5GioYFu_IqoA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MongoDB Atlas: connect from AWS Lambda and Vercel without pool exhaustion <span>— Vectle</span></a>