SlideShare a Scribd company logo
Building a Multi-tenant
Application with
Django.
Adedapo Adedamola
@dexbeulah_
Data Totems
Building a multitenant application with Django
Glossary
• Database – collection of structured data to model reality in a way that support
processes requiring information.
• Schema - refers to the set of formulas or “rules” imposed on data in a database to be
organized into a certain way.
• App server – is a distributed network that provides the business logic for an
application.
What is Multi-tenancy?
• Multi tenant applications allow you to serve multiple customers with one install of the
application.
• It is the ability to run one instance of an app, and serve it to multiple distinct end user.
Each one of them is isolated from the other in terms of performance & data-leads.
Building a multitenant application with Django
Iaas
• Iaas – Insfrasture Application as Software.
• This is the tenancy model created by virtualization, and it is the ability to carve
large large server into multiple virtual machine.
App 1 App 2
App 4App 3
Host OS
Hypervisor
it - centric
Paas
• Paas – Platform as a Service.
• This is a better way of hosting a co-habiting Apps rather than dedicating a whole
OS instance to them.
OS OS OS OS
Component
Saas
• Saas – Software as a Service.
• It is the ability to run a single instance of an app that serves multiple distinct end
users. Each one of them is isolated from the other in terms of performance & data-
leads.
APP 1
T 1 T 2 T 3
User
User
User
User
User
User
User
User
User
Building a multitenant application with Django
Saas
• Benefits:
• Reduce infrastructure costs by sharing hardware resources.
• Simplify software maintenance by keeping a single code base.
• Simplify infrastructure maintenance by having fewer nodes.
App
management
is once.
Monitoring is
once.
QA is once.
Multi-tenancy
• Approaches:
• Shared database with shared schema
• Shared database with isolated schema
• Isolated database with a shared app server
Argh!
Django again…
Crash course on Django:
Shared database with shared schema
Single-tenant app:
Building a multitenant application with Django
Adding multi tenancy:
• python manage.py startapp tenants
• Create a model for storing Tenant data.
• TenantAwareModel class will be subclass from.
Adding multi tenancy:
• Poll.model subclassing TenantAwareModel
Identifying tenants:
• Giving each tenant their own subdomain.
• abc.example.com
• xyz.example.com
• This will be will have a subdomain_prefix which will identify the tenant.
Extracting tenant from request:
• Utils.py
A detour to /etc/hosts:
• C:WindowsSystemsDriversetchosts
tenant_from_request in the views:
• Function based || Class based || Django rest framework
Isolating the admin:
• Get_queryset: so that only the current tenant’s objects show up.
• Save_model: so that tenant gets set on the object when the object is saved.
Limitation:
• Weak separation of tenant’s data.
• Tenant isolation code is intermixed with app code.
• Duplication of code.
QUESTION(S)?
Shared database with isolated schema
What to do?
• Map tenant to schema
• Manage database migrations
• Tenant separation in views
• A middleware to set schemas
• Add get and set methods to utils.py
• Mapping tenant to schema
Mapping tenants to schemas for easy routing
• Manage database migrations
• Tenant separation in views
• A middleware to set schemas
• Add get and set methods to utils.py
CREATE SCHEMA IF NOT EXISTS potter
SET search_path to potter
We make manage.py schema aware, therefore, it create schema according to
the tenant in case it never existed, and set search_path tenant accordingly.
• Tenant separation in views
• A middleware to set schemas
• Add get and set methods to utils.py
The following methods allow us to get and set the schema.
• Tenant separation in views
• A middleware to set schemas
Separating the tenants in the view.
• Tenant separation in views
• A middleware to set schemas
Separating the tenants in the view.
• A middleware to set schemas
Make admin.py aware of the shared schema
Separating the admin page for tenant.
Isolated database with shared app server
Multiple database support in Django:
To read poll from thor: Poll.objects.using(‘thor’).all()
But we’d rather avoid code duplication, therefore use db routing
DB_ROUTERS settings take a list of classes which implement a few methods. A router class looks like this.
DB routing:
But none takes an argument, therefore, we cannot call tenant_db_from_request
We use threadlock variables to calculate DB to use, pass it to the router
TO pass
Middleware class:
DEMO
https://youtu.be/NLhUnzvvm1s
Content credit:
Django Multi Tenant Application documentation.
https://books.giliq.com/projects/Django-multi-tenant/en/latest
Demo:
https://github.com/agiliq/building-multi-tenant-applications-with-django/tree/master/isolated-db
Memes credit:
Ajakaye Paul
E-mail:
dexbeulah@gmail.com
THANK YOU

More Related Content

PPTX
Azure Migration Program Pitch Deck
PDF
Docker & kubernetes
PPTX
Innovation anywhere with microsoft azure arc
PDF
Azure Arc by K.Narisorn // Azure Multi-Cloud
PDF
Dockers and kubernetes
PDF
금융 X 하이브리드 클라우드 플랫폼 - 한화생명 디지털 트랜스포메이션 전략 - 김나영 AWS 금융부문 사업개발 담당 / 박인규 AWS 금융...
PDF
클라우드 마이그레이션 성공적인 여정, 그 중요한 시작 "Readiness Assessment (전환 준비 평가)" - 김준범, AWS Mi...
PDF
Introduction to Google Cloud Platform and APIs
Azure Migration Program Pitch Deck
Docker & kubernetes
Innovation anywhere with microsoft azure arc
Azure Arc by K.Narisorn // Azure Multi-Cloud
Dockers and kubernetes
금융 X 하이브리드 클라우드 플랫폼 - 한화생명 디지털 트랜스포메이션 전략 - 김나영 AWS 금융부문 사업개발 담당 / 박인규 AWS 금융...
클라우드 마이그레이션 성공적인 여정, 그 중요한 시작 "Readiness Assessment (전환 준비 평가)" - 김준범, AWS Mi...
Introduction to Google Cloud Platform and APIs

What's hot (20)

PDF
Async Messaging in CQRS: Part 1 - Masstransit + DDD Intro
PDF
Deploy 22 microservices from scratch in 30 mins with GitOps
PPTX
Introduction to Docker - 2017
DOC
Cloud computing report
PPTX
Aws multi-region High Availability
PPTX
Communication in a Microservice Architecture
PPTX
Windows Azure Service Bus
PPTX
Docker: From Zero to Hero
PPSX
Big Data Redis Mongodb Dynamodb Sharding
PDF
Cloud Native Bern 05.2023 — Zero Trust Visibility
PDF
Introduction to Kubernetes and GKE
PPTX
Domain Driven Design
PDF
Azure Arc Overview from Microsoft
PPTX
AWS Cloud Security
PPTX
Introduction to microservices
PPTX
Architect your app modernization journey with containers on Microsoft Azure
PPSX
CI-CD Jenkins, GitHub Actions, Tekton
PDF
Elastic Observability
PDF
Istio on Kubernetes
PDF
Microservices, Kubernetes and Istio - A Great Fit!
Async Messaging in CQRS: Part 1 - Masstransit + DDD Intro
Deploy 22 microservices from scratch in 30 mins with GitOps
Introduction to Docker - 2017
Cloud computing report
Aws multi-region High Availability
Communication in a Microservice Architecture
Windows Azure Service Bus
Docker: From Zero to Hero
Big Data Redis Mongodb Dynamodb Sharding
Cloud Native Bern 05.2023 — Zero Trust Visibility
Introduction to Kubernetes and GKE
Domain Driven Design
Azure Arc Overview from Microsoft
AWS Cloud Security
Introduction to microservices
Architect your app modernization journey with containers on Microsoft Azure
CI-CD Jenkins, GitHub Actions, Tekton
Elastic Observability
Istio on Kubernetes
Microservices, Kubernetes and Istio - A Great Fit!
Ad

Similar to Building a multitenant application with Django (20)

PPT
Multi Tenancy With Python and Django
PDF
Multitenant applications: How and Why
PDF
Scaling Multi-tenant Applications Using the Django ORM & Postgres | PyCon Can...
PDF
Scaling Multi-Tenant Applications Using the Django ORM & Postgres | PyCaribbe...
PPTX
Software as a service
PPTX
Multi Tenancy In The Cloud
PPTX
Multi tenancy with mvc3 and entity framework
PDF
Scaling Multi-Tenant Applications Using Django and Postgres | PyBay 2018 | Sa...
PDF
Defining multitenancy
PPTX
What is Multi-Tenant Architecture ?
PDF
Multitenancy in cloud computing architecture
PPTX
SaaSy maps - using django-tenants and geodjango to provide web-gis software-a...
PPTX
“Salesforce Multi-tenant architecture”,
PPTX
Saa s multitenant database architecture
PDF
Schema-based multi-tenant architecture using Quarkus & Hibernate-ORM.pdf
PPTX
Multi_Tenancy_White_Paper_0829_pptx.pptx
PDF
Ijcet 06 06_007
PDF
Cloud application services (saa s) – multi tenant data architecture
PDF
PDF
A systematic review of in-memory database over multi-tenancy
Multi Tenancy With Python and Django
Multitenant applications: How and Why
Scaling Multi-tenant Applications Using the Django ORM & Postgres | PyCon Can...
Scaling Multi-Tenant Applications Using the Django ORM & Postgres | PyCaribbe...
Software as a service
Multi Tenancy In The Cloud
Multi tenancy with mvc3 and entity framework
Scaling Multi-Tenant Applications Using Django and Postgres | PyBay 2018 | Sa...
Defining multitenancy
What is Multi-Tenant Architecture ?
Multitenancy in cloud computing architecture
SaaSy maps - using django-tenants and geodjango to provide web-gis software-a...
“Salesforce Multi-tenant architecture”,
Saa s multitenant database architecture
Schema-based multi-tenant architecture using Quarkus & Hibernate-ORM.pdf
Multi_Tenancy_White_Paper_0829_pptx.pptx
Ijcet 06 06_007
Cloud application services (saa s) – multi tenant data architecture
A systematic review of in-memory database over multi-tenancy
Ad

Recently uploaded (20)

PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
System and Network Administration Chapter 2
PPTX
ai tools demonstartion for schools and inter college
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Online Work Permit System for Fast Permit Processing
PDF
medical staffing services at VALiNTRY
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Transform Your Business with a Software ERP System
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
System and Network Administration Chapter 2
ai tools demonstartion for schools and inter college
Design an Analysis of Algorithms II-SECS-1021-03
Operating system designcfffgfgggggggvggggggggg
Online Work Permit System for Fast Permit Processing
medical staffing services at VALiNTRY
PTS Company Brochure 2025 (1).pdf.......
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Odoo Companies in India – Driving Business Transformation.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo POS Development Services by CandidRoot Solutions
history of c programming in notes for students .pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Softaken Excel to vCard Converter Software.pdf
Understanding Forklifts - TECH EHS Solution
Transform Your Business with a Software ERP System
ISO 45001 Occupational Health and Safety Management System
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx

Building a multitenant application with Django

  • 1. Building a Multi-tenant Application with Django. Adedapo Adedamola @dexbeulah_ Data Totems
  • 3. Glossary • Database – collection of structured data to model reality in a way that support processes requiring information. • Schema - refers to the set of formulas or “rules” imposed on data in a database to be organized into a certain way. • App server – is a distributed network that provides the business logic for an application.
  • 4. What is Multi-tenancy? • Multi tenant applications allow you to serve multiple customers with one install of the application. • It is the ability to run one instance of an app, and serve it to multiple distinct end user. Each one of them is isolated from the other in terms of performance & data-leads.
  • 6. Iaas • Iaas – Insfrasture Application as Software. • This is the tenancy model created by virtualization, and it is the ability to carve large large server into multiple virtual machine. App 1 App 2 App 4App 3 Host OS Hypervisor it - centric
  • 7. Paas • Paas – Platform as a Service. • This is a better way of hosting a co-habiting Apps rather than dedicating a whole OS instance to them. OS OS OS OS Component
  • 8. Saas • Saas – Software as a Service. • It is the ability to run a single instance of an app that serves multiple distinct end users. Each one of them is isolated from the other in terms of performance & data- leads. APP 1 T 1 T 2 T 3 User User User User User User User User User
  • 10. Saas • Benefits: • Reduce infrastructure costs by sharing hardware resources. • Simplify software maintenance by keeping a single code base. • Simplify infrastructure maintenance by having fewer nodes. App management is once. Monitoring is once. QA is once.
  • 11. Multi-tenancy • Approaches: • Shared database with shared schema • Shared database with isolated schema • Isolated database with a shared app server Argh! Django again…
  • 12. Crash course on Django:
  • 13. Shared database with shared schema
  • 16. Adding multi tenancy: • python manage.py startapp tenants • Create a model for storing Tenant data. • TenantAwareModel class will be subclass from.
  • 17. Adding multi tenancy: • Poll.model subclassing TenantAwareModel
  • 18. Identifying tenants: • Giving each tenant their own subdomain. • abc.example.com • xyz.example.com • This will be will have a subdomain_prefix which will identify the tenant.
  • 19. Extracting tenant from request: • Utils.py
  • 20. A detour to /etc/hosts: • C:WindowsSystemsDriversetchosts
  • 21. tenant_from_request in the views: • Function based || Class based || Django rest framework
  • 22. Isolating the admin: • Get_queryset: so that only the current tenant’s objects show up. • Save_model: so that tenant gets set on the object when the object is saved.
  • 23. Limitation: • Weak separation of tenant’s data. • Tenant isolation code is intermixed with app code. • Duplication of code.
  • 25. Shared database with isolated schema
  • 26. What to do? • Map tenant to schema • Manage database migrations • Tenant separation in views • A middleware to set schemas • Add get and set methods to utils.py
  • 27. • Mapping tenant to schema Mapping tenants to schemas for easy routing
  • 28. • Manage database migrations • Tenant separation in views • A middleware to set schemas • Add get and set methods to utils.py CREATE SCHEMA IF NOT EXISTS potter SET search_path to potter We make manage.py schema aware, therefore, it create schema according to the tenant in case it never existed, and set search_path tenant accordingly.
  • 29. • Tenant separation in views • A middleware to set schemas • Add get and set methods to utils.py The following methods allow us to get and set the schema.
  • 30. • Tenant separation in views • A middleware to set schemas Separating the tenants in the view.
  • 31. • Tenant separation in views • A middleware to set schemas Separating the tenants in the view.
  • 32. • A middleware to set schemas
  • 33. Make admin.py aware of the shared schema Separating the admin page for tenant.
  • 34. Isolated database with shared app server
  • 35. Multiple database support in Django: To read poll from thor: Poll.objects.using(‘thor’).all() But we’d rather avoid code duplication, therefore use db routing
  • 36. DB_ROUTERS settings take a list of classes which implement a few methods. A router class looks like this. DB routing: But none takes an argument, therefore, we cannot call tenant_db_from_request We use threadlock variables to calculate DB to use, pass it to the router
  • 39. Content credit: Django Multi Tenant Application documentation. https://books.giliq.com/projects/Django-multi-tenant/en/latest Demo: https://github.com/agiliq/building-multi-tenant-applications-with-django/tree/master/isolated-db Memes credit: Ajakaye Paul E-mail: dexbeulah@gmail.com