I've been brooding over the right/optimal way to create a multitenancy application based on Django.
Some explanation:
Application can be used by several tenants (tenant1, tenant2, ...,).
All tenant-individual data has to be secured against access of other tenants (and their users).
Optionally tenants can create additional custom-fields for application-objects.
Of course, underlying hardware limits number of tenants on one "system".
1) Separating each tenant by e.g. sub-domain and using tenant-specific databases in the underlying layer
2) Using some tenant-ID in the model to separate the tenant-data in the database
I am thinking about deployment-processes, performance of the system-parts (web-server(s), database-server(s), working-node(s),...)
What would be the best setup ? Where are the pro's and con's?
What do you think?
In a multi-tenant architecture, multiple instances of an application operate in a shared environment. This architecture is able to work because each tenant is integrated physically, but logically separated; meaning that a single instance of the software will run on one server and then serve multiple tenants.
A multi-tenant application (or multitenancy software) is an architecture in which a single instance serves multiples tenants. In other words, we host our application in a single place and all our customers (tenants) use the same resources.
In a single-tenant cloud, each customer lives alone in a single apartment building which has its own security system and facilities and is completely isolated from neighboring buildings. In multi-tenant cloud architecture, tenants live in different apartments inside a single apartment building.
Tenants can individually customize features of the application, such as: User Interface – Tenants can define a specialized “look and feel” for their application interface. Business Process – Tenants can customize the rules, logic, and workflows of the business processes that are implemented in the application.
We built a multitenancy platform using the following architecture. I hope you can find some useful hints.
(r'^(?P<tenant_id>[\w\-]+)
threading.local
)login_required
), middlewares or factories to protect views and select the right modelsRegarding the environment we use the following setup:
From my point of view this setup has the following pro's and con's:
Pro:
Contra:
Of course the best architecture strongly depends on your requirements as number of tenants, the delta of your models, security requirements and so on.
Update: As we reviewed our architecture, I suggest to not rewrite the URL as indicated in point 2-3. I think a better solutions is to put the tenant_id
as a Request Header and extract (point 4) the tenant_id
out of the request with something like request.META.get('TENANT_ID', None)
. This way you get neutral URLs and it's much easier to use Django built-in functions (e.g. {% url ...%}
or reverse()
) or external apps.
Here are some pointers to related discussions:
mezzanine.utils.sites.current_site_id
, mezzanine.core.models.SiteRelated
and mezzanine.core.request
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With