A software architecture known as multi-tenancy is one in which a single instance of the program operates on a server and caters to several tenants.
When a customer uses a SAAS-based platform to manage their business operations, they are referred to as tenants. Tenants are segregated from one another with respect to their data, user accounts, and configuration settings.
Because a single software instance can serve several clients, eliminating the need for separate servers and infrastructure for each client, multi-tenancy enables the efficient use of resources.
Because updates can be done to a single instance of the software rather than needing to update several instances for each customer, it also makes maintenance and upgrades easier.
Various methods of execution
Multi-tenancy implementation can be divided into three categories.
One Database
A single database instance and schema are shared by all tenants. This method uses the fewest resources, and it makes evaluating data from different tenants easier. This method streamlines management and is economical.
However, with this strategy, security becomes a crucial concern because there is a greater chance of performance bottlenecks as the number of renters rises. Adherence to data requirements can become intricate and demanding to oversee, particularly when tenants function in disparate locations with disparate data protection statutes.
One database, each with its own schema
While each tenant will have their own schema, all tenants will share a single database. This method is more resource-efficient than having separate databases, yet it still offers a high level of security and isolation. Operating expenses are decreased by managing a single database, as are administrative duties like backups and upgrades.
However, there is a chance for data contention and performance degradation in situations where there is intense competition for resources, like frequent read and write operations across various schemas.
Different Database
Tenant-specific data will be stored in a single common database within the application, while each tenant will have their own database. Stronger protection and isolation from several databases lowers the possibility of tenant illegal access and data breaches.
Because each tenant’s database may be scaled independently depending on their unique requirements and usage patterns, scalability is improved.
Nevertheless, maintaining several databases makes administrative duties more difficult. Synchronization issues could occur, which could result in inconsistent data between databases. increased use of resources in terms of infrastructure expenses, processing, and storage.
Today, In this blog, we will learn how to implement a separate database approach of multi-tenancy in the .Net Core application and how to provide a dynamic connection string to DbContext.
I will give a demo of an Application where I will take two DbContext classes.
- One DB Context for the main database, which will store tenant-related stuff such as tenant details, tenant user logins, etc.
- Second DB Context for each tenant database, we will connect this database based on TenantId
We will add a request header with the name x-tenant-id, which will have the tenantId for which we want to fetch data.
Step 1. Create a Tenant Pod table. We can use this table to store tenant connection strings for the database. If we don’t want to store the connection string directly in DB, we can Azure Key Vault or Aws Secret Manager and, in this DB, we can store Secret Name.
Step 2. Create a Method to get the connection string based on the request header.
Step 3. In the Db Context, add logic to use the above method, Override OnConfiguring Method of the DBContext class.
That’s It. Now, you can use this DB Context in the repository or service layer, and this DbContext will fetch the connection string based on the TenantId passed. We need to make sure every API has a tenant in the request header.
Note. Here, in this example, I have a Tenant table that is duplicated in both Db, but to use the constraint, we need to duplicate the table, and we should have logic to synchronize the data across the database. Also, we can implement Redis Cache or In Memory Cache Service to fetch the connection string from In Memory so that this can be faster. In My Next Article, We will learn how to use Memory Cache and Redis Cache in .Net Core.
Conclusion
In this article, we learned about multi-tenancy and its different approaches. Also, we explored how to connect Databases dynamically based on TenantId.
ASP.NET 8 Hosting Recommendation
ASP.NET is a powerful platform for creating web applications and services. You must be comfortable with JavaScript, HTML, CSS, and C# before developing a web application in ASP.NET. On the market, there are thousands of web hosting companies providing ASP.NET Hosting. But, only very few web hosting companies could provide high quality ASP.NET hosting solution.
ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.