Skip to content
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

question about mongodb connection handling #11

Open
m-sallam opened this issue Jun 14, 2022 · 8 comments
Open

question about mongodb connection handling #11

m-sallam opened this issue Jun 14, 2022 · 8 comments

Comments

@m-sallam
Copy link

m-sallam commented Jun 14, 2022

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

@agrittiwari
Copy link
Contributor

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

@Barruchou
Copy link

Barruchou commented Jun 21, 2022

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.

@musiur
Copy link

musiur commented Feb 21, 2023

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.

@frouo
Copy link

frouo commented Jul 6, 2023

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

@frouo
Copy link

frouo commented Jul 17, 2023

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

@jayeshcreole
Copy link

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?

@frouo
Copy link

frouo commented Mar 12, 2024

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.

@jayeshcreole
Copy link

@

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants