mongodb-starter
mongodb-starter copied to clipboard
question about mongodb connection handling
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
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
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.
Hi,
I am facing a connection error from Altas with the message connection
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? 🙏
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
?
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? 🙏
![]()
Hey did you solved this problem?
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.
@
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