- Newest
- Most votes
- Most comments
The issue you're experiencing is likely due to the nature of how Provisioned Concurrency works in AWS Lambda, combined with the behavior of your MongoDB connection. While Provisioned Concurrency does help reduce cold starts by keeping a specified number of execution environments initialized, it doesn't guarantee that your MongoDB connection will remain active indefinitely.
Here are a few points to consider and potential solutions:
-
Provisioned Concurrency Behavior: Provisioned concurrency initializes execution environments, but it doesn't continuously run your function. The environments are ready to respond quickly, but they may still shut down after periods of inactivity.
-
MongoDB Connection: The MongoDB connection is likely being closed due to inactivity or timeout settings, not because of Lambda's runtime shutdown. Database connections typically have idle timeout settings to manage resources efficiently.
-
Connection Management: Instead of trying to keep a single connection alive indefinitely, it's generally better to manage connections more dynamically. Consider implementing connection pooling or creating a new connection for each invocation.
-
Optimization Strategies:
- Use a database proxy like Amazon RDS Proxy (if applicable) to manage connections more efficiently.
- Implement connection pooling in your Lambda function to reuse connections when possible.
- Optimize your MongoDB connection settings to reduce connection time.
- Consider using AWS DocumentDB (with MongoDB compatibility) which might offer better integration with AWS services.
-
Warm-up Function: Implement a separate "warm-up" Lambda function that periodically invokes your main function to keep it active. This can be scheduled using Amazon EventBridge (CloudWatch Events).
-
Asynchronous Initialization: If possible, restructure your code to initialize the MongoDB connection asynchronously while beginning to process the request. This can help reduce perceived latency.
-
Caching: For frequently accessed data, consider implementing caching using services like Amazon ElastiCache to reduce database calls.
Remember, while Provisioned Concurrency helps with reducing cold starts related to Lambda initialization, it doesn't solve all latency issues, especially those related to external connections. The goal should be to optimize your function's code and database interaction rather than trying to keep a single connection alive indefinitely.
If after implementing these strategies you still experience significant delays, you may want to review your MongoDB configuration and network settings to ensure there are no other factors contributing to the connection delays.
Sources
Configuring provisioned concurrency for a function - AWS Lambda
Understanding Lambda function scaling - AWS Lambda
AWS Lambda - Serverless Applications Lens
Hi,
You want to maintain the MongoDB connection up & running to fully leverage the Lambda Provisioned Concurrency. To achieve it, you have to set the keepalive parameter of MonDB driver to the right value for your use case.
Best,
Didier
I am using Mongoose for more controlled schema and the keepAlive is on by default, the connection severed anyway. Is there any other way to keep the connection alive or connect the Mongo cluster with low latency (below1 or 1 second)
In addition to the other answers, note the function's execution log will give you info on where the delay is - whether there's a delay before execution starts or a delay during execution waiting for the DB.
The delay is during the function's execution. this is my sample handler function:
import connDb from "/opt/nodejs/connection.mjs" ...other imports
export const handler = async (event) => { const headers = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "PUT", "Access-Control-Allow-Headers": "Content-Type, Authorization" }; try { await connDb(); // the execution of this line causes the delay. ...other logics } catch (error) { } };
Relevant content
asked 2 years ago
- AWS OFFICIALUpdated a month ago

Due to extra costings, we can't invoke the Lambda function periodically. The RDS proxy does not directly support MongoDB. The connection pooling is default on mongoose connection , it can reuse the existing connection but even though due to inactivity over a certain period causes the connection to be severed.