-1

I have been struggling with the way I should set up my multi-tenant environment. In a previous project I had set it al up as a monolith where there was a tenant entity to bind al the correct data to each other. This way is quite easy to set up but the data of all tenants is in one big database.

My idea now is to set it all up in a different way. I will be using Postgres because of the possibility to also have 'schema'. My idea is to have overall data in the public schema and tenant specific data in the tenants own schema.

There are some struggles that I face where I just can not get my head around. In the picture below, you see a quick sketch of the two scenarios. The bottom one has everything in the public schema where the above one has for example domain and user in the public schema.

My idea with this is that when a customer goes to a tenants specific URL the application does not have to crawl through all possible schemas. Same counts for user. When a user wants to login to the application we also do not want to crawl through all schema's. But how do I set this up in a proper way?

Also when the user manages his domains (add/edit/delete), he is actually managing them on the public schema. Or should I avoid using public at all? I thought of putting them here because otherwise when a user enters a new domain I have to check them also on other tenants schema.

Also can I just simple set the schema name of the tenant in the domain or should this be a connectionstring?

I also read here and there about multi-tenancy but I'm unsure how the application can get the data of a tenant with the use of a different schema (public) to fetch the correct data from the tenants schema. Should I use Inversion of Control to get a specific implementation depending on calling for Domain (public schema) or customer (tenant specific)?

https://nebulab.it/blog/how-to-switch-solidus-ecommerce-to-multi-tenant/

https://michael-mckenna.com/multi-tenant-asp-dot-net-core-application-tenant-resolution

Which Multi-tenant approach is recommended

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Leroy Meijer
  • 1,169
  • 17
  • 40
  • Different applications determine the tenant at different times. Sometimes it's based on information in a web request or it's configured on a specific instance of the application or tenant scoped entities are correlated via an explicit column – Aluan Haddad Aug 15 '20 at 11:36

1 Answers1

1

I personally do something like your first approach + multidatabase using a first database that tells me where each tenant is located. I call it routing database. That dB has 2 tables.

  • databases (if, connection string)
  • tenants (tenant info, database if)

This way I have the data across multiple databases and still multiple tenants per database.

Also the database id => connection string is cached usually with redis so no need to hit the dB on every request (you can even use in memory for this) with cachemanagercore library.

If a tenant requires its own database not shared is easy to handle too. Usually a cookie with the dB id picked up on a Middleware and acceded then by a DatabaseResolver class injected where you need to access the dB with a method like DbContext Getdatabse () is all you need

dariogriffo
  • 4,148
  • 3
  • 17
  • 34
  • Hi @dariogriffo, As I understand you correctly, you add an extra database? Where the public schema should be held? Each Tenant is located in a specific schema that is determined by the domain. Where would you store (all) the domain(s)? Not within that seperate databases per tenant right? The same counts for users? Would you first fetch and store all users, domains and tenants at start of the application? Store them in redis for example? I do appreciate you pointing me out in a certain direction, thank you! – Leroy Meijer Aug 16 '20 at 09:50