<!-- llms-explorer concept facts · https://llms-explorer.com/tree/lambda-mongodb-driver-pattern/ · pack 2026-10-22 · ~1055 tokens -->

# Lambda MongoDB Driver Pattern

> Source-anchored research for Lambda MongoDB Driver Pattern. Run status: COMPLETED.

Parent: [Atlas Data API Removal](https://llms-explorer.com/tree/atlas-data-api-removal/) · 4 facets · 9 facts · page: https://llms-explorer.com/tree/lambda-mongodb-driver-pattern/

## How it works

- The reuse boundary is one Lambda execution environment. Because each MongoClient owns its own per-server pool, Lambda scale-out can create separate pools in separate environments; module-scope caching does not set a fleet-wide Atlas connection cap. This is an inference from AWS's environment reuse guidance and MongoDB's per-client pool model. (high confidence) — [source](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html)
- AWS Lambda Managed Instances can run multiple Node.js requests concurrently in one execution environment, sharing global objects. When using this mode, isolate mutable request state and use a database client pool that supports concurrent operations; the MongoDB Node.js driver documents one MongoClient supporting multiple concurrent requests. (medium confidence) (verified as of 2026-10-22) — [source](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-nodejs-runtime.html)
- MongoDB's current Node.js driver guide says releases earlier than 6.18.0 did not close idle connections when minPoolSize was 0 during inactivity. Verify the driver version before relying on minPoolSize=0 to reclaim idle sockets. (low confidence) (verified as of 2026-10-14) — [source](https://www.mongodb.com/docs/drivers/node/current/connect/connection-options/connection-pools.md)

## How-to and procedures

- Use context.callbackWaitsForEmptyEventLoop=false only with callback-style Node.js handlers; MongoDB's Lambda guide says the setting does not apply to async handlers. AWS currently documents callback-based handlers only for Node.js 22 and earlier runtimes. (high confidence) (verified as of 2026-10-22) — [source](https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda.md)

## Problems, failure modes and limitations

- AWS Lambda can purge idle connections, so a later invocation cannot assume an idle socket remains usable. MongoDB's Node.js driver exposes maxIdleTimeMS and minPoolSize to control idle pool behavior; tune these with the workload and test invocations after long idle periods. (medium confidence) (verified as of 2026-10-14) — [source](https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html)
- Lambda can purge idle connections, so reuse does not guarantee an old socket remains usable. AWS warns that reusing a purged idle connection can error; Atlas recommends maxIdleTimeMS=60000 to reduce serverless timeout errors. (high confidence) (verified as of 2026-10-14) — [source](https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html)

## Facts and statements

- For standard AWS Lambda, create one Node.js MongoClient outside the handler and reuse it across warm invocations handled by the same execution environment. AWS documents execution-environment resource reuse, MongoDB's Atlas guide shows a module-scope client, and the Serverless Framework example implements the same pattern. Google's Atlas tutorial demonstrates the same instance-local pattern for Cloud Functions. (high confidence) — [source](https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda.md)
- Each MongoClient maintains pools per server and opens monitoring sockets in addition to application connections. Set pool limits against expected Lambda concurrency and MongoDB topology; maxPoolSize is not a total connection cap for the deployment. (medium confidence) (verified as of 2026-10-14) — [source](https://www.mongodb.com/docs/drivers/node/current/connect/connection-options/connection-pools.md)
- Each MongoClient maintains a pool for each server. The current Node.js driver docs list maxPoolSize=100 by default and up to two additional monitoring sockets per server per client, so estimate connection use across both topology and Lambda environments. (high confidence) (verified as of 2026-10-14) — [source](https://www.mongodb.com/docs/drivers/node/current/connect/connection-options/connection-pools.md)
