55

Questions

  1. Why do some multi-tenant web applications use subdomains to designate the tenant while others do not?
  2. Are there technical, privacy, or security reasons?
  3. Is it dependent on the language or framework used to develop the web application?
  4. Is it simply a matter of style or developer's choice?

Example Web Applications Using Subdomains to Designate Tenants

Example Web Applications Not Using Subdomains to Designate Tenants

Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
  • 5
    As a recommendation, I would say design you app from the outset not to use subdomains, and then build this functionality in as a final layer. If you integrate subdomains all the way through, it becomes very inflexible to change it in future. (source: experience) – cjm2671 Jan 30 '14 at 11:18
  • 3
    Hello, If you can avoid using sub-domains, do it! We are forced to use custom domains (not just sub-domains) for our app because our product is a complete white-labeled solution and it often requires us to have our clients setup the custom domains on their hosts file and point to our system and also include the same as a setting in the system. Needless to say, it is a long drawn out process as many of our clients are also resellers and need to do the same. I really think that we can't get away from this, but if you can, try and avoid :) – Anup Marwadi Aug 21 '14 at 03:10

2 Answers2

40

There are several ways to determine tenant on HTTP level:

  • domain - tenant is determined by whole Host header
  • sub-domain - sub-domain part of Host header,
  • path based - path segment, usually by prefix host.com/tenantId/...
  • cookie based - cookie value contains tenant id (good framework encrypts this!)
  • user based - user session or some data records on server

Here are an answers to your questions:

  1. (Sub-)domain multi-tenancy is good if you want to give an user a perception of fully isolated tenancy. The customer may want custom welcome and login page, separate user-base etc. On the other hand the path based multi-tenancy is good for the users who are not fixed to single tenant namespace. It is mostly used by social networks like Facebook, GitHub etc.

  2. (Sub-)domains can give you better isolation and security control for cookies, cross-origin resources sharing (CORS). It makes cross-tenant CSRF or XSS a bit harder. Moreover if you have control over DNS or Load-balancer you can assign tenants to different IPs (think geo-routing) or to various versions of application (e.g. beta tenants). You can assign a separate app instance or server for your most important tenants. This way you get a cheap tool to control risk of single point of failure and all eggs in one basket.

  3. Any web-framework which gives you an access to HTTP headers (Host) is sub-domains capable. Any serious MVC web-framework should give you sub-domain as action parameter directly or by plugin.

  4. It is definitely a design choice. If you want to know the best way think what level of isolation you want for your tenants. If you decide but you will find that the way is not right then you can migrate to another level with help of HTTP 301 redirection.

gertas
  • 16,869
  • 1
  • 76
  • 58
  • Hi @gertas, I have an app where users create their own website and I want to allow them to edit the html and css of the template they choose without affecting the security of my app, do you think Multi-tenancy is a good solution ? thanks! – medBouzid Mar 02 '15 at 13:13
  • You must, any user own space = multi-tenancy. I would go with sub-domain or domain level with proper CORS headers to prevent XSS. However if you have very good html/css sanitizer or you do moderation then you can try path based multi-tenancy, see ebay or old myspace. But at least put admin module on separate domain and don't directly render these htmls there - in admin try iframes. – gertas Mar 03 '15 at 11:03
  • I have done some research and i found that there are (separate databases), (shared database, separate schema), (shared database,shared schema), as I am using Rails framework there is a gem called "apartment" which use the (shared database, separate schema) approach, but it seems that hosting like heroku says "we strongly recommend against it as it has caused numerous cases of operational problems...) they say also "even >50 can severely impact the performance..), please have you any experience within this subject ? can "subdomain" only solve the problem? – medBouzid Mar 04 '15 at 14:19
  • 3
    You mentioned different layer of multi-tenancy isolation - DB. I use single DB schema with tenant_id column in each table. – gertas Mar 05 '15 at 09:07
  • I am also looking for similar concept to implement in one of the project , where i need to decide for use Single Domain V/S Multi Sub Domain , for single code base , Multiple DB for each account. Planning to use Load Balancer , what is best , Single Domain - Multi Tenant Or Multi Sub Domain - Multi Tenant? – Pragnesh Karia Aug 10 '16 at 10:42
3
  1. See below.
  2. Cookies would be the most obvious, with the second being that you can change DNS settings for a subdomain but not for a path
  3. No
  4. Partially, see above.
Noon Silk
  • 54,084
  • 6
  • 88
  • 105