SlideShare a Scribd company logo
1 of 40
Download to read offline
Building a Multi-tenant
Application with
Django.
Adedapo Adedamola
@dexbeulah_
Data Totems
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.
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
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:
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

What's hot

Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction Kanika Gera
 
CKA Certified Kubernetes Administrator Notes
CKA Certified Kubernetes Administrator Notes CKA Certified Kubernetes Administrator Notes
CKA Certified Kubernetes Administrator Notes Adnan Rashid
 
From Obvious to Ingenius: Incrementally Scaling Web Apps on PostgreSQL
From Obvious to Ingenius: Incrementally Scaling Web Apps on PostgreSQLFrom Obvious to Ingenius: Incrementally Scaling Web Apps on PostgreSQL
From Obvious to Ingenius: Incrementally Scaling Web Apps on PostgreSQLKonstantin Gredeskoul
 
Docker Container Security
Docker Container SecurityDocker Container Security
Docker Container SecuritySuraj Khetani
 
Docker - un outil pour faciliter le développement et le déploiement informatique
Docker - un outil pour faciliter le développement et le déploiement informatiqueDocker - un outil pour faciliter le développement et le déploiement informatique
Docker - un outil pour faciliter le développement et le déploiement informatiquesdenier
 
Midi technique - présentation docker
Midi technique - présentation dockerMidi technique - présentation docker
Midi technique - présentation dockerOlivier Eeckhoutte
 
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...Simplilearn
 
Deploying Azure DevOps using Terraform
Deploying Azure DevOps using TerraformDeploying Azure DevOps using Terraform
Deploying Azure DevOps using TerraformAdin Ermie
 
Introduction to Google Cloud Services / Platforms
Introduction to Google Cloud Services / PlatformsIntroduction to Google Cloud Services / Platforms
Introduction to Google Cloud Services / PlatformsNilanchal
 
Service Mesh - Why? How? What?
Service Mesh - Why? How? What?Service Mesh - Why? How? What?
Service Mesh - Why? How? What?Orkhan Gasimov
 
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQueryIntro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQueryChris Schalk
 
Microservice Architecture
Microservice ArchitectureMicroservice Architecture
Microservice Architecturetyrantbrian
 

What's hot (20)

Présentation Docker
Présentation DockerPrésentation Docker
Présentation Docker
 
Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction
 
CKA Certified Kubernetes Administrator Notes
CKA Certified Kubernetes Administrator Notes CKA Certified Kubernetes Administrator Notes
CKA Certified Kubernetes Administrator Notes
 
From Obvious to Ingenius: Incrementally Scaling Web Apps on PostgreSQL
From Obvious to Ingenius: Incrementally Scaling Web Apps on PostgreSQLFrom Obvious to Ingenius: Incrementally Scaling Web Apps on PostgreSQL
From Obvious to Ingenius: Incrementally Scaling Web Apps on PostgreSQL
 
Docker
DockerDocker
Docker
 
Docker Container Security
Docker Container SecurityDocker Container Security
Docker Container Security
 
Docker - un outil pour faciliter le développement et le déploiement informatique
Docker - un outil pour faciliter le développement et le déploiement informatiqueDocker - un outil pour faciliter le développement et le déploiement informatique
Docker - un outil pour faciliter le développement et le déploiement informatique
 
12-Factor Apps
12-Factor Apps12-Factor Apps
12-Factor Apps
 
Midi technique - présentation docker
Midi technique - présentation dockerMidi technique - présentation docker
Midi technique - présentation docker
 
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
 
Devops Mindset Essentials
Devops Mindset EssentialsDevops Mindset Essentials
Devops Mindset Essentials
 
Deploying Azure DevOps using Terraform
Deploying Azure DevOps using TerraformDeploying Azure DevOps using Terraform
Deploying Azure DevOps using Terraform
 
Introduction to Google Cloud Services / Platforms
Introduction to Google Cloud Services / PlatformsIntroduction to Google Cloud Services / Platforms
Introduction to Google Cloud Services / Platforms
 
Service Mesh - Why? How? What?
Service Mesh - Why? How? What?Service Mesh - Why? How? What?
Service Mesh - Why? How? What?
 
Multi-Tenancy
Multi-TenancyMulti-Tenancy
Multi-Tenancy
 
Docker Basics
Docker BasicsDocker Basics
Docker Basics
 
Docker on Docker
Docker on DockerDocker on Docker
Docker on Docker
 
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQueryIntro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
 
Node js Introduction
Node js IntroductionNode js Introduction
Node js Introduction
 
Microservice Architecture
Microservice ArchitectureMicroservice Architecture
Microservice Architecture
 

Similar to Building a Multi-tenant App with Django

Multi Tenancy In The Cloud
Multi Tenancy In The CloudMulti Tenancy In The Cloud
Multi Tenancy In The Cloudrohit_ainapure
 
Enterprise Use Case Webinar - PaaS Metering and Monitoring
Enterprise Use Case Webinar - PaaS Metering and Monitoring Enterprise Use Case Webinar - PaaS Metering and Monitoring
Enterprise Use Case Webinar - PaaS Metering and Monitoring WSO2
 
Cloudify workshop at CCCEU 2014
Cloudify workshop at CCCEU 2014 Cloudify workshop at CCCEU 2014
Cloudify workshop at CCCEU 2014 Uri Cohen
 
Charla Azure TLF.pptx
Charla Azure TLF.pptxCharla Azure TLF.pptx
Charla Azure TLF.pptxJuan Garcia
 
Paa sing a java ee 6 application kshitiz saxena
Paa sing a java ee 6 application   kshitiz saxenaPaa sing a java ee 6 application   kshitiz saxena
Paa sing a java ee 6 application kshitiz saxenaIndicThreads
 
Cloud-native Data: Every Microservice Needs a Cache
Cloud-native Data: Every Microservice Needs a CacheCloud-native Data: Every Microservice Needs a Cache
Cloud-native Data: Every Microservice Needs a Cachecornelia davis
 
Stay productive_while_slicing_up_the_monolith
Stay productive_while_slicing_up_the_monolithStay productive_while_slicing_up_the_monolith
Stay productive_while_slicing_up_the_monolithMarkus Eisele
 
Architectures, Frameworks and Infrastructure
Architectures, Frameworks and InfrastructureArchitectures, Frameworks and Infrastructure
Architectures, Frameworks and Infrastructureharendra_pathak
 
2015 zData Inc. - Apache Ambari Overview
2015 zData Inc. - Apache Ambari Overview2015 zData Inc. - Apache Ambari Overview
2015 zData Inc. - Apache Ambari OverviewzData Inc.
 
Developing saas application in azure
Developing saas application in azureDeveloping saas application in azure
Developing saas application in azureVinod Wilson
 
Cloud architecture
Cloud architectureCloud architecture
Cloud architectureAdeel Javaid
 
AWS Summit Stockholm 2014 – T5 – Deploy, manage and scale applications on AWS
AWS Summit Stockholm 2014 – T5 – Deploy, manage and scale applications on AWSAWS Summit Stockholm 2014 – T5 – Deploy, manage and scale applications on AWS
AWS Summit Stockholm 2014 – T5 – Deploy, manage and scale applications on AWSAmazon Web Services
 
Cloud patterns at Carleton University
Cloud patterns at Carleton UniversityCloud patterns at Carleton University
Cloud patterns at Carleton UniversityTaswar Bhatti
 
Services Saas,Pass,Iaas
Services Saas,Pass,IaasServices Saas,Pass,Iaas
Services Saas,Pass,IaasSofiya81
 
Azure from scratch part 3 By Girish Kalamati
Azure from scratch part 3 By Girish KalamatiAzure from scratch part 3 By Girish Kalamati
Azure from scratch part 3 By Girish KalamatiGirish Kalamati
 
CloudComputing
CloudComputingCloudComputing
CloudComputingAdi Challa
 
Cloud inventory analytics Maestro server
Cloud inventory analytics   Maestro serverCloud inventory analytics   Maestro server
Cloud inventory analytics Maestro serverFelipe Klerk Signorini
 

Similar to Building a Multi-tenant App with Django (20)

Multi Tenancy In The Cloud
Multi Tenancy In The CloudMulti Tenancy In The Cloud
Multi Tenancy In The Cloud
 
Enterprise Use Case Webinar - PaaS Metering and Monitoring
Enterprise Use Case Webinar - PaaS Metering and Monitoring Enterprise Use Case Webinar - PaaS Metering and Monitoring
Enterprise Use Case Webinar - PaaS Metering and Monitoring
 
Cloud presentation NELA
Cloud presentation NELACloud presentation NELA
Cloud presentation NELA
 
Cloudify workshop at CCCEU 2014
Cloudify workshop at CCCEU 2014 Cloudify workshop at CCCEU 2014
Cloudify workshop at CCCEU 2014
 
Charla Azure TLF.pptx
Charla Azure TLF.pptxCharla Azure TLF.pptx
Charla Azure TLF.pptx
 
Paa sing a java ee 6 application kshitiz saxena
Paa sing a java ee 6 application   kshitiz saxenaPaa sing a java ee 6 application   kshitiz saxena
Paa sing a java ee 6 application kshitiz saxena
 
Cloud-native Data: Every Microservice Needs a Cache
Cloud-native Data: Every Microservice Needs a CacheCloud-native Data: Every Microservice Needs a Cache
Cloud-native Data: Every Microservice Needs a Cache
 
Stay productive_while_slicing_up_the_monolith
Stay productive_while_slicing_up_the_monolithStay productive_while_slicing_up_the_monolith
Stay productive_while_slicing_up_the_monolith
 
Architectures, Frameworks and Infrastructure
Architectures, Frameworks and InfrastructureArchitectures, Frameworks and Infrastructure
Architectures, Frameworks and Infrastructure
 
2015 zData Inc. - Apache Ambari Overview
2015 zData Inc. - Apache Ambari Overview2015 zData Inc. - Apache Ambari Overview
2015 zData Inc. - Apache Ambari Overview
 
Developing saas application in azure
Developing saas application in azureDeveloping saas application in azure
Developing saas application in azure
 
Azure App Service Deep Dive
Azure App Service Deep DiveAzure App Service Deep Dive
Azure App Service Deep Dive
 
Cloud architecture
Cloud architectureCloud architecture
Cloud architecture
 
AWS Summit Stockholm 2014 – T5 – Deploy, manage and scale applications on AWS
AWS Summit Stockholm 2014 – T5 – Deploy, manage and scale applications on AWSAWS Summit Stockholm 2014 – T5 – Deploy, manage and scale applications on AWS
AWS Summit Stockholm 2014 – T5 – Deploy, manage and scale applications on AWS
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Cloud patterns at Carleton University
Cloud patterns at Carleton UniversityCloud patterns at Carleton University
Cloud patterns at Carleton University
 
Services Saas,Pass,Iaas
Services Saas,Pass,IaasServices Saas,Pass,Iaas
Services Saas,Pass,Iaas
 
Azure from scratch part 3 By Girish Kalamati
Azure from scratch part 3 By Girish KalamatiAzure from scratch part 3 By Girish Kalamati
Azure from scratch part 3 By Girish Kalamati
 
CloudComputing
CloudComputingCloudComputing
CloudComputing
 
Cloud inventory analytics Maestro server
Cloud inventory analytics   Maestro serverCloud inventory analytics   Maestro server
Cloud inventory analytics Maestro server
 

Recently uploaded

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

Building a Multi-tenant App with Django

  • 1. Building a Multi-tenant Application with Django. Adedapo Adedamola @dexbeulah_ Data Totems
  • 2.
  • 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.
  • 5.
  • 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
  • 9.
  • 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
  • 15.
  • 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