hoangvvo / nextjs-mongodb-app

A Next.js and MongoDB web application, designed with simplicity for learning and real-world applicability in mind.
https://nextjs-mongodb.now.sh/
MIT License
1.53k stars 286 forks source link

High number of connections on mongodb #89

Closed esteban89 closed 3 years ago

esteban89 commented 4 years ago

Is it normal to have 30-50 connections to mongodb for just one user on production? (It's obviously higher on dev environment because of React HMR) Number seems pretty high if I look forward to scale the number of users.

hoangvvo commented 4 years ago

Similar issue: https://github.com/hoangvvo/nextjs-mongodb-app/issues/87#issuecomment-663805716

The error happens because we tries to client.connect many times (especially in dev environment with hot reload): https://jira.mongodb.org/browse/NODE-1868

However, in our code, we have already checked against it with client.isConnected() so that client.connect is called only once.

https://github.com/hoangvvo/nextjs-mongodb-app/blob/master/middlewares/database.js#L17

...which, however, does not work with useUnifiedTopology: true @.@

https://jira.mongodb.org/browse/NODE-2234

Database never plays well with serverless in my experience...

hoangvvo commented 4 years ago

There is this alternative setup from vercel: https://vercel.com/guides/deploying-a-mongodb-powered-api-with-node-and-vercel

They make the assumption that client is always connected and only check if the database instance exists in cache and reuse it. I haven't tried that but it may work for you.

BhaveshSGupta commented 4 years ago

Should one open and close connection on each request and response? if we do that, then number of connections can be low or would there a issue here?

StarAzure commented 4 years ago

This is becoming a big issue in prod too. I am getting warning emails fro mongodb Atlas. Here is an example with just one user and one post, there are 238 connections. There is no hot reload in prod.

image

This will basically kill all our hard work which starts with this very useful repo. I hope someone can share a solution.

hoangvvo commented 4 years ago

@BhaveshSGupta We definitely can but there would be some perf issues since for the extra connect/disconnect. @StarAzure This is a common issue for db in serverless environment. You can switch to non-serverless platform like Heroku. Another solution is to use MongoDB Stitch, which provides serverless db access.

hoangvvo commented 3 years ago

This is partly mitigated with https://github.com/hoangvvo/nextjs-mongodb-app/commit/5b2523e7fd588abd7f441e11f22f0e168b1c5147

StarAzure commented 3 years ago

@hoangvvo You nailed the fix and connections are much lower now. - Thanks. See it live - https://starazure.com