Closed esteban89 closed 3 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-1868However, in our code, we have already checked against it with
client.isConnected()
so thatclient.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
@.@
Database never plays well with serverless in my experience...
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.
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?
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.
This will basically kill all our hard work which starts with this very useful repo. I hope someone can share a solution.
@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.
This is partly mitigated with https://github.com/hoangvvo/nextjs-mongodb-app/commit/5b2523e7fd588abd7f441e11f22f0e168b1c5147
@hoangvvo You nailed the fix and connections are much lower now. - Thanks. See it live - https://starazure.com
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.