MongoDB Atlas Multi-Tenant Architecture Without Exceeding Connection Limit

Hello,

I’m designing a multi-tenant database for a SaaS application. Each tenant gets their own NextJS application which is hosted in Vercel. All of these NextJS applications can have up to hundreds of concurrent users. This causes a connection limit problem.

How should I design the database so that there won’t be too many simultaneous connections? My initial plan was to use a single ServerlessCluster in which each tenant would get their own database containing relevant data coming mainly from the NextJS application’s users. The problem with this approach seems to be that the ServerlessCluster has a limit of 500 simultaneous connections, which basically means that there cannot be even 500 concurrent users interacting with the NextJS applications. I don’t expect more than 20 tenants, which means that the peak connections should not get over 4000 as 500 connections per tenant concurrently is likely to be enough.

I’ve tried to look into connection pooling, but I haven’t figured a way to make it work with many different databases. So, in short: how can this connection limit problem be solved?

  • Teemu

@7be1cbd7e8b42024c9c2ca2990fc7cb
I have the almost same issue that you faced here. can you help me with how did you overpass the problem?
I found some pool solutions on the internet but I am not satisfied with that solutions.

The best solution I could find is to use a separate Express server hosted in Heroku for handling all database interactions. This way the serverless functions don’t create new connection pools themselves, and the server has a single connection pool per tenant database. In case of a large amount of tenants, it is then possible to manually disconnect and reconnect

1 Like