mongodb-starter icon indicating copy to clipboard operation
mongodb-starter copied to clipboard

question about mongodb connection handling

Open m-sallam opened this issue 2 years ago • 8 comments

from looking at the history of the mongodb connection handling code, it started out with pretty much the code from the with-mongodb example however it eventually ended up with caching the connection in global in both dev and production (even though there is a comment in the example about not using the global for production, which i don't understand why tbh).

is there a reason for that / an issue that made you switch the way of handling the connection?

also could you share the peak number of opened database connections you had and if possible what was the traffic like then? as i am wondering how the number of connections scales with traffic

m-sallam avatar Jun 14 '22 19:06 m-sallam

For production, Each MongoClient instance manages its own connection pool to the MongoDB cluster or node specified when the MongoClient is created.

// Export a module-scoped MongoClient promise. By doing this in a // separate module, the client can be shared across functions.

And as it using /working on Hot module replacement, It is a good approach to cache connection in global , https://blog.bitsrc.io/webpacks-hot-module-replacement-feature-explained-43c13b169986 This is helpful. I want to know more

agrittiwari avatar Jun 20 '22 02:06 agrittiwari

Hi,

Thank you @m-sallam for opening the issue, I was asking myself the same question. However, it seems that the mongodb.ts file has been updated one day after your post and it followed the with-mongodb example. It is definitely the right way to do so. About the next topic mentioned in your post :

also could you share the peak number of opened database connections you had and if possible what was the traffic like then? as i am wondering how the number of connections scales with traffic

I encountered the same case, maybe an answer from maintainers would be helpful.

Thank you.

Barruchou avatar Jun 21 '22 21:06 Barruchou

Hi, I am facing a connection error from Altas with the message connection to 35.171.70.188:27017 closed. How to solve this issue. I have been trying to run the project for more than 2/3 hours. But stuck here.

musiur avatar Feb 21 '23 14:02 musiur

I am joining this conversation. It first starts here https://github.com/vercel/mongodb-starter/issues/24 but I realize that it's becoming a duplicate / related issue.

I am facing the same issue : my mongodb connections exploses as soon as I am using mongodb from Vercel serverless functions (pages/api/ routes).

I wanted to start using mongodb in NextJS API routes, so I just started with this code, and see the result right after I pushed the code to production! Any idea on how to solve this? Does this Vercel <> MongoDB integration solve the issue somehow? 🙏

image image

frouo avatar Jul 06 '23 09:07 frouo

Here is a little update, I still don't get it, even with maxIdleTimeMs=2000 & maxPoolSize=10 options and with this mongodb.ts copy/pasted implementation.

I am still receiving like 10 or 15 emails a day "[Alert] You're nearing the maximum connections threshold". And I am just using mongodb in only 1 api route!!

Should I go to the extreme with maxIdleTimeMs = 1000 and maxPoolSize = 1?

Untitled

frouo avatar Jul 17 '23 16:07 frouo

I am joining this conversation. It first starts here #24 but I realize that it's becoming a duplicate / related issue.

I am facing the same issue : my mongodb connections exploses as soon as I am using mongodb from Vercel serverless functions (pages/api/ routes).

I wanted to start using mongodb in NextJS API routes, so I just started with this code, and see the result right after I pushed the code to production! Any idea on how to solve this? Does this Vercel <> MongoDB integration solve the issue somehow? 🙏

image image

Hey did you solved this problem?

jayeshcreole avatar Mar 12 '24 08:03 jayeshcreole

Hey did you solved this problem?

I keep requesting my database from my expressjs server (which holds the mongodb connection).

To me, nextjs serverless functions is not compatible with a mongodb dedicated server. Even though tutorial like this one let you think that it does... 😢

Indeed, everytime a nextjs API route is called, it instantly starts somewhere in the cloud an instance, run your code, then the instance is destroyed. In milliseconds. So there is no global state that holds that clientPromise. As a result, a new connection to mongodb is created every single time an API route is invoked, that's it !

Please feel free to correct me if I'm wrong.

frouo avatar Mar 12 '24 09:03 frouo

@

Hey did you solved this problem?

I keep requesting my database from my expressjs server (which holds the mongodb connection).

To me, nextjs serverless functions is not compatible with a mongodb dedicated server. Even though tutorial like this one let you think that it does... 😢

Indeed, everytime a nextjs API route is called, it instantly starts somewhere in the cloud an instance, run your code, then the instance is destroyed. In milliseconds. So there is no global state that holds that clientPromise. As a result, a new connection to mongodb is created every single time an API route is invoked, that's it !

Please feel free to correct me if I'm wrong.

I think this won't solve the problem as on every request it will create new connection

jayeshcreole avatar Mar 12 '24 10:03 jayeshcreole