Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To Multi-Tenant, or Not To Multi-tenant

I have a difficult database design decision to make regarding multi-tenancy for the growing number of branches of my client's web-based CRM, which I actively maintain.

I made the decision early on to use separate applications with separate databases for each branch, because it was the simplest way to cater for three different branches with disparate data and code requirements. I also wanted to avoid managing Tenant IDs in every query, like I had to with the legacy Classic ASP (cringe) application I built in 2007...the horror.

But now the data requirements for branches are converging and as the business expands, I need to be able to roll out new branches quickly and share global product SKUs.

Since tables and views are the same for all branches and better ORM tools are now available to manage multi-tenant applications, I wonder if it would be better to have a shared database for multiple branches.

Considerations for a centralised database:

  • Global product SKUs
  • Simplified inventory requisitions
  • Easier to backup
  • Deploy once instead of for every branch

Considerations against a centralised database:

  • Easier to differentiate branch requirements with separate DBs
  • Modular deployment (one downed branch doesn't break all)
  • Harder to manage and develop for shared DB
  • I have to re-design invoice numbering (sequence generated by seed)
  • Less WHERE clauses everywhere
  • Restoring one broken branch has plenty of implications for other branches

It is unlikely there will be ever be as many as 10 branches. Right now there are 3.

Developers with real-world experience in this area, what would you do in my situation? Keep apps & DBs separate, or combine into one giant system?


Edit: Great Microsoft article on multi-tenancy pro's and cons. I should note that data isolation between branches is not a major issue.

like image 447
Petrus Theron Avatar asked Apr 08 '11 14:04

Petrus Theron


People also ask

Which is better single tenant or multi-tenant?

In comparison to single-tenancy, multi-tenancy is cheaper, has efficient resource usage, has a lower maintenance cost and a potentially larger computing capacity.

What are the benefits of multi-tenancy?

With multitenancy, MSSPs can manage and monitor multiple security systems for numerous customers all in one place. When MSSPs can centrally manage everything with multitenancy, they can increase scalability, reduce cost and improve security.

What are the advantages of single tenant hosting?

Potential benefits of single-tenant include: Security: A single customer and a single server is often contained on secure hardware being used by a limited number of people. Dependability: With an entire environment dedicated to one client, resources are abundant and available anytime.

Why would a customer want a single tenant environment?

Security. While no system is 100% secure, single tenant environments more readily allow for increased security as each customer's data is completely separate from any others. The chance that one customer will accidentally access another's data (as in a multi-tenant environment) is essentially eliminated.


4 Answers

Bite the bullet and merge them. Add your tenant ID where it needs to be, and change your queries.

For customizations, look into a plugin type architecture that would allow you to deploy specific screens for particular clients.

We have a software product that is built in just such a fashion. Sometimes it's deployed on a client site, sometimes we host it. For all intents and purposes it is an order of magnitude easier to deal with a single code base that has client specific extensions than dealing with multiple branches of the code.

For one, when we fix a problem, we fix it for everyone. Sure, if we break it, we break it for everyone but that's what unit tests are for. And it is a heck of a lot easier to maintain a set of unit tests against one code base than it is to maintain them for multiple branches.

We've been doing multi-tenant for over 10 years and not once have I looked back. Generally speaking, queries aren't that different if you are already security conscious in verifying that the person retrieving the record is actually allowed to get it.

I disagree with the issues brought up by Corbin. The one around versioning should already be handled by having an attribute based security structure in place. That way you can turn things on/off via user or tenant configuration. Also, I find it very rare that client A doesn't want the same new feature that client B asked for.

The second one about data mingling is also a non-issue. Just look at salesforce.com or any of the other large scale sites. They absolutely use a multi-tenant architecture and judging by the shear number of clients that use them this doesn't seem to be a problem. The main thing here is being able to ensure to your clients that their data is secured.

like image 163
NotMe Avatar answered Sep 22 '22 15:09

NotMe


If you're talking about 10 branches, multi-tenancy seems like a big cost with little benefit.

There are complications with multi-tenancy you don't mention:

  • Versioning becomes difficult. Clients X, Y, and Z may want a new feature while clients A, B, and C don't. A multi-tenant app makes accommodating everyone difficult, especially if a new feature requires database schema changes. It's not impossible, it's just more difficult.
  • Some clients are very uncomfortable with their data mingling in the same tables as other clients. Even though we know better, it feels like a security risk to them. Legal departments hate it. In addition, if you ever dump raw data for a client, a shared database requires caution.

You can eliminate a few of your pain points with better practices:

  • Automate deployment. This should make it easier to add a new client or upgrade/downgrade an existing client. Database maintenance (backups, rebuilding indexes) should be set up automatically as well.
  • Store shared data (SKUs, inventory) in a central database and have every application instance access it either directly or through a service.

Don't get me wrong, one of the more interesting apps I worked on was multi-tenant. There can be huge benefits, but you'll more likely see them with thousands of clients versus ten.

like image 36
Corbin March Avatar answered Sep 21 '22 15:09

Corbin March


Honestly, this is a business question. You are either going to be able to deliver more customized features to a smaller user group in a multi-tenant setup but with more IT overhead. That is, you will need more people and hardware (management reads this:money) but deliver greater flexibility.

If you are in a one GIANT Borg situation then you lower your IT overhead (again, people & things, to management money) but your end users have to absorb less flexibility in their software. All bugs are problems for all users, so big ones get whacked fast. However new features impact all users as well so they happen slower.

If you personally have the juice to make this call and the business just has to listen to what you say, or you can nudge management one way or another I'd suggest asking YOURSELF a series of questions about which scenario you prefer:

A) Do you want to have to have more people managing this and share salary/responsibilty B) To the best of your knowledge is there going to be a 4th user group soon? C) How long do you want to stay at this company?

If you answer yes to the first two, then you probably want multi-tenant.

like image 22
maczealot Avatar answered Sep 22 '22 15:09

maczealot


I work in a situation where, for regulatory/legal reasons, we have to keep each client's data in a separate database. However, there is certain information that must be shared, mostly related to things like a lookup table for which client's URL corresponds to which database. Also, a client can choose to have multiple databases if they wish to separate their data in some logical way. So, for each of our products, we really have three types of databases:

  1. ApplicationData, which has just a few tables that contain information about the clients themselves, like which MasterData database (see below) to use when reached by a certain URL and which features are available to that client. Each product has just one ApplicationData, no matter how many different clients are using that product.
  2. MasterData, which contains client-specific information such as users, roles, and permissions (in our case, the tables that aspnet_regsql creates are here). Among the permissions specified here are which ClientData databases are available to a given user (see below). The schema for all MasterData databases (for the same product) are the same.
  3. ClientData, which contains the data with which the user interacts. In one product, this is data that the client can search based on a large number of criteria, create reports about, etc. In another product, this contains the dynamic data that a client can upload so that other users can contact people to take surveys over the phone, etc. The schema for all ClientData databases for the same product is the same.

Now, one caveat: We actually use the same schema, and often the same actual database, for MasterData and ClientData. This is for historical reasons, as the ability to allow a client to have one authentication database (MasterData) corresponding to a number of ClientData databases is a relatively new feature that only applies to one of our products. Also, this structure simplifies deployment, since most clients only use one ClientData database. However, MasterData and ClientData have separate entity models under Entity Framework in our projects, and we have to ensure that there are no direct relationships between MasterData and ClientData such as foreign keys.

This setup works pretty well for us. One major advantage is that there is no problem with putting different ClientData databases on different servers. This helps greatly with load balancing, and it provides a natural way to partition data. We can essentially offer a client with a huge amount of data a dedicated database server if they are willing to pay for it.

One more thing that has really helped us in this situation are Red Gate's tools, specifically tools like Multi-Script, SQL Source Control, and Schema Compare. When we upgrade something, and the schema changes, we have to deploy the changes to all the relevant databases. These tools have more than paid for themselves in time saved. Note that I have no affiliation with Red Gate other than as a satisfied user.

Edit: (in response to comment)

ApplicationData is one database per product. The three web-based products we have use the same schema for ApplicationData, since they record basically the same types of information. However, there is no reason it would have to stay that way. The ApplicationData databases are all on the same server. One of the tables in ApplicationData points to the correct server and database name for the client's MasterData, so MasterData for a given client can reside on any server.

MasterData has server and database name information for each ClientData database, so again, the databases can reside on any server. In practice, for now, we only have two production database servers total for these products. The MasterData schema is similar per product, but I do not think they are exactly the same (I would have to check). Each client has its own MasterData. If a client purchases multiple products, there is a MasterData for each product for that client; the products interact in other ways (through web services, basically) if a client has purchased that feature (or requests custom development of such a feature. ClientData for a given product always has the same schema.

So, in summary:

  1. ApplicationData is per product and happens to have the same schema in each product.
  2. MasterData is per client for a product.
  3. There are one or more ClientData instances for a client within a product.

I did oversimplify slightly in that only one of our products supports multiple ClientData instances per client. For a second product, that will probably be implemented eventually. For a third product, it would make no sense at all as a feature and will likely just remain as is.

I hope that answers your question!

like image 27
Andrew Avatar answered Sep 20 '22 15:09

Andrew