SlideShare a Scribd company logo
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

Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
Zahra Heydari
 
JMS-Java Message Service
JMS-Java Message ServiceJMS-Java Message Service
JMS-Java Message Service
Kasun Madusanke
 
Domain Driven Design Introduction
Domain Driven Design IntroductionDomain Driven Design Introduction
Domain Driven Design Introduction
wojtek_s
 
Mobile Agent
Mobile AgentMobile Agent
Mobile Agent
Pavan Kumar
 
Dark Energy, Dark Matter and the Microservices Patterns?!
Dark Energy, Dark Matter and the Microservices Patterns?!Dark Energy, Dark Matter and the Microservices Patterns?!
Dark Energy, Dark Matter and the Microservices Patterns?!
Chris Richardson
 
Serverless Computing
Serverless Computing Serverless Computing
Serverless Computing
Rushi Namani
 
Introduction to CloudStack
Introduction to CloudStack Introduction to CloudStack
Design patterns in Magento
Design patterns in MagentoDesign patterns in Magento
Design patterns in Magento
Divante
 
Java RMI
Java RMIJava RMI
Java RMI
Prajakta Nimje
 
Microservices Architecture Part 2 Event Sourcing and Saga
Microservices Architecture Part 2 Event Sourcing and SagaMicroservices Architecture Part 2 Event Sourcing and Saga
Microservices Architecture Part 2 Event Sourcing and Saga
Araf Karsh Hamid
 
Mobile cloud computing
Mobile cloud computingMobile cloud computing
Mobile cloud computing
Fatih Özlü
 
Hexagonal architecture - message-oriented software design
Hexagonal architecture  - message-oriented software designHexagonal architecture  - message-oriented software design
Hexagonal architecture - message-oriented software design
Matthias Noback
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applications
hchen1
 
How to create a chat application on Android platform?
How to create a chat application on Android platform? How to create a chat application on Android platform?
How to create a chat application on Android platform?
baabtra.com - No. 1 supplier of quality freshers
 
Domain Driven Design 101
Domain Driven Design 101Domain Driven Design 101
Domain Driven Design 101
Richard Dingwall
 
Dependency injection presentation
Dependency injection presentationDependency injection presentation
Dependency injection presentation
Ahasanul Kalam Akib
 
Application Virtualization presentation
Application Virtualization presentationApplication Virtualization presentation
Application Virtualization presentation
ATWIINE Simon Alex
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introduction
ejlp12
 
Virtualization in cloud computing ppt
Virtualization in cloud computing pptVirtualization in cloud computing ppt
Virtualization in cloud computing ppt
Mehul Patel
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
Afaq Mansoor Khan
 

What's hot (20)

Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
JMS-Java Message Service
JMS-Java Message ServiceJMS-Java Message Service
JMS-Java Message Service
 
Domain Driven Design Introduction
Domain Driven Design IntroductionDomain Driven Design Introduction
Domain Driven Design Introduction
 
Mobile Agent
Mobile AgentMobile Agent
Mobile Agent
 
Dark Energy, Dark Matter and the Microservices Patterns?!
Dark Energy, Dark Matter and the Microservices Patterns?!Dark Energy, Dark Matter and the Microservices Patterns?!
Dark Energy, Dark Matter and the Microservices Patterns?!
 
Serverless Computing
Serverless Computing Serverless Computing
Serverless Computing
 
Introduction to CloudStack
Introduction to CloudStack Introduction to CloudStack
Introduction to CloudStack
 
Design patterns in Magento
Design patterns in MagentoDesign patterns in Magento
Design patterns in Magento
 
Java RMI
Java RMIJava RMI
Java RMI
 
Microservices Architecture Part 2 Event Sourcing and Saga
Microservices Architecture Part 2 Event Sourcing and SagaMicroservices Architecture Part 2 Event Sourcing and Saga
Microservices Architecture Part 2 Event Sourcing and Saga
 
Mobile cloud computing
Mobile cloud computingMobile cloud computing
Mobile cloud computing
 
Hexagonal architecture - message-oriented software design
Hexagonal architecture  - message-oriented software designHexagonal architecture  - message-oriented software design
Hexagonal architecture - message-oriented software design
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applications
 
How to create a chat application on Android platform?
How to create a chat application on Android platform? How to create a chat application on Android platform?
How to create a chat application on Android platform?
 
Domain Driven Design 101
Domain Driven Design 101Domain Driven Design 101
Domain Driven Design 101
 
Dependency injection presentation
Dependency injection presentationDependency injection presentation
Dependency injection presentation
 
Application Virtualization presentation
Application Virtualization presentationApplication Virtualization presentation
Application Virtualization presentation
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introduction
 
Virtualization in cloud computing ppt
Virtualization in cloud computing pptVirtualization in cloud computing ppt
Virtualization in cloud computing ppt
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
 

Similar to Building a multitenant application with Django

Multi Tenancy In The Cloud
Multi Tenancy In The CloudMulti Tenancy In The Cloud
Multi Tenancy In The Cloud
rohit_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.pptx
Juan 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 saxena
IndicThreads
 
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
cornelia 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_monolith
Markus 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 Overview
zData Inc.
 
Developing saas application in azure
Developing saas application in azureDeveloping saas application in azure
Developing saas application in azure
Vinod Wilson
 
Azure App Service Deep Dive
Azure App Service Deep DiveAzure App Service Deep Dive
Azure App Service Deep Dive
Azure Riyadh User Group
 
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 AWS
Amazon Web Services
 
Cloud computing
Cloud computingCloud computing
Cloud computing
Rohith Shankar
 
Cloud patterns at Carleton University
Cloud patterns at Carleton UniversityCloud patterns at Carleton University
Cloud patterns at Carleton University
Taswar Bhatti
 
Services Saas,Pass,Iaas
Services Saas,Pass,IaasServices Saas,Pass,Iaas
Services Saas,Pass,Iaas
Sofiya81
 
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
Girish Kalamati
 
CloudComputing
CloudComputingCloudComputing
CloudComputingAdi Challa
 
Cloud inventory analytics Maestro server
Cloud inventory analytics   Maestro serverCloud inventory analytics   Maestro server
Cloud inventory analytics Maestro server
Felipe Klerk Signorini
 

Similar to Building a multitenant application 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

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 

Recently uploaded (20)

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 

Building a multitenant application 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