Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach for entityframework core multi tenancy

We develop a bigger SaaS application based on ASP.NET Core and EFCore. We separate tenants by database, means that we have one database for each tenant.

The database is migrated on any incoming request by a client by a custom middleware. This required, because the application should be immediately available after a tenant signed up on our SSO-Server.

So the pipeline looks like this:

  1. Authentication Middleware (ASP.NET Core Standard)
  2. UserResolverMiddleware (extracts TenantID from the JWT token and adds it to HttpContext.Items)
  3. TenantDatabaseInitializerMiddleware (constructs a TenantDbContext object by passing the tenantid in the ConnectionString and starts migration)

Unfortunately this has some downsides, as I recognized later:

  1. The TenantDatabaseInitializer sometimes uses an TenantDbContext of a different tenant. Therefore no database is migrated and the whole request fails.
  2. If there are a lot of incoming requests for the same tenant, the migrations often overlap and fail. (The Middleware is catching the InvalidOperationExceptions that are thrown, but this does not seem as good solution).
  3. It creates a lot of databases.
  4. The requests take potentially long to complete (check for migration).

Long introduction, short question: Can anyone judge whether this migration workflow is a good practise? If not: Do you have a better idea how I can achieve multi tenancy?

I read about Global Query Filters in EFCore 2.0, but I'm not sure how safe this approach is regarding data isolation.

Thanks!

like image 282
Valentin B. Avatar asked Oct 25 '17 08:10

Valentin B.


People also ask

Which database is best for multi tenants?

SQL Database supports row-level security, which can enforce that data returned from a query be scoped to a single tenant. Processing: A multi-tenant database shares compute and storage resources across all its tenants.

How do you implement multi-tenancy?

We can implement multi-tenancy using any of the following approaches: Database per Tenant: Each Tenant has its own database and is isolated from other tenants. Shared Database, Shared Schema: All Tenants share a database and tables. Every table has a Column with the Tenant Identifier, that shows the owner of the row.

Is a benefit of developing applications in a multi-tenant environment?

What is a benefit of developing applications in a multi-tenant environment? Developing applications in a multi-tenant environment enables multiple users or customers to share the same underlying resources (i.e., database and hardware).

What is the general setup of multi-tenant architecture?

In a multi-tenant architecture, multiple instances of an application operate in a shared environment. This architecture is able to work because each tenant is integrated physically, but logically separated; meaning that a single instance of the software will run on one server and then serve multiple tenants.


1 Answers

I think creating a database per each tenant is not practical. If you have 2000 tenants, then you have to maintain 2000 database instances!

I recommend using 1 database for all the application and tenants. And add a TenantId to most of tables so you can find out which tenant this specific records belong to.

like image 123
Afshar Mohebi Avatar answered Sep 28 '22 18:09

Afshar Mohebi