Too many connections to MongoDB Atlas from Next.js API
Unanswered
Asian paper wasp posted this in #help-forum
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?
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.
https://www.mongodb.com/community/forums/t/large-number-of-connections-with-mongoose-and-vercel/204917/12
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.
@tafutada777 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
Asian paper waspOP
Thank you! This definitely seems like a solution that would work. I'm worried about the speed though as your link also mentions. If my API has to make a HHTP request to another API I'd expect it to take a good while longer than using Mongoose for example. And more complicated code.
The Postgres solution seems great, but yea I don't really wanna switch my database now haha. Hopefully some neat solution like that could be made for Monogdb
The Postgres solution seems great, but yea I don't really wanna switch my database now haha. Hopefully some neat solution like that could be made for Monogdb
year, HTTPS is really slow. maybe Vercel would be working on web socket version like Postgres.