Search code examples
phplaraveldatabase-schemamulti-tenantsaas

best approach for multi tenant SaaS application


I'm trying to build a Multi tenant SaaS application, and i know there are multiple approaches to that, but i picked the multi-database one i think it would fit most.

so I create a db for each tenant and store a table of tenants in the main db and connect users to their respective db based on the subdomain.

now my issue here is where to store the users, in the main db? or the tenant db, storing the users in the main db is going to make it difficult to get user related models in other db's, but storing it inside tenants db would make it difficult to authenticate on all users ...

also what's the best scenario?

  1. authenticate, get jwt token.
  2. send token with each request.
  3. on each request validate token, check subdomain, connect to respective tenant db, execute request.

is this a good approach? what should I do with the users table issue? ThnQ


Solution

  • I can offer third option. Have user both in tenant and main db. You can then create procedure to update main db when user changes in tenant db (or vice versa).

    Also, I don't know models in Laravel, but MySQL doesn't have problem with cross database JOINs.