Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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


Published on

There are a number of data architectures you could use when building a multi-tenant app. Some, such as using one database per customer or one schema per customer. These two options scale to an extent when you have say 10s of tenants. However as you start scaling to hundreds and thousands of tenants, you start running into challenges both from performance and maintenance of tenants perspective.

You could solve the above problem by adding the notion of tenancy directly into the logic of your SaaS application. How to implement/automate this in Django-ORM is a challenge? We will talk about how to make the django-app tenant aware and at a broader level explain how scale out applications that are built on top of Django ORM and follow a multi tenant data model. We'd take postgres as our database of choice and the logic/implementation can be extended to any other relational databases as well.

Published in: Technology
  • Login to see the comments

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

  1. 1. Scaling Multi-tenant Applications Using Django And Postgres Sai Srirampur August 2018
  2. 2. About Me •Sai Srirampur a.k.a Sai •Early engineer @ Citus Data •Citus: Open source extension which horizontally scales Postgres •Fun fact: ping pong!
  3. 3. Multi-tenant Applications • Multiple customers each dealing with their own data • Most SaaS applications are multi-tenant • Ex: Shopify, Salesforce etc.
  4. 4. 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
  5. 5. 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!
  6. 6. django_multitenant to the rescue! “Automates all ORM calls to be scoped to a single tenant.”
  7. 7. 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” .
  8. 8. 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>”
  9. 9. Benefits of django_multitenant • Minimizes application changes • Guarantees isolation • Ready to scale with distributed Postgres (Citus)
  10. 10. Scale out Django! @citusdata © 2018 Citus Data. All right reserved.