SlideShare a Scribd company logo
1 of 48
Download to read offline
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
Scaling Multi-Tenant
Applications Using the
Django ORM & Postgres
Sai Srirampur
PyCon Canada | Toronto | Nov 2018
Sai Srirampur | PyConCA 2018
• Sai Srirampur a.k.a Sai
• Engineer at Citus Data
• Joined Citus to make it so
developers never have to
worry about scaling their
database
• Creator django multi-tenant
• Follow me @saisrirampur
@citusdata
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Talk about my favorite things
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
PYTHON &
DJANGO
POSTGRES SCALING
MULTI-TENANT
APPLICATIONS
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
Why 💙 Postgres? TLDR;
Open source
Constraints
Extensions
PostGIS / Geospatial
HLL, TopN, Citus
Foreign data wrappers
Rich SQL
CTEs
Window functions
Full text search
Datatypes
JSONB
Sai Srirampur | PyConCA 2018
Today… Scaling Multi-Tenant Apps
Using the Django ORM & Postgres
3 architectures to build Multi-tenant apps
django_multitenant
Scaling multi-tenant apps with distributed
postgres
Sai Srirampur | PyConCA 2018
• Multiple customers
(“tenants”)
• Each with own data
• SaaS
• Shopify, Salesforce
Multi-Tenant Apps
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
Why talk
about scalable
multi-tenant
applications?
tens
100’s
1000’s
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
Architectures
To
Build
Multi-Tenant
Applications3
Sai Srirampur | PyConCA 2018
[1] One database per
tenant
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
What defines a Database?
• Organized collection of interrelated data
• Don’t share resources.
• Username and password
• Connections
• Memory
Sai Srirampur | PyConCA 2018
One database per tenant
Tenant 1251 Tenant 1252
Tenant 5
[1]
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
CREATE DATABASE tenant_100;
./manage.py migrate --database=tenant_100;
One database per tenant / Onboarding
database_routing: db_for_read, db_for_write etc.
Sai Srirampur | PyConCA 2018
● Start quickly
● Isolate customer (tenant) data
● Compliance is a bit easier
● Time for DBA/developer to manage
● Maintain consistency (ex: create
index across all databases)
● Longer running migrations
● Performance degrades as #
customers (tenants) goes up
PROS CONS
[1] One Database Per Tenant
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
[2] One schema per
tenant
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
What defines a database Schema?
• Logical namespaces to hold a set of tables
• Share resources:
• Username and password
• Connections
• Memory
Sai Srirampur | PyConCA 2018
One schema per tenant
Tenant 5 Tenant 1251
Tenant 1252
Database
[2]
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
CREATE schema tenant_100;
./manage.py migrate --schema=tenant_100;
app_code for schema routing based on tenant
One schema per tenant / Onboarding
Sai Srirampur | PyConCA 2018
● Better resource utilization vs. one
database per tenant
● Start quickly
● Logical isolation
● Hard to manage (ex: add column
across all schemas)
● Longer running migrations
● Performance degrades as #
customers (tenants) goes up
PROS CONS
[2] One Schema Per Tenant
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
[3] Shared table
architecture
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Accounts
Shared table architecture
Campaigns
Leads TenantId
5
5
5
1251
Database
[3]
Sai Srirampur | PyConCA 2018
Example
Sai Srirampur | PyConCA 2018
● Easy maintenance
● Faster running migrations
● Best resource utilization
● Faster performance
● Scales to 1k-100k tenants
● Application code to guarantee
isolation
● Make sure ORM calls are always
scoped to a single tenant
PROS CONS
[3] Shared Table Architecture :-)
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Comparing the 3 architectures for scaling
multi-tenant Django applications
ONE
DATABASE
PER TENANT
ONE
SCHEMA
PER TENANT
SHARED
TABLE
ARCHITECTURE
Database
Database
[1]
[2]
[3]
start quickly & isolation guarantees,
bad resource utilization & not scalable
better resource util, logical isolation
not scalable
Scales to over 100K tenants!
explicitly handle isolation
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
django_multitenant to the rescue
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
django_multitenant
Automates all ORM calls
to be scoped to a single tenant
Sai Srirampur | PyConCA 2018
Today...
Purchase.objects.filter(id=1)
<=>
"SELECT* from purchase where id=1"
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
With django_multitenant
Purchase.objects.filter(id=1)
<=>
"SELECT* from purchase where id=1 and
store_id=<current_tenant>"
Sai Srirampur | PyConCA 2018
Components of
django_multitenant
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
django_multitenant usage — 3 steps
1. Inherit all models with TenantModel
2. Change ForeignKey to TenantForeignKey
3. Define tenant scoping: set_current_tenant(t)
Sai Srirampur | PyConCA 2018
TenantModel defines new Manager
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
TenantForeignKey mimics composite
foreign key behavior
• Adds tenant_id filter to referenced model. Handles:
• Reference lookups — ex: (Purchase.product.name)
• select_related() / prefetch_related()
• Explicit Joins (product__name)
Sai Srirampur | PyConCA 2018
set_current_tenant(t)
• Specifies which tenant the APIs should be scoped to
• Set at authentication logic via middleware
• Set explicitly at top of function (ex. view, external
tasks/jobs)
Sai Srirampur | PyConCA 2018
Example Code
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Models without django_multitenant
class Purchase(models.Model):
store = models.ForeignKey(Store)
product_purchased = models.ForeignKey(Product)
ordered_at = models.DateTimeField(default=timezone.now)
billing_address = models.TextField()
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Post django_multitenant
class Purchase(TenantModel):
store = models.ForeignKey(Store)
product_purchased = TenantForeignKey(Product)
ordered_at = models.DateTimeField(default=timezone.now)
billing_address = models.TextField()
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Setting the tenant at authentication
class SetCurrentTenantFromUser(object):
def process_request(self, request):
if not hasattr(self, 'authenticator'):
from rest_framework_jwt.authentication import
JSONWebTokenAuthentication
self.authenticator = JSONWebTokenAuthentication()
try:
user, _ = self.authenticator.authenticate(request)
except:
return
try:
#Assuming your app has a function to get the tenant associated
for a user
current_tenant = get_tenant_for_user(user)
except:
# TODO: handle failure
return
set_current_tenant(current_tenant)
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Benefits of django_multitenant
Drop-in implementation of shared tables architecture
Guarantees isolation
Ready to scale with distributed Postgres (Citus)
Sai Srirampur | PyConCA 2018
Sai Srirampur | PyConCA 2018
Infinite scale
with Citus
Sai Srirampur | PyConCA 2018
django_multitenant with Postgres
Sai Srirampur | PyConCA 2018
django_multitenant with Citus
Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018
django_multitenant with Citus
Sai Srirampur | PyConCA 2018
also one of my favorites: Citus
Scale out Django!
github.com/citusdata/django-multitenant
@saisrirampur @citusdata
sai@citusdata.com
citusdata.com/newsletter
Thank You

More Related Content

What's hot

IPC Global Big Data To Decision Solution Overview
IPC Global Big Data To Decision Solution OverviewIPC Global Big Data To Decision Solution Overview
IPC Global Big Data To Decision Solution Overview
pzybrick
 
Serving the Real-Time Data Needs of an Airport with Kafka Streams and KSQL
Serving the Real-Time Data Needs of an Airport with Kafka Streams and KSQL Serving the Real-Time Data Needs of an Airport with Kafka Streams and KSQL
Serving the Real-Time Data Needs of an Airport with Kafka Streams and KSQL
confluent
 

What's hot (19)

MongoDB World 2019: A MongoDB Journey: Moving From a Relational Database to M...
MongoDB World 2019: A MongoDB Journey: Moving From a Relational Database to M...MongoDB World 2019: A MongoDB Journey: Moving From a Relational Database to M...
MongoDB World 2019: A MongoDB Journey: Moving From a Relational Database to M...
 
Metrics-Driven Performance Tuning for AWS Glue ETL Jobs (ANT332) - AWS re:Inv...
Metrics-Driven Performance Tuning for AWS Glue ETL Jobs (ANT332) - AWS re:Inv...Metrics-Driven Performance Tuning for AWS Glue ETL Jobs (ANT332) - AWS re:Inv...
Metrics-Driven Performance Tuning for AWS Glue ETL Jobs (ANT332) - AWS re:Inv...
 
Aws re invent 2018 recap
Aws re invent 2018 recapAws re invent 2018 recap
Aws re invent 2018 recap
 
Streaming Event Time Partitioning with Apache Flink and Apache Iceberg - Juli...
Streaming Event Time Partitioning with Apache Flink and Apache Iceberg - Juli...Streaming Event Time Partitioning with Apache Flink and Apache Iceberg - Juli...
Streaming Event Time Partitioning with Apache Flink and Apache Iceberg - Juli...
 
Building A Self Service Streaming Platform at Pinterest - Steven Bairos-Novak...
Building A Self Service Streaming Platform at Pinterest - Steven Bairos-Novak...Building A Self Service Streaming Platform at Pinterest - Steven Bairos-Novak...
Building A Self Service Streaming Platform at Pinterest - Steven Bairos-Novak...
 
HOP! Airlines Jets to Real Time
HOP! Airlines Jets to Real TimeHOP! Airlines Jets to Real Time
HOP! Airlines Jets to Real Time
 
MongoDB and the Future of Workspaces
MongoDB and the Future of WorkspacesMongoDB and the Future of Workspaces
MongoDB and the Future of Workspaces
 
R, Spark, Tensorflow, H20.ai Applied to Streaming Analytics
R, Spark, Tensorflow, H20.ai Applied to Streaming AnalyticsR, Spark, Tensorflow, H20.ai Applied to Streaming Analytics
R, Spark, Tensorflow, H20.ai Applied to Streaming Analytics
 
Christoph Bussler [Google Cloud] | IoT Event Processing and Analytics with In...
Christoph Bussler [Google Cloud] | IoT Event Processing and Analytics with In...Christoph Bussler [Google Cloud] | IoT Event Processing and Analytics with In...
Christoph Bussler [Google Cloud] | IoT Event Processing and Analytics with In...
 
Inneractive - Spark meetup2
Inneractive - Spark meetup2Inneractive - Spark meetup2
Inneractive - Spark meetup2
 
Easy and Efficient Batch Computing on AWS
Easy and Efficient Batch Computing on AWSEasy and Efficient Batch Computing on AWS
Easy and Efficient Batch Computing on AWS
 
IPC Global Big Data To Decision Solution Overview
IPC Global Big Data To Decision Solution OverviewIPC Global Big Data To Decision Solution Overview
IPC Global Big Data To Decision Solution Overview
 
2018-10-23 5 C - Accelerating developement and business with Azure containers...
2018-10-23 5 C - Accelerating developement and business with Azure containers...2018-10-23 5 C - Accelerating developement and business with Azure containers...
2018-10-23 5 C - Accelerating developement and business with Azure containers...
 
Accelaratinng developmennt and business with azure conntainers
Accelaratinng developmennt and business with azure conntainersAccelaratinng developmennt and business with azure conntainers
Accelaratinng developmennt and business with azure conntainers
 
From logging to monitoring to reactive insights - C Schneider
From logging to monitoring to reactive insights - C SchneiderFrom logging to monitoring to reactive insights - C Schneider
From logging to monitoring to reactive insights - C Schneider
 
[NEW LAUNCH!] Introducing AWS Ground Station – Fully managed Ground Station a...
[NEW LAUNCH!] Introducing AWS Ground Station – Fully managed Ground Station a...[NEW LAUNCH!] Introducing AWS Ground Station – Fully managed Ground Station a...
[NEW LAUNCH!] Introducing AWS Ground Station – Fully managed Ground Station a...
 
CGSpace Update, 2015–2016
CGSpace Update, 2015–2016CGSpace Update, 2015–2016
CGSpace Update, 2015–2016
 
Serving the Real-Time Data Needs of an Airport with Kafka Streams and KSQL
Serving the Real-Time Data Needs of an Airport with Kafka Streams and KSQL Serving the Real-Time Data Needs of an Airport with Kafka Streams and KSQL
Serving the Real-Time Data Needs of an Airport with Kafka Streams and KSQL
 
Flink Forward Berlin 2017: Gyula Fora - Building and operating large-scale st...
Flink Forward Berlin 2017: Gyula Fora - Building and operating large-scale st...Flink Forward Berlin 2017: Gyula Fora - Building and operating large-scale st...
Flink Forward Berlin 2017: Gyula Fora - Building and operating large-scale st...
 

Similar to Scaling Multi-tenant Applications Using the Django ORM & Postgres | PyCon Canada 2018 | Sai Srirampur

Learning to Rank: From Theory to Production - Malvina Josephidou & Diego Cecc...
Learning to Rank: From Theory to Production - Malvina Josephidou & Diego Cecc...Learning to Rank: From Theory to Production - Malvina Josephidou & Diego Cecc...
Learning to Rank: From Theory to Production - Malvina Josephidou & Diego Cecc...
Lucidworks
 

Similar to Scaling Multi-tenant Applications Using the Django ORM & Postgres | PyCon Canada 2018 | Sai Srirampur (20)

InfluxDB 101 – Concepts and Architecture by Michael DeSa, Software Engineer |...
InfluxDB 101 – Concepts and Architecture by Michael DeSa, Software Engineer |...InfluxDB 101 – Concepts and Architecture by Michael DeSa, Software Engineer |...
InfluxDB 101 – Concepts and Architecture by Michael DeSa, Software Engineer |...
 
Make your data fly - Building data platform in AWS
Make your data fly - Building data platform in AWSMake your data fly - Building data platform in AWS
Make your data fly - Building data platform in AWS
 
How Trek10 Uses Datadog's Distributed Tracing to Improve AWS Lambda Projects ...
How Trek10 Uses Datadog's Distributed Tracing to Improve AWS Lambda Projects ...How Trek10 Uses Datadog's Distributed Tracing to Improve AWS Lambda Projects ...
How Trek10 Uses Datadog's Distributed Tracing to Improve AWS Lambda Projects ...
 
Analyze Amazon CloudFront and Lambda@Edge Logs to Improve Customer Experience...
Analyze Amazon CloudFront and Lambda@Edge Logs to Improve Customer Experience...Analyze Amazon CloudFront and Lambda@Edge Logs to Improve Customer Experience...
Analyze Amazon CloudFront and Lambda@Edge Logs to Improve Customer Experience...
 
OSMC 2022 | The Power of Metrics, Logs & Traces with Open Source by Emil-Andr...
OSMC 2022 | The Power of Metrics, Logs & Traces with Open Source by Emil-Andr...OSMC 2022 | The Power of Metrics, Logs & Traces with Open Source by Emil-Andr...
OSMC 2022 | The Power of Metrics, Logs & Traces with Open Source by Emil-Andr...
 
Streamlining Feature Engineering Pipelines with Open Source
Streamlining Feature Engineering Pipelines with Open SourceStreamlining Feature Engineering Pipelines with Open Source
Streamlining Feature Engineering Pipelines with Open Source
 
Reduce Costs and Build a Strong Operational Foundation with the AWS Migration...
Reduce Costs and Build a Strong Operational Foundation with the AWS Migration...Reduce Costs and Build a Strong Operational Foundation with the AWS Migration...
Reduce Costs and Build a Strong Operational Foundation with the AWS Migration...
 
QCon 2018 | Gimel | PayPal's Analytic Platform
QCon 2018 | Gimel | PayPal's Analytic PlatformQCon 2018 | Gimel | PayPal's Analytic Platform
QCon 2018 | Gimel | PayPal's Analytic Platform
 
TigerGraph UI Toolkits Financial Crimes
TigerGraph UI Toolkits Financial CrimesTigerGraph UI Toolkits Financial Crimes
TigerGraph UI Toolkits Financial Crimes
 
MongoDB World 2019: Unleash the Power of the MongoDB Aggregation Framework
MongoDB World 2019: Unleash the Power of the MongoDB Aggregation FrameworkMongoDB World 2019: Unleash the Power of the MongoDB Aggregation Framework
MongoDB World 2019: Unleash the Power of the MongoDB Aggregation Framework
 
The Past, Present, and Future of Apache Flink®
The Past, Present, and Future of Apache Flink®The Past, Present, and Future of Apache Flink®
The Past, Present, and Future of Apache Flink®
 
SaaS Reference Architectures: Review of Real-World Patterns & Strategies (GPS...
SaaS Reference Architectures: Review of Real-World Patterns & Strategies (GPS...SaaS Reference Architectures: Review of Real-World Patterns & Strategies (GPS...
SaaS Reference Architectures: Review of Real-World Patterns & Strategies (GPS...
 
How GumGum Migrated from Cassandra to Amazon DynamoDB (DAT345) - AWS re:Inven...
How GumGum Migrated from Cassandra to Amazon DynamoDB (DAT345) - AWS re:Inven...How GumGum Migrated from Cassandra to Amazon DynamoDB (DAT345) - AWS re:Inven...
How GumGum Migrated from Cassandra to Amazon DynamoDB (DAT345) - AWS re:Inven...
 
Con1169 office depot scm cloud mbx session
Con1169 office depot scm cloud mbx sessionCon1169 office depot scm cloud mbx session
Con1169 office depot scm cloud mbx session
 
AI as a Service, Build Shared AI Service Platforms Based on Deep Learning Tec...
AI as a Service, Build Shared AI Service Platforms Based on Deep Learning Tec...AI as a Service, Build Shared AI Service Platforms Based on Deep Learning Tec...
AI as a Service, Build Shared AI Service Platforms Based on Deep Learning Tec...
 
Learning to Rank: From Theory to Production - Malvina Josephidou & Diego Cecc...
Learning to Rank: From Theory to Production - Malvina Josephidou & Diego Cecc...Learning to Rank: From Theory to Production - Malvina Josephidou & Diego Cecc...
Learning to Rank: From Theory to Production - Malvina Josephidou & Diego Cecc...
 
Sitecore & Salesforce DMP integration
Sitecore & Salesforce DMP integrationSitecore & Salesforce DMP integration
Sitecore & Salesforce DMP integration
 
Blueprint Series: Architecture Patterns for Implementing Serverless Microserv...
Blueprint Series: Architecture Patterns for Implementing Serverless Microserv...Blueprint Series: Architecture Patterns for Implementing Serverless Microserv...
Blueprint Series: Architecture Patterns for Implementing Serverless Microserv...
 
Graph Gurus Episode 25: Unleash the Business Value of Your Data Lake with Gra...
Graph Gurus Episode 25: Unleash the Business Value of Your Data Lake with Gra...Graph Gurus Episode 25: Unleash the Business Value of Your Data Lake with Gra...
Graph Gurus Episode 25: Unleash the Business Value of Your Data Lake with Gra...
 
Cloud Native with Kyma
Cloud Native with KymaCloud Native with Kyma
Cloud Native with Kyma
 

More from Citus Data

Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...
Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...
Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...
Citus Data
 

More from Citus Data (20)

Architecting peta-byte-scale analytics by scaling out Postgres on Azure with ...
Architecting peta-byte-scale analytics by scaling out Postgres on Azure with ...Architecting peta-byte-scale analytics by scaling out Postgres on Azure with ...
Architecting peta-byte-scale analytics by scaling out Postgres on Azure with ...
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
 
JSONB Tricks: Operators, Indexes, and When (Not) to Use It | PostgresOpen 201...
JSONB Tricks: Operators, Indexes, and When (Not) to Use It | PostgresOpen 201...JSONB Tricks: Operators, Indexes, and When (Not) to Use It | PostgresOpen 201...
JSONB Tricks: Operators, Indexes, and When (Not) to Use It | PostgresOpen 201...
 
Tutorial: Implementing your first Postgres extension | PGConf EU 2019 | Burak...
Tutorial: Implementing your first Postgres extension | PGConf EU 2019 | Burak...Tutorial: Implementing your first Postgres extension | PGConf EU 2019 | Burak...
Tutorial: Implementing your first Postgres extension | PGConf EU 2019 | Burak...
 
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Whats wrong with postgres | PGConf EU 2019 | Craig KerstiensWhats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
 
When it all goes wrong | PGConf EU 2019 | Will Leinweber
When it all goes wrong | PGConf EU 2019 | Will LeinweberWhen it all goes wrong | PGConf EU 2019 | Will Leinweber
When it all goes wrong | PGConf EU 2019 | Will Leinweber
 
Amazing SQL your ORM can (or can't) do | PGConf EU 2019 | Louise Grandjonc
Amazing SQL your ORM can (or can't) do | PGConf EU 2019 | Louise GrandjoncAmazing SQL your ORM can (or can't) do | PGConf EU 2019 | Louise Grandjonc
Amazing SQL your ORM can (or can't) do | PGConf EU 2019 | Louise Grandjonc
 
What Microsoft is doing with Postgres & the Citus Data acquisition | PGConf E...
What Microsoft is doing with Postgres & the Citus Data acquisition | PGConf E...What Microsoft is doing with Postgres & the Citus Data acquisition | PGConf E...
What Microsoft is doing with Postgres & the Citus Data acquisition | PGConf E...
 
Deep Postgres Extensions in Rust | PGCon 2019 | Jeff Davis
Deep Postgres Extensions in Rust | PGCon 2019 | Jeff DavisDeep Postgres Extensions in Rust | PGCon 2019 | Jeff Davis
Deep Postgres Extensions in Rust | PGCon 2019 | Jeff Davis
 
Why Postgres Why This Database Why Now | SF Bay Area Postgres Meetup | Claire...
Why Postgres Why This Database Why Now | SF Bay Area Postgres Meetup | Claire...Why Postgres Why This Database Why Now | SF Bay Area Postgres Meetup | Claire...
Why Postgres Why This Database Why Now | SF Bay Area Postgres Meetup | Claire...
 
A story on Postgres index types | PostgresLondon 2019 | Louise Grandjonc
A story on Postgres index types | PostgresLondon 2019 | Louise GrandjoncA story on Postgres index types | PostgresLondon 2019 | Louise Grandjonc
A story on Postgres index types | PostgresLondon 2019 | Louise Grandjonc
 
Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...
Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...
Why developers need marketing now more than ever | GlueCon 2019 | Claire Gior...
 
The Art of PostgreSQL | PostgreSQL Ukraine | Dimitri Fontaine
The Art of PostgreSQL | PostgreSQL Ukraine | Dimitri FontaineThe Art of PostgreSQL | PostgreSQL Ukraine | Dimitri Fontaine
The Art of PostgreSQL | PostgreSQL Ukraine | Dimitri Fontaine
 
Optimizing your app by understanding your Postgres | RailsConf 2019 | Samay S...
Optimizing your app by understanding your Postgres | RailsConf 2019 | Samay S...Optimizing your app by understanding your Postgres | RailsConf 2019 | Samay S...
Optimizing your app by understanding your Postgres | RailsConf 2019 | Samay S...
 
When it all goes wrong (with Postgres) | RailsConf 2019 | Will Leinweber
When it all goes wrong (with Postgres) | RailsConf 2019 | Will LeinweberWhen it all goes wrong (with Postgres) | RailsConf 2019 | Will Leinweber
When it all goes wrong (with Postgres) | RailsConf 2019 | Will Leinweber
 
The Art of PostgreSQL | PostgreSQL Ukraine Meetup | Dimitri Fontaine
The Art of PostgreSQL | PostgreSQL Ukraine Meetup | Dimitri FontaineThe Art of PostgreSQL | PostgreSQL Ukraine Meetup | Dimitri Fontaine
The Art of PostgreSQL | PostgreSQL Ukraine Meetup | Dimitri Fontaine
 
Using Postgres and Citus for Lightning Fast Analytics, also ft. Rollups | Liv...
Using Postgres and Citus for Lightning Fast Analytics, also ft. Rollups | Liv...Using Postgres and Citus for Lightning Fast Analytics, also ft. Rollups | Liv...
Using Postgres and Citus for Lightning Fast Analytics, also ft. Rollups | Liv...
 
How to write SQL queries | pgDay Paris 2019 | Dimitri Fontaine
How to write SQL queries | pgDay Paris 2019 | Dimitri FontaineHow to write SQL queries | pgDay Paris 2019 | Dimitri Fontaine
How to write SQL queries | pgDay Paris 2019 | Dimitri Fontaine
 
When it all Goes Wrong |Nordic PGDay 2019 | Will Leinweber
When it all Goes Wrong |Nordic PGDay 2019 | Will LeinweberWhen it all Goes Wrong |Nordic PGDay 2019 | Will Leinweber
When it all Goes Wrong |Nordic PGDay 2019 | Will Leinweber
 
Why PostgreSQL Why This Database Why Now | Nordic PGDay 2019 | Claire Giordano
Why PostgreSQL Why This Database Why Now | Nordic PGDay 2019 | Claire GiordanoWhy PostgreSQL Why This Database Why Now | Nordic PGDay 2019 | Claire Giordano
Why PostgreSQL Why This Database Why Now | Nordic PGDay 2019 | Claire Giordano
 

Recently uploaded

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Scaling Multi-tenant Applications Using the Django ORM & Postgres | PyCon Canada 2018 | Sai Srirampur

  • 1. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 Scaling Multi-Tenant Applications Using the Django ORM & Postgres Sai Srirampur PyCon Canada | Toronto | Nov 2018
  • 2. Sai Srirampur | PyConCA 2018 • Sai Srirampur a.k.a Sai • Engineer at Citus Data • Joined Citus to make it so developers never have to worry about scaling their database • Creator django multi-tenant • Follow me @saisrirampur @citusdata
  • 3. Sai Srirampur | PyConCA 2018
  • 4. Sai Srirampur | PyConCA 2018 Talk about my favorite things Sai Srirampur | PyConCA 2018
  • 5. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 PYTHON & DJANGO POSTGRES SCALING MULTI-TENANT APPLICATIONS
  • 6. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 Why 💙 Postgres? TLDR; Open source Constraints Extensions PostGIS / Geospatial HLL, TopN, Citus Foreign data wrappers Rich SQL CTEs Window functions Full text search Datatypes JSONB
  • 7. Sai Srirampur | PyConCA 2018 Today… Scaling Multi-Tenant Apps Using the Django ORM & Postgres 3 architectures to build Multi-tenant apps django_multitenant Scaling multi-tenant apps with distributed postgres
  • 8. Sai Srirampur | PyConCA 2018 • Multiple customers (“tenants”) • Each with own data • SaaS • Shopify, Salesforce Multi-Tenant Apps Sai Srirampur | PyConCA 2018
  • 9. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 Why talk about scalable multi-tenant applications? tens 100’s 1000’s
  • 10. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 Architectures To Build Multi-Tenant Applications3
  • 11. Sai Srirampur | PyConCA 2018 [1] One database per tenant Sai Srirampur | PyConCA 2018
  • 12. Sai Srirampur | PyConCA 2018 What defines a Database? • Organized collection of interrelated data • Don’t share resources. • Username and password • Connections • Memory
  • 13. Sai Srirampur | PyConCA 2018 One database per tenant Tenant 1251 Tenant 1252 Tenant 5 [1]
  • 14. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 CREATE DATABASE tenant_100; ./manage.py migrate --database=tenant_100; One database per tenant / Onboarding database_routing: db_for_read, db_for_write etc.
  • 15. Sai Srirampur | PyConCA 2018 ● Start quickly ● Isolate customer (tenant) data ● Compliance is a bit easier ● Time for DBA/developer to manage ● Maintain consistency (ex: create index across all databases) ● Longer running migrations ● Performance degrades as # customers (tenants) goes up PROS CONS [1] One Database Per Tenant Sai Srirampur | PyConCA 2018
  • 16. Sai Srirampur | PyConCA 2018 [2] One schema per tenant Sai Srirampur | PyConCA 2018
  • 17. Sai Srirampur | PyConCA 2018 What defines a database Schema? • Logical namespaces to hold a set of tables • Share resources: • Username and password • Connections • Memory
  • 18. Sai Srirampur | PyConCA 2018 One schema per tenant Tenant 5 Tenant 1251 Tenant 1252 Database [2]
  • 19. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 CREATE schema tenant_100; ./manage.py migrate --schema=tenant_100; app_code for schema routing based on tenant One schema per tenant / Onboarding
  • 20. Sai Srirampur | PyConCA 2018 ● Better resource utilization vs. one database per tenant ● Start quickly ● Logical isolation ● Hard to manage (ex: add column across all schemas) ● Longer running migrations ● Performance degrades as # customers (tenants) goes up PROS CONS [2] One Schema Per Tenant Sai Srirampur | PyConCA 2018
  • 21. Sai Srirampur | PyConCA 2018 [3] Shared table architecture Sai Srirampur | PyConCA 2018
  • 22. Sai Srirampur | PyConCA 2018 Accounts Shared table architecture Campaigns Leads TenantId 5 5 5 1251 Database [3]
  • 23. Sai Srirampur | PyConCA 2018 Example
  • 24. Sai Srirampur | PyConCA 2018 ● Easy maintenance ● Faster running migrations ● Best resource utilization ● Faster performance ● Scales to 1k-100k tenants ● Application code to guarantee isolation ● Make sure ORM calls are always scoped to a single tenant PROS CONS [3] Shared Table Architecture :-) Sai Srirampur | PyConCA 2018
  • 25. Sai Srirampur | PyConCA 2018 Comparing the 3 architectures for scaling multi-tenant Django applications ONE DATABASE PER TENANT ONE SCHEMA PER TENANT SHARED TABLE ARCHITECTURE Database Database [1] [2] [3] start quickly & isolation guarantees, bad resource utilization & not scalable better resource util, logical isolation not scalable Scales to over 100K tenants! explicitly handle isolation
  • 26. Sai Srirampur | PyConCA 2018
  • 27. Sai Srirampur | PyConCA 2018 django_multitenant to the rescue Sai Srirampur | PyConCA 2018
  • 28. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 django_multitenant Automates all ORM calls to be scoped to a single tenant
  • 29. Sai Srirampur | PyConCA 2018 Today... Purchase.objects.filter(id=1) <=> "SELECT* from purchase where id=1"
  • 30. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 With django_multitenant Purchase.objects.filter(id=1) <=> "SELECT* from purchase where id=1 and store_id=<current_tenant>"
  • 31. Sai Srirampur | PyConCA 2018 Components of django_multitenant Sai Srirampur | PyConCA 2018
  • 32. Sai Srirampur | PyConCA 2018 django_multitenant usage — 3 steps 1. Inherit all models with TenantModel 2. Change ForeignKey to TenantForeignKey 3. Define tenant scoping: set_current_tenant(t)
  • 33. Sai Srirampur | PyConCA 2018 TenantModel defines new Manager Sai Srirampur | PyConCA 2018
  • 34. Sai Srirampur | PyConCA 2018 TenantForeignKey mimics composite foreign key behavior • Adds tenant_id filter to referenced model. Handles: • Reference lookups — ex: (Purchase.product.name) • select_related() / prefetch_related() • Explicit Joins (product__name)
  • 35. Sai Srirampur | PyConCA 2018 set_current_tenant(t) • Specifies which tenant the APIs should be scoped to • Set at authentication logic via middleware • Set explicitly at top of function (ex. view, external tasks/jobs)
  • 36. Sai Srirampur | PyConCA 2018 Example Code Sai Srirampur | PyConCA 2018
  • 37. Sai Srirampur | PyConCA 2018 Models without django_multitenant class Purchase(models.Model): store = models.ForeignKey(Store) product_purchased = models.ForeignKey(Product) ordered_at = models.DateTimeField(default=timezone.now) billing_address = models.TextField() Sai Srirampur | PyConCA 2018
  • 38. Sai Srirampur | PyConCA 2018 Post django_multitenant class Purchase(TenantModel): store = models.ForeignKey(Store) product_purchased = TenantForeignKey(Product) ordered_at = models.DateTimeField(default=timezone.now) billing_address = models.TextField() Sai Srirampur | PyConCA 2018
  • 39. Sai Srirampur | PyConCA 2018 Setting the tenant at authentication class SetCurrentTenantFromUser(object): def process_request(self, request): if not hasattr(self, 'authenticator'): from rest_framework_jwt.authentication import JSONWebTokenAuthentication self.authenticator = JSONWebTokenAuthentication() try: user, _ = self.authenticator.authenticate(request) except: return try: #Assuming your app has a function to get the tenant associated for a user current_tenant = get_tenant_for_user(user) except: # TODO: handle failure return set_current_tenant(current_tenant) Sai Srirampur | PyConCA 2018
  • 40. Sai Srirampur | PyConCA 2018
  • 41. Sai Srirampur | PyConCA 2018 Benefits of django_multitenant Drop-in implementation of shared tables architecture Guarantees isolation Ready to scale with distributed Postgres (Citus)
  • 42. Sai Srirampur | PyConCA 2018
  • 43. Sai Srirampur | PyConCA 2018 Infinite scale with Citus
  • 44. Sai Srirampur | PyConCA 2018 django_multitenant with Postgres
  • 45. Sai Srirampur | PyConCA 2018 django_multitenant with Citus
  • 46. Sai Srirampur | PyConCA 2018Sai Srirampur | PyConCA 2018 django_multitenant with Citus
  • 47. Sai Srirampur | PyConCA 2018 also one of my favorites: Citus
  • 48. Scale out Django! github.com/citusdata/django-multitenant @saisrirampur @citusdata sai@citusdata.com citusdata.com/newsletter Thank You