An introduction into creating a multi tenant SaaS application, creating a database per tenant architecture. Incluiding a case study, example and general pointers
5. •Single point of failure
•More expensive
•Cannot grow in small steps
•Scaling up is failing up [1]
[1] Scalability rules: 50 principles for Scaling Web Sites
WHY?
9. 100% tenant isolation
•Snapshot backup/restore
•Recover to point in time
•Compliance
•Limited authorization risks
100% tenant isoluation
10.
11. •Designed for single tenant, single db
•Move to the cloud, one by one
•Use case I will present
Cloud strategy for legacy applications
12. WONDERING WHO
IS THAT GUY?
HENRY BEEN
Independent Devops & Azure Architect
E: consultancy@henrybeen.nl
T: @henry_been
L: linkedin.com/in/henrybeen
W: henrybeen.nl
13. A (very) short history of multi-tenancy
Tenant C
Standalone App
Tenant B
Tenant
DB
App
Tenant A
14. A (very) short history of multi-tenancy
App
Sharded Multi-tenant Database
Catalog
Tenant
C
Tenant
B
App
Tenant
M
Catalog
Tenants
A,B,C,D
Tenant
A
Tenants
E,F,J,K
Share everything Database per Tenant
Tenant
DB
App
Tenant
L
15. "Incidental Expenses" (CC BY-NC 2.0) by tim ellis
"O’ the Mysteries of Colorado" (CC BY-NC 2.0) by WarzauWynn
25. Catalog
Cust 1 Cust 2 Cust 3 Cust N
2. App uses key to get
connection from catalog
Tenant Databases
4. On subsequent requests, use a
cache!
Tenant Catalog
1. User connects to the app
Selfservice tenant creation and destruction
A. User signs up for a new
tenant
Cust 4
B. App provisions new
tenant db
C. …registers tenant key,
db location in catalog
3. …then connects to correct
tenant database
ARM / SQL Database
Tenant Onboarding Application Connection
Base
TenantDB
Tenant
bacpac
Apps
26. Catalog
Cust 1 Cust 2 Cust 3 Cust N
Tenant Databases
Tenant Catalog
More simple approach: no selfservice
Apps
Management scripts
AAD
27. So…. what do I need?
"tools" (CC BY-NC-ND 2.0) by ᴾᴴᴵᴸ
31. Elastic Database Client Library
using (SqlConnection conn = customerShardMap.OpenConnectionForKey(
customerId,
Configuration.GetCredentialsConnectionString(),
ConnectionOptions.Validate))
{
// Execute a simple command.
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"UPDATE Sales.Customer ….";
// … and more boring stuff ..
}
32. Elastic Transactions
using (var scope = new TransactionScope())
{
using (var conn1 = new SqlConnection(connStrDb1))
{ // … boring stuff … }
using (var conn2 = new SqlConnection(connStrDb2))
{ // … boring stuff }
scope.Complete();
}
33. Cust 1 Cust 2 Cust 3 Cust N
Schema management at scale
Cust 4
Catalog
Manage 1000s of databases as one
Apps
T-SQLT-SQLT-SQL T-SQLT-SQL
Azure Portal
Job
Account
T-SQL
CREATE TABLE…
CREATE INDEX…
INSERT INTO…
SELECT * FROM…
T-SQL Job
SELECT * FROM…
Elastic Jobs
Tenant Databases
Tenant Catalog
Jobs, target groups,
schedules, credentials
34. Cust 1 Cust 2
Tenant Databases
Cust 3 Cust N
Distributed query across tenant databases
Tenant Catalog
Adhoc
Analytics
Cust 4
Catalog
Database locations are
retrieved from catalog
Distributed query plan
External tables
project data from
tenant dbs
PowerBI
Query from any client
Query all tenants as if they are in a single database
Apps
Elastic Query
37. Standalone App Database per tenant Sharded Multi-tenant
Scale High
1-1000s
Very high
1-100,000s
Unlimited
1-1,000,000s
Database cost–per tenant High (sized for peaks) Low (using pools) Lowest (small tenants)
Tenant isolation Very High High Low (high for singletons)
Performance monitoring/mgt. Per-tenant Aggregate + per-tenant Aggregate, per-tenant for
singletons only
Restore single tenant Easy Easy Hard (easy for singletons)
Disaster recovery (all tenants) Moderate
Many components to
recover
Moderate
Patterns address complexity at
scale
Easy (for multi-tenant dbs)
Patterns address singleton
complexity at scale
Development complexity Low Low Medium (sharding)
Operational complexity Medium-High
Individually simple, complex
at scale
Low-Medium
Patterns address complexity at
scale
Low-High
Individual tenant management
is complex
Per-tenant customization Easy Easy Complex
Comparing the models, what we see…
38.
39. DO TRY THIS AT HOME!
HENRY BEEN
Independent Devops & Azure Architect
E: consultancy@henrybeen.nl
T: @henry_been
L: linkedin.com/in/henrybeen
W: henrybeen.nl
Editor's Notes
Licence notice for those parts I copied in from the WingTips presentations
MIT License Copyright (c) Microsoft Corporation. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
"O’ the Mysteries of Colorado" (CC BY-NC 2.0) by WarzauWynn
The Masterplan
The Masterplan
Image from Wikipedia, https://upload.wikimedia.org/wikipedia/commons/c/ca/DART_UML_DART_2011_2013_RAW.svg
"tools" (CC BY-NC-ND 2.0) by ᴾᴴᴵᴸ
Real telemetry from an accounting customer’s set of pools (when this data was recorded they had over 300 pools, and each pool had nearly 400 databases sharing 200 DTUs).
As you can see, their aggregate tenant workload is easily supported by 200 DTU pool.
[The pool at 200 DTUs is twice the per-database max of 100 DTUs so would allow 2 databases to be active]