Connection Pool with Next.js & MongoDB
Unanswered
Artois Hound posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/0.png)
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](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
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](https://cdn.discordapp.com/embed/avatars/0.png)
Artois HoundOP
Yes, I am planning to use a VPS, do you know of any tutorial that presents how to set this up?
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
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
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](https://cdn.discordapp.com/avatars/809942393637306400/1e4e5a29a08b96de9a1e3d3520b5e771.webp?size=256)
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](https://cdn.discordapp.com/embed/avatars/0.png)
Artois HoundOP
Oh, thx for that, I didn't know it creates it internally.