What is Multitenancy?
Tags: architecture, webdev, cloud, programming
Published: 2023-07-31 06:37:40

I recently decided to start learning more about Software Architecture and one of the first keywords that instantly appeared in my search results was `Multitenancy`. I've heard this term few times already but I never really understood how it works under the hood.

So today, I will be diving into the multitenant architecture to see what are the benefits, when you should use it, and what are the issues you can come along the way.

It will be fun!

## What is Multitenancy?

First of all, the term `multitenancy` is used mainly in the area of `cloud computing`. What it means under the hood that multiple customers of a cloud provider are using exactly the same resources. And what is especially interesting here is that even though these cloud customers are sharing these computing resources, they are not aware of each other. And also, their data is kept separate from each other as well.

Multitenancy is a really important element of a cloud architecture. Cloud solutions would be much less practical and efficient without multitenancy. You can utilize this feature in several different types of public cloud like PaaS, SaaS, IaaS, containers, and serverless computing.

To better understand how multitenancy work, let's imagine how modern banking works under the hood. 

![Bank Multitenancy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhbk8gi45gkezi4ladv5.png)

Customers can store their money in a single bank, while each customer's money and other resources is separated so they are not aware of each other.

In public cloud the idea is very similar. Cloud customers are using the same infrastructure (sometimes the same servers) while keeping their data separate and secure.

## What is Software multitenancy?

There is also concept of Software multitenancy. In this architecture, single instance of software runs on a server and serves multiple tenants which means that the systems utilizing this approach are shared rather than isolated. A tenant is a group of users who share common access with specific privileges to the software instance. 

In a multitentant software architecture, system is designed to deliver its every tentant a dedicated amount of resources on the shared instance. These resources can be data, configuration, user management, tenant individual functionality, and non-functional properties.

## Pros of using Multitenancy

There are multiple advantages of using Multitenancy so I decided to only mention few of them that are the most significant for me:

* **More efficient usage of the resources** - One instance per each tenant wouldn't be efficient as that one tenant is not likely to use all of the instance computing power. By using multitenancy, the usage of available resources is maximized efficiently.
* **Lower costs** - Cloud provider can offer services to more customers at a lower price thanks to this multitenancy
* **Automatic updates** - Tenants that use a provider's software don't have to worry about updates, because they're pushed out by the host provider.
* **Scalability** - The architecture is easily scalable.

## Cons of using Multitenancy

Unfortunately, with the pros of multitenancy there are also some cons that you need to be aware of:

* **Security & compliance issues** - Some companies may not be able to store data within shared infrastructure, no matter how secure, due to regulatory requirements. Additionally, security problems or corrupted data from one tenant could spread to other tenants on the same machine.
* **"noisy neighbor" effect"** - When one tenant is using a huge amount of computing power, this could slow down the performance of the other tenants.
* **Complexity** - It is more complex than single-tenancy because of the additional virtualization and management needed to isolate and secure each tenant
* **Flexibility** - Apps delivered by a provider tend to be less flexible than apps in other tenant architectures, such as single-tenancy

## Multitenancy in Nest.js

There is a really good article about building a multitenant application in Nest.js that you can check out with the link below:

https://thomasvds.com/schema-based-multitenancy-with-nest-js-type-orm-and-postgres-sql/

And also, if you would like to see the repository immediately, you can visit the following link:

https://github.com/thomasvds/nestjs-multitenants

## Resources

Below, I am listing resources that I have used to write this article so that you can check them out for more details and examples

* https://www.redhat.com/en/topics/cloud-computing/what-is-multitenancy
* https://www.cloudflare.com/en-gb/learning/cloud/what-is-multitenancy/
* https://www.techtarget.com/whatis/definition/multi-tenancy#:~:text=Multi%2Dtenancy%20is%20an%20architecture,customer%20is%20called%20a%20tenant.