Multitenant architectures provide great opportunities for scaling, but most solutions do not provide a way to share data between accounts. In this talk, we will introduce core concepts of multitenancy and explore a data model that allows two clients to share subsets of data instantly using a Laravel application and MySQL database.
Decoding Movie Sentiments: Analyzing Reviews with Data Analysis model
Sharing data in a multitenant architecture
1. SHARING DATA IN A
MULTITENANT ARCHITECTURE
DAN FEY
https://github.com/crowdskout/laravel-shared-multitenancy
https://joind.in/talk/71f10
2. About Dan Fey
Grew up in northern NJ
Back-end engineer at Crowdskout in DC
Work on API and data layers in PHP using Laravel
Work with MySQL, Mongo, and Elasticsearch
3. Crowdskout
Data collection, analytics, and outreach SaaS
platform
Collects, normalizes, and matches data for
customers
Provides tools for segmenting audiences, building
dynamic charting, and acting on segments
4. In this talk
Foundations of Multitenancy
Data sharing with Multitenancy
Laravel/MySQL application demonstrating data
sharing with Multitenancy
5. Multitenant CRM/Contact List
Build a scalable, self-serve CRM application
View profiles/contacts
Allow customers to share subsets of profiles
Customers have multiple users that can view
their data
6. Multitenancy
Sharing resources among multiple tenants (i.e.
accounts/customers/users)
Can save on infrastructure, development,
maintenance
Great for scaling
Three general forms: separate databases,
separate schema, shared schema.
8. Separate Databases
Separate database servers per tenant
Application shared
Can extend/change database/schema per tenant
Adding tenants requires infrastructure
Each tenant has isolated database resources
10. Separate Schemas
Same database server, different schemas per
tenant
Application shared
Can extend/change schema per tenant, but not
database type/configuration
Less resource isolation
Adding tenants may require operations/migrations
12. Shared Schema
Shared application, database server, and schema
for all tenants
Harder to extend/change schema or database per
tenant
Harder to isolate resources
Adding tenants requires minimal changes
18. Multitenant CRM/Contact List
Build a scalable, self-serve CRM application
View profiles/contacts
Allow customers to share subsets of profiles
Customers have multiple users that can view
their data
19.
20. Demo 1
Create Customer table and model
Create Profile, Name, Email tables and models
Populate Users, Customers, and Profiles
Login to app
48d18621
26. Pivoting
Customers with Users
user_id name
1 Dan
2 Bob
3 Amy
customer_id user_id
Google 5 1 Dan
Apple 6 1 Dan
Apple 6 2 Bob
Google 5 3 Amy
customer_id name
5 Google
6 Apple
27. Demo 2
Add customer_user pivot table and model
Add belongs to many relationships
php artisan db:seed --class=CustomerUserSeeder
31. Sources Data Model
profile_id first last source_id
1 Kenyon Harris 2
2 Bette Kemmer 1
3 Oscar Gorczany 1
profile_id email source_id
1 kharris@test.com 1
1 kharris@example.com 2
2 bkemmer@test.com 1
3 ogorcz@test.com 1
source_id name
1 Imported
2 Web Form
32. Pivoting
Customers with Sources
customer_id source_id
Apple 6 1 Apple Imported
Apple 6 3 Public Data
Google 5 3 Public Data
customer_id name
5 Google
6 Apple
source_id name
1 Apple Imported
2 Google Forms
3 Public Data
33. Demo 3
Add sources table, add source_id to profile data
tables
Adding query scopes for access control
Show access filtering
Show adding/removing access
fd0eafa
39. Advantages of Sources
Data Model
Instantly share/remove large subsets of data
Updates to data points shared between customers
Data sources are isolated for change/cleaning/
deleting
Granular to row level
Model is scalable
40. Issues with Sources Data
Model
Duplicate data
Requires matching
Application code can get complex
41. Code tweaks for scalability
Cache requests to high use tables and queries
users
sources
client_users
client_sources
Add scrolling support with LIMIT and OFFSET
Use Query Builder instead of Eloquent or even raw queries
when necessary
43. Extending - additional
profile tables
Examples: date of birth, gender, phone numbers
Add new table with new fields including profile id
and source id
Add new info to the views to display
Also works with activities like emailings, phone
calls, or page views
44. Additional profile tables
date_of_births
prf_profiles_id int
date_of_birth datetime
source_id int
phones
prf_profiles_id int
phone_number string
source_id int
genders
prf_profiles_id int
gender
enum('male',
'female', 'other')
source_id int
phone_calls
prf_profiles_id int
customer_id int
contact_datetime datetime
phone_number string
source_id int
46. Extending - add user
permissioning
Examples: some users can only see names, not
email addresses
Add new table for permissions, and a pivot table
for user_permissions
Check permissions before querying data to limit
access to certain tables
48. Extending - adding search
capabilities
Examples: search for profiles by name, email, etc.
Possible on smaller scales, but full-text search is
difficult to scale with MySQL
Elasticsearch is great for this, but adds complexity
Data must be replicated into Elasticsearch and
kept in-sync
Does not use SQL for querying