Next.js Discord

Discord Forum

Connection Pool with Next.js & MongoDB

Unanswered
Artois Hound posted this in #help-forum
Open in Discord
Avatar
Artois HoundOP
I went through the official docs on this, but I haven't quite found what I was looking for. I will send one relevant tutorial here, which does not include the Connection Pooling. Anybody has an idea on how to do this correctly for production? https://www.mongodb.com/developer/languages/javascript/nextjs-with-mongodb/

6 Replies

Avatar
tafutada777
basically connection pooling and web sockets won’t work in Serverless environments such as Vercel and Cloudflare. self hosted on VPS is okay.
Avatar
Artois HoundOP
Yes, I am planning to use a VPS, do you know of any tutorial that presents how to set this up?
Avatar
Oak saucer gall
Afaik the MongoClient instance itself creates a pool internally and there's no need to create one yourself: https://www.mongodb.com/docs/manual/administration/connection-pool-overview/

This MongoDB example (which looks similar to the tutorial you posted) uses a single MongoClient which means it utilizes some sort of pooling https://github.com/vercel/next.js/tree/canary/examples/with-mongodb
Avatar
tafutada777
yeah right. in case of VPS, import statement works to access global variables where connections are managed.
that doesn’t work with serverless where instances are ephemeral and short lived
Avatar
Artois HoundOP
Oh, thx for that, I didn't know it creates it internally.