#Multi Tenancy SaaS Backend

5 messages · Page 1 of 1 (latest)

ember fable
#

I plan to create a multi tenant saas WebApp with NestJs + Prisma + MySQL as backend.
One Database each table hat a id and tenant_id column.

For approximately 100 Tenants each tenant had about 2000 users.

I plan to Identify each tenant by header X-Tenant-Id: tenant1

I have found somethin about Durable provider in the NestJs docs:
https://docs.nestjs.com/fundamentals/injection-scopes

But in there is a Hint that this strategy is not ideal for applications operating with a large number of tenants.
What is a "large number of tenant" and what is the best alternative ?

Is there a best practice example for NestJS & Prisma Multi tenancy ?

For scaling I plan something like this, but is this a good idea ?
Database Server 1
with DB 1 and Tenant 1 - 10
with DB2 and Tenant 10-20
with DB 3 and Tenant 20-30

Database Server 2
with DB 4 and Tenant 40-50
with DB 5 and Tenant 50-60
with DB 6 and Tenant 60-70

Database Server 3
with DB 4 and Tenant 70-80
with DB 5 and Tenant 80-90
with DB 6 and Tenant 90-100

crude flint
#

It's not a "large number of tenants." I think you should avoid over-engineering. It makes more sense to have a properly planned database schema and relationships. Of course, this depends on the data you will store in the database. If necessary, you could consider a master/slave setup if one database becomes insufficient at some point.

ember fable
#

You're right, but the Backend could be also the bottle neck, is it a good idea use the contextIdStrategy instead of the Request Scoped Provider ?

sinful bridge
#

I'm going to offer an alternative solution to propagating the tenant context - one that I originally created for the exact use-case that you're solving now.

https://www.npmjs.com/package/nestjs-cls

#

Request scoping the entire application would get you tangled in a hot mess of issues of not having access to request scope everywhere you need and prevent you from using scoped repositories in singletons. Using AsyncLocalStorage makes that much easier.