Hi,
i just tried out AWS Lambda and MongoDB Atlast together, but so far i'm a bit disappointed by the bad performance.
Since i can't believe the results i'm getting are normal i would like to find out what i do wrong here so i can continue to use MongoDB.
- I have a MongoDB Atlas cluster (Free Tier) in the eu-west-1 region
- I also have a AWS Lambda Function in the same region
- Atlas is configured to allow access from everywhere at the moment, there is no VPC set up
- The code of the lambda is just simple nodeJS with query to an empty collection (see code below)
Execution Time for this lambda is typically 1000s+, while i had the same query with DynamoDB running at around or below 200ms.
I'm testing from the AWS Lambda web console, so no request overhead.
I suppose a lot of the execution time is for making a new connection to MongoDB with every execution.
Moving the connection in the initialization could improve this, to have a connection cached between requests.
But even then 1+ seconds execution time for a cold request is still pretty high compared to what DynamoDB got.
Is is the missing VPC? Is it slow throughput on the free Atlas tier? Or am i doing something wrong here?
Thanks a lot :)
'use strict';
const MongoClient = require('mongodb').MongoClient;
module.exports.handle = (event, context, callback) => {
console.log('event : ', event);
MongoClient.connect(process.env.MONGODB_URL, function(err, MongoDB) {
console.log('Connected correctly to server');
MongoDB.collection('items').find({}).toArray(function(error, items) {
if (error) {
console.error(error);
callback(null, {
statusCode: 500,
body: 'error'
});
} else {
console.log('data returned', items);
callback(null, {
statusCode: 200,
body: 'success'
});
MongoDB.close();
}
});
});
};