New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Atlas MongoDb+Lambda connection count spiking after update 5.7.1->5.12.10 #10280
Comments
Update: Using this connection code seems to have resolved the issue:
I found this code inside of this PR: https://github.com/vercel/next.js/pull/23321/files I'd be curious why this approach works better than the former 🤔 |
I can confirm the report. We didn't notice it straight away but it seems that our I've tried several previous releases and it seems 5.9.29 was the last version to respect the setting. Thanks @scottweinert for sharing your fix. |
@guillaumekh 'use strict';
const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
const { Schema } = mongoose;
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true,
poolSize: 2
});
await mongoose.connection.dropDatabase();
const Model = mongoose.model('Test', Schema({ name: String }));
await Model.create({ name: 'test' });
const start = Date.now();
await Promise.all([
Model.find({ $where: 'sleep(5000) || true' }),
Model.find({ $where: 'sleep(5000) || true' })
]);
console.log('Done', Date.now() - start);
} @scottweinert it's hard to tell without taking a closer look at your code. My best guess is because your original code has a race condition: It looks like your new code also has a similar race condition, it's possible for const mongoose = require('mongoose')
let cached = global.mongoose
if (!cached) {
cached = global.mongoose = { conn: null, promise: null }
}
exports.init = async (mongoDbUri) => {
if (cached.conn) {
return cached.conn
}
if (!cached.promise) {
const opts = {
useNewUrlParser: true,
useUnifiedTopology: true,
bufferCommands: false,
bufferMaxEntries: 0,
useFindAndModify: false,
useCreateIndex: true
}
cached.promise = mongoose.connect(mongoDbUri, opts).then((mongoose) => {
return mongoose
})
cached.conn = await cached.promise
return cached.conn
}
return cached.promise // <-- add this case
} |
@vkarpov15 OK i've done some digging around. Like @scottweinert we had a conditional check on With that said, I have managed to narrow down our With Node 16.4, Mongoose 5.12.14, and a 3-node MongoDB 4.4.6 replica set running on Atlas:
Upstream (mongodb@3.6.8) shows similar behaviour so this might not be a mongoose thing.
Shouldn't there be a single TCP connection here, or am I misunderstanding how |
Submitted to upstream : https://developer.mongodb.com/community/forums/t/poolsize-connection-count/113029 I think this issue can be closed. |
For reference, and in case anyone else stumbles here wondering just how many TCP connections a single
Source: MongoDB @emadum @ https://developer.mongodb.com/community/forums/t/poolsize-connection-count/113029/2 |
bug
What is the current behavior?
I'm experiencing an odd behavior after upgrading our mongoose package from
5.7.1
to5.12.10
that I cannot explain. We're using AWS Lambda and we are reusing the connections with the script below, and settingcontext.callbackWaitsForEmptyEventLoop = false;
in our Lambda handler. We've been running it for months, and after releasing the package update on production, our connections quickly went through the roof.We had to quickly revert as to not bring our system down. Reading through the release notes, I cannot find anything that would explain this behavior, but I am able to consistently reproduce it by releasing and putting load on our dev environment in AWS Lambda.
If the current behavior is a bug, please provide the steps to reproduce.
Here is our code for connecting to the DB:
What is the expected behavior?
For the connections not to spike after updating mongoose.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 14.x runtime in AWS Lambda, MongoDB 4.4, Mongoose 5.12.10
The text was updated successfully, but these errors were encountered: