Scaling Multi-tenant Applications Using
Django And Postgres
Sai Srirampur
August 2018
About Me
•Sai Srirampur a.k.a Sai
•Early engineer @ Citus Data
•Citus: Open source extension which horizontally scales Postgres
•Fun fact: ping pong!
Multi-tenant Applications
• Multiple customers each dealing with their own data
• Most SaaS applications are multi-tenant
• Ex: Shopify, Salesforce etc.
Starting out: 1 tenant per database/schema
• Scales up to 10s to 100s of tenants
• Guarantees better isolation
• At scales of 1k to 100k tenants:
•Hard to manage - ex. consistent schema-changes
•Long time to run migration
•Performance issues
1000+ tenants: Shared Tables
•Scales to 100,000+ tenants
•Faster running migrations
•Better performance
•But
Make sure ORM calls are always scoped to a single tenant!
django_multitenant to the rescue!
“Automates all ORM calls to be scoped to a single tenant.”
Today...
class Purchase(Model):
store = models.ForeignKey(Store)
tenant_id='store_id'
product_purchased = ForeignKey(Product)
Purchase.objects.filter(id=1)
<=>
“SELECT* from purchase where id=1”
.
With django_multitenant
class Purchase(TenantModel):
store = models.ForeignKey(Store)
tenant_id='store_id'
product_purchased = TenantForeignKey(Product)
Purchase.objects.filter(id=1)
<=>
“SELECT* from purchase where id=1 and
store_id=<current_tenant>”
Benefits of django_multitenant
• Minimizes application changes
• Guarantees isolation
• Ready to scale with distributed Postgres (Citus)
Scale out Django!
github.com/citusdata/django-multitenant
www.citusdata.com @citusdata
© 2018 Citus Data. All right reserved.
sai@citusdata.com

Scaling Multi-Tenant Applications Using Django and Postgres | PyBay 2018 | Sai Srirampur