Skip to content

Sharding explained

Jon P Smith edited this page Aug 9, 2023 · 11 revisions

AuthP's multi-tenant uses the enum TenantTypes to define what type of multi-tenant you want to use. By default all the tenant's data is stored in one database, with a DataKey to select each tenant's data. See Multi-tenant explained documentation for an overview of the multi-tenant options.

But if you add the SetupMultiTenantSharding method to your registering of the AuthP code, then an extra TenantTypes called AddSharding is added, which adds the extra ability for a tenant to store its data in its own database. This means that you have two ways to build your multi-tenant app:

TwoWaysAuthPHandlesSharding

This means that when the AddSharding enum is added, then for each HTTP request from a tenant user (i.e. a user that is linked to a specific tenant) the multi-tenant code must select a database so that they can access the tenant's data. The AuthP does this via two parts

1. Getting the database server from the connection strings

Anyone that uses ASP.NET Core will know that the appsettings.json file contains a ConnectionStrings section that holds connection strings. These connection strings usually everything you about a database (database name, server name, authentication, etc.) you want to use. But for AuthP sharding code uses the connection strings to access the server name, authentication, etc. but it will add the database name.

If you look at Example6's appsettings.json you will see that the first connection string (called "DefaultConnection") has the normal format, i.e with the database name included, but the other connection string don't have a database part, which means only defines the server.

So, how do we get the database name? See below.

2. Getting the database name from the sharding entries.

AuthP holds a list of the entries (known as sharding entries) that provides the data needed to create a complete connection string. Each sharding entry contains

Name Value
DatabaseName The name of the database
ConnectionName The name of the connection string defining the server
DatabaseType The EF Core's short name (e.g. SqlServer) of the database type to use
Name This is the unique key to this entry

So the AuthP tenant user (AuthUser class) is linked to the AuthP's Tenant class which hold the Name of the sharding entry to use to obtain a complete connection string

Because the sharding-only approach is a popular choice, the AuthP version 6 provides various features / services to help developers who want to build a sharding-only

Articles / Videos

Concepts

Setup

Usage

Admin

SupportCode

Clone this wiki locally