Next.js Discord

Discord Forum

Too many connections to MongoDB Atlas from Next.js API

Unanswered
Asian paper wasp posted this in #help-forum
Open in Discord
Asian paper waspOP
With only about 50 concurrent users I'm hitting my connection limit (500) on MongoDB Atlas.
Seems like every time a request is made to one of my API endpoints, which connects to the database, then a new connection is made to MongoDB Atlas.
I'm using the recommended dbConnect.js file from Nextjs: https://github.com/vercel/next.js/blob/canary/examples/with-mongodb-mongoose/lib/dbConnect.js

What would you guys recommend doing here?
Upgrade to a dedicated Atlas tier? Make a real backend with Heroku instead of using Vercel serverless?
Or preferably, are there some eaiser option by changing some settings or something?

4 Replies

this might help. anyway connection pooling in Serverless and Edge is tricky.
The Data API is essentially a REST interface to your database you can communicate with over HTTPS and abstracts away the connection monitoring and pooling typically performed by socket-based drivers.

Since connection management isn’t performed at the client level (ex: your serveless functions) but instead at the Data API level, the type of connection-storm behavior you may have previously seen as a result of an influx of traffic triggering a flurry of new serverless processes to spin up wouldn’t occur.

If you are sufferring of migrating mongoose to use Data API. Just try Railway <-> Atlas. Railway is not serverless. Projects in railway are containers. They run until you destroy them


https://www.mongodb.com/community/forums/t/large-number-of-connections-with-mongoose-and-vercel/204917/12
in case of Postgres, Vercel developed web socket connections, which employ the db side connection pooling.
year, HTTPS is really slow. maybe Vercel would be working on web socket version like Postgres.