Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Citus Data
August 17, 2018

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

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.

Citus Data

August 17, 2018
Tweet

More Decks by Citus Data

Other Decks in Technology

Transcript

  1. About Me •Sai Srirampur a.k.a Sai •Early engineer @ Citus

    Data •Citus: Open source extension which horizontally scales Postgres •Fun fact: ping pong!
  2. Multi-tenant Applications • Multiple customers each dealing with their own

    data • Most SaaS applications are multi-tenant • Ex: Shopify, Salesforce etc.
  3. 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
  4. 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!
  5. 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>”