Discriminator Strategy
The discriminator pattern works on a single database service and single schema for all tenants. Constituent tenants are discriminated by a specific strategy such as a tenant_id field embedded in tables containing tenant specific data. Beyond the below pro's/con's this strategy is a non-starter for use case which legally require 'air-space' between tenants.Pros
- Single database and schema instance to manage
- Single schema to backup
- Single schema to archive, upgrade etc.
- Simple reporting across tenants (e.g. SELECT .... GROUP BY tenant_id)
- Single database service account to manage per application.
- Single database instance to tune and maintain.
- Tenant data is interwoven meaning backup & restore is an all or nothing proposition.
- Care needs to be taken with every database interaction that the data returned is appropriately scoped.
- If your database goes down, all your customers go down, therefor necessitating a high availability strategy which is generally a good idea but essential in this strategy.
- If a table becomes corrupted it becomes corrupted for all users.
- If a tenant leaves, it can be tricky to extract and archive the information
- If that tenant comes back it can be trickier to reinsert the data and easier to integrate from scratch.
- While storage is cheap, performance is not and an inactive tenant in a single schema will take up database buffer pool resources simply by it's existence through indices alone.
- Because it is likely that a single service account will be used to access the schema and all tenants reside in the schema it can be challenging to trace database load to specific tenant usage.
- As a single database service is serving all tenants, performance is subject to "noisy neighbors".
Schema Strategy
The schema strategy employs a single database server like the DISCRIMINATOR strategy but specifies a schema instance per tenant meaning that each tenant has complete isolation at the data layer from other tenants.Pros
- Tenant data is robustly isolated from other tenant data
- This in turn means for simpler more robust application development. However the application must be tenant aware and capable of switching tenants reliably.
- Schema & table corruption affects only a single tenant
- Ad-hoc queries are automatically scoped to a single tenant.
- Granular backups can be taken and restored with ease & in parallel.
- Tenants can be migrated to and from different environments easily.
- Instrumentation is available on a per schema basis allowing the attribution of load and bottlenecks to specific tenant generated load.
- Single database service account to manage per application.
- Single database instance to tune and maintain.
- As a single database service is serving all tenants, performance is subject to noisy neighbors similar to the DISCRIMINATOR strategy. However it is trivial to move problem customers onto dedicated databases should the need arise.
- If your database goes down, all your customers go down, again necessitating a good failover strategy.
- Tooling needs to be built to handle schema updates, backups and restores of the tenant schemas with an environment.
- Reporting across tenants requires additional tooling.
- De-normalization of common reference tables may be necessary or a 'common/admin' schema employed and shared by all tenants. This in itself can assist in some of the maintenance tooling mentioned.
Database Strategy
The database strategy takes the SCHEMA strategy one step further whereby each tenant has a separate schema instance on a separate database.Pros
- Tenant data is robustly isolated from other tenant data
- This in turn means for simpler more robust application development.
- Schema & table corruption affects only a single tenant
- Granular backups can be taken and restored with ease & in parallel.
- Tenants can be migrated to and from environments easily.
- Instrumentation is available on a per schema basis allowing the attribution of load and bottlenecks to specific tenant generated load.
- "Noisy neighbor" problems are eliminated at the database layer.
- Multiple databases instances to tune and maintain.
- Additional infrastructure cost of the multiple database instances.
- A connection pool per tenant per application is now required (assuming the application layer is multi tenant) which may require additional tuning when considering the number of application instances you need to scale to and the overhead each connection incurs on your storage service.
- Multiple database service accounts to manage per application.
- This assumes that an application will switch between tenants and therefor need connection credentials to all databases making this strategy equal from a security standpoint to a single service account.
- If a database goes down, only a single tenant is affected.
- Tooling needs to be built to handle schema updates, backups and restores of the entire environment.
- Reporting across tenants requires additional tooling.
- This may be complicated by the multiple service accounts to connect with each database.
References
1. http://stuartingram.com/2016/10/02/spring-boot-schema-based-multi-tenancy/
Comments
Post a Comment