#Payload CMS on Next.js 15 (Serverless, Vercel) Exceeding MongoDB Atlas M2 Connection Limits

9 messages · Page 1 of 1 (latest)

unique junco
#

I am using Payload CMS (v3.20.0) with Next.js 15, deployed on Vercel (serverless runtime), and using MongoDB Atlas (M2 cluster) as my database.

Problem
Each time I refresh my home page, I receive the following alert from MongoDB Atlas:

You are receiving this alert email because connections to your cluster(s) have exceeded your threshold and are nearing the connection limit for the M2 cluster (Cluster0) in the Project (Project 0) within Organization.

My MongoDB connection limit is 400, and I can see that the number of connections spikes every time the page is refreshed.

What I Have Tried
Ensured a single database connection instance: I am using Payload CMS with the default MongoDB connection settings, but I'm concerned that serverless deployments on Vercel might be opening new connections for each request.
Checked Payload CMS settings: I looked into how Payload handles database connections but couldn't find a built-in solution to reuse connections efficiently.
Investigated Vercel's serverless behavior: Since Vercel uses ephemeral functions, I suspect that each function execution creates a new database connection.
Questions
How can I ensure Payload CMS reuses MongoDB connections instead of opening new ones on each request?
Is there a recommended approach to optimize MongoDB connections for Payload CMS when deployed on a serverless platform like Vercel?
Would switching to a dedicated MongoDB Atlas cluster (M10+) be the only solution, or is there a way to manage connections efficiently within the M2 plan?
Any guidance on handling MongoDB connections efficiently in this setup would be greatly appreciated!

scarlet shore
#

Did you try enabling Vercel's Fluid Compute? It might help reduce the number of database connections per request. But yeah, I think you're right—on a serverless environment like Vercel, it's easy to exceed the connection limits of an M2 cluster. Let me know if Fluid Compute makes a difference for you, as I'm planning to use the same stack in an upcoming project. Thanks!
https://vercel.com/docs/functions/fluid-compute

unique junco
unique junco
# scarlet shore Did you try enabling Vercel's Fluid Compute? It might help reduce the number of ...

Good day, Nacer.

I have observed a significant drop in the number of connections since enabling Vercel's Fluid Compute. Previously, my MongoDB Atlas connection count was around 400, but after turning on Fluid Compute, it has decreased to 110 connections.

It seems that Fluid Compute is helping manage and reuse connections more efficiently in the serverless environment. If you're planning to use the same stack, I’d definitely recommend enabling it to avoid hitting connection limits, especially on lower-tier MongoDB clusters like M2.

Let me know if you need any other insights!

scarlet shore
#

Great!! Even though 110 connections still too much. Maybe if you can benefit more from the next js caching mechanism. Anyways, thank you so much for your feedback

unique junco
# scarlet shore Great!! Even though 110 connections still too much. Maybe if you can benefit mor...

Good day, Nacer.

After enabling Vercel's Fluid Compute and addressing the cold start issue of cloud functions by keeping them warm using a cron job, the number of connections has now dropped further to just 4 connections.

It looks like this combination helps significantly reduce unnecessary database connections in a serverless environment. If you're considering the same setup, I'd highly recommend trying both optimizations.

scarlet shore
#

Hi Tebogo. Excellent job !! and I think on Vercel you don’t pay for the idle time that makes safe I guess.
Thanks again

mortal field