-
Discussed in #10621Originally posted by Nikhil22 September 28, 2023 QuestionWhen I connect to MongoDB (via MONGODB_URI env variable, via the node driver) from my Vercel-deployed app, I get intermittent errors (MongoServerSelectionError) that present as 500 errors in my Vercel-deployed app. The errors go away on refresh. An example of the error is:
Just to be clear, the connection string and credentials work and are correct as it does connect properly when errors are not there, but when errors that I describe above do occur, it crashes the entire app. I've written about this in detail in the MongoDB community here (https://www.mongodb.com/community/forums/t/no-way-to-avoid-replicasetnoprimary-errors/243498). I've also been in contact with MongoDB support and they said that there are no problems with the Mongo cluster itself. Moreover, upon further research, it seems that this is a common problem when trying to connect to MongoDB from Vercel. For example, here (https://www.mongodb.com/community/forums/t/intermittently-getting-mongoserverselectionerror-error-on-my-next-js-vercel-deployment/242470). I'd like to solve this problem, or at the very least, not have my entire app crash with a 500 due to the error, as it currently does. Please help. |
Beta Was this translation helpful? Give feedback.
Replies: 26 comments 18 replies
-
I am also encountering a nearly identical issue. Strangely enough the timeouts seem to occur almost instantaneously, suggesting there is some sort of stale connection or the issues are being misrepresented as timeouts. |
Beta Was this translation helpful? Give feedback.
-
Encountering the same issue! |
Beta Was this translation helpful? Give feedback.
-
Same issue here. A lot of other articles on the web talk about connection count issues, but my connection count is never above 2 while this is happening. I think its actually the opposite, some type of stale connection. |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing the same issue, with no clear pattern as when the issue occurs... |
Beta Was this translation helpful? Give feedback.
-
Yea, it's really tough. I've done a ton of debugging and I really think it's outside the developers control. When the issue happens NO logging in my code ever runs, even things that are output before calls to the DB code. My best guess: A previous call leaves a connection open, during the management of that connection something goes wrong and throws an unhandled exception, when a future request is handled by the same thread it already has an unhandled exception waiting for it. I have no real evidence of this but its the only thing that makes sense. Also, monitoring the events I see the serverClosedEvent happens almost at the same time as the error. |
Beta Was this translation helpful? Give feedback.
-
+1 to this. same issue. |
Beta Was this translation helpful? Give feedback.
-
Same issue |
Beta Was this translation helpful? Give feedback.
-
Same issue here. Is there any news on this, or hope? |
Beta Was this translation helpful? Give feedback.
-
In my case, it's failing when next-auth tries to refetch the session. The request to mongo fails and the session is lost. |
Beta Was this translation helpful? Give feedback.
-
Happens to me as well. Any fix for this? |
Beta Was this translation helpful? Give feedback.
-
Vercel FIX THIS! |
Beta Was this translation helpful? Give feedback.
-
Same problem. Please fix this |
Beta Was this translation helpful? Give feedback.
-
I've got the same issue, here is a full log: It appears at random and throws 500 errors, it seems that it doesn't follow a definitive pattern. I'm also using Prisma as ORM, but it doesn't seems to be linked to that. I found some clues about people having the same issue on AWS Lambda. |
Beta Was this translation helpful? Give feedback.
-
Same problem here. Please, the community need a workaround at least. |
Beta Was this translation helpful? Give feedback.
-
yup same issue |
Beta Was this translation helpful? Give feedback.
-
PLEASE FIX |
Beta Was this translation helpful? Give feedback.
-
Same thing happening here, and my app is in production. Not a good UX. I hope they release a fix ASAP. |
Beta Was this translation helpful? Give feedback.
-
Same issue here |
Beta Was this translation helpful? Give feedback.
-
As a general best practice, please have a look at their official guide here: Note: for the
are still very relevant to this type of error. So be sure to test them out as well. This is not necessarily an issue with the Vercel platform itself, but more of a common issue when you're dealing with raw TCP connections on serverless platforms in general. I wish Mongo team has an HTTP-based client library that utilizes their REST APIs: |
Beta Was this translation helpful? Give feedback.
-
The only guide I found for the integration with MongoDB was here. It does a good job over all, but it's a good practice to always return a response for an error, which they don't do. This is also a good reference to read: https://www.mongodb.com/docs/drivers/node/current/faq/ I managed to almost eliminate the timeout errors by making a few adjustments. For one, add these options to increase the timeout value:
The official docs for The one change that has probably helped the most was retrying the connection if there's an error. I no longer see numerous connection timeouts in my server logs the way I did before. Here's my full connection code:
I got the majority of this code from another article. I still get erros from time to time, but not nearly as often, and my server logs don't show connection timeout errors nearly as frequently. I hope this helps others. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your work @fsultani I digged a bit deeper. So for me this is connected to next-auth. Client keeps calling For me the options were new and the reconnecting logic. I tried to keep a tab open again and 40 minutes long no error happened, which made me hopeful. Unfortunately today I could see that the error happened to more users, not sure if less often than before. Unfortunately I also couldn’t see any notice of a successful connection after 2 tries. So I have no evidence the reconnect even works. I would suggest trying more than 1 reconnect. The weird thing for me is that I’m not even using the MongoDBAdapter as I could try using the adapter, but even the next-auth mongodb adapter file says:
So I assume so far a configuration in that file, or really Vercel has to fix this. |
Beta Was this translation helpful? Give feedback.
-
I am using mongoose to connect to MongoDB from my Vercel NextJS app. I was getting the same After a lot of searching, implementing retries, scratching my head, I found and implemented the lambda pattern at the bottom of this page: https://mongoosejs.com/docs/lambda.html I haven't seen the error since then. I think it's resolved? 🤞 |
Beta Was this translation helpful? Give feedback.
-
I’m trying this config for mongodb instead of mongoose, but it didn’t work
I’m not using models, but I’m gonna try your mongoose version now |
Beta Was this translation helpful? Give feedback.
-
Anyone found a solution to this using Prisma + MongoDB? |
Beta Was this translation helpful? Give feedback.
-
Hi everyone. It references this Github issue/discussion. If you could share any additional context or troubleshooting attempts there it would be very helpful. I understand this issue has been affecting many teams and we really appreciate your patience. Thanks, |
Beta Was this translation helpful? Give feedback.
-
Can someone show the exact sample code?
…On Tue, May 28, 2024 at 7:38 AM Maurizio Crocci ***@***.***> wrote:
@maulikdhameliya <https://github.com/maulikdhameliya>, I tried @ceduch
<https://github.com/ceduch> suggestion: not using .connect() and instead
wraping the client in a promise.
It has been running in production for 5 days and have not seen the same
error again. We usually saw it every 2 hours depending on usage patterns.
Thank you @ceduch <https://github.com/ceduch> !
—
Reply to this email directly, view it on GitHub
<#11178 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7O47CMCGMZHQ6P5AAE4O7LZERUDRAVCNFSM6AAAAABD2PH662VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TKOBQGAZDE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Hi everyone.
We have submitted a
node-mongodb-native
bug report to the MongoDB team: NODE-6179It references this Github issue/discussion. If you could share any additional context or troubleshooting attempts there it would be very helpful. I understand this issue has been affecting many teams and we really appreciate your patience.
Thanks,
Aldo