12
3 Comments

What is multi-tenancy & why you might need it

Hey all,

I thought I'd write about a topic that I feel many people need, but don't know about.

And that is multi-tenancy.

What is multi-tenancy?

Multi-tenancy is the ability to provide your application to multiple customers, with separate data, without having to host an instance for each customer.

When do you need it?

Say you have built an e-commerce platform. Your database has tables for users, orders, products, ordered products, product variants, invoices etc.

You want to let multiple businesses use this platform. You want them to have separate orders, products and all the other things.

You can do two things.

  1. Host the application separately for each customer. This would be called multi-instance. It's fine when you have only a few instances, but once you grow, keeping multiple instances up-to-date becomes a pain.
  2. Use multi-tenancy in your code. This will detect the tenant based on e.g. the domain and show the correct data.

Single vs. multi-database tenancy

Multi-tenancy comes in two flavors.

  1. Single-database tenancy - your tables have a tenant_id column and you use WHERE clauses to scope the data to the current tenant.
  2. Multi-database tenancy - each tenant has his own database with these tables.

Single-database tenancy comes with more code complexity. You have to keep the scoping in mind at all times. Some backend frameworks, like Laravel, let you add "global scopes" that automatically apply these WHERE clauses to your queries, but there are still edge cases when you need to be mindful of the data scoping.

Multi-database tenancy comes with more devops complexity. If you implement tenancy right, you don't need to think about the data scoping at all - you can just switch the database connection before your application's code (controllers in MVC frameworks) is reached. However, you will have to maintain multiple databases - such as running migrations for each one separately, which means slower deployments.

But, one devops upside of multi-database tenancy is that you likely won't EVER have to think about database sharding. Your data is already sharded in a way. Single-DB tenancy can result in one massive database that would need sharding.

Which flavor should you use?

There's a lot of misconception about the multi-database approach. People often that it's somehow very complex. They often say "for most apps, it's not needed". But that assumes that it's somehow more work to use multi-database tenancy.

Personally, I'm a fan of it because it lets me write my code as if tenancy weren't a thing and then just have logic that switches the database to the correct tenant based on hostname, before my application code is reached.

But multi-database tenancy comes with one big disadvantage: sharing data between tenants can be a pain. There are three scenarios here:

  1. You just need the data. In this case, you can use database queries that explicitly use the central database connection, even if you're in the tenant "context".
  2. You need database "relationships" (when using ORMs) between the shared data and the tenant data. Therefore, you need to sync the shared data between tenant databases.
  3. Cross-database queries, which you can do in MySQL and PostgreSQL. It won't work once you need more than a single database server. So I'd consider that a bad practice.

So my recommendation is that if you want a comfortable coding experience and don't need a lot of shared data between tenants, use multi-database tenancy. If you just need to scope a part of the database to a tenant, but otherwise have a lot of "global" data, use single-database tenancy.

Laravel

If you're using Laravel, you can look into my package: https://tenancyforlaravel.com/

It's focused on providing as much out-of-the-box functionality as possible and making you ship very quickly.

That's the reason I'm writing this article. Maintaining a multi-tenancy package for a year and a half, I have a lot of experience with this and wanted to share this concept with the people who aren't that familiar with it.

  1. 1

    This is a great write up! I'm currently learning multi-tenancy and how I'll implement it for my own Node.js and MongoDB project. It seems that there is much less support for that framework in multi-tenancy vs. Rails/php and SQL.

  2. 1

    Good write up!

    Another note is that single-database multi-tenancy is the only option you have if you have a lot of users with not a lot of data (e.g. something more consumer focused). An advantage is also that analytics via SQL is easier when it's all one db instance.

  3. 1

    Nice write-up! And cool package, even though I'm not a PHP dev :)

    I did some consulting after I left my 9-5 job, and had to hack together some separate instances. Can confirm it was a nightmare (esp since we did custom branding in HTML templates).

    I've also done tenancy after an app was built, and those edge cases always crop up and bite you!

Trending on Indie Hackers
Feedback on my (not yet published) about page 23 comments Open Sourcing my SAAS Starter Kit 9 comments A house in Germany is being sold as an NFT 9 comments Vegans, vegetarians, and anyone with an allergy, food intolerance, or just a preference, I need you! 8 comments Nerdogram - A photo sharing app for Github nerds 5 comments Free Python Books Went Viral on Hacker News 5 comments