Understanding Multitenancy Isolation Models in Plain English

Discuss popular isolation models for multitenant applications using Azure Cosmos DB or similar NoSQL databases.

Shawn Shi
Geek Culture
5 min readFeb 27, 2023

--

Background

Multitenancy refers to a software architecture pattern, which allows multiple customers (i.e., tenants) to share the same software application as well as the database. Multitenancy is a very popular approach in building software as a service (SaaS) applications as it offers significant benefits such as cost savings, scalability improvement, and ease of management.

Of course, there are challenges coming with building multitenant applications. One critical challenge is to ensure tenant data isolation. Data isolation means ensuring that each tenant’s data is stored separately and cannot be accessed by other tenants. It would look really bad if one customer sees another customer’s data. Purely, awful… Microsoft has a detailed documentation, Multitenancy and Azure Cosmos DB, covering how Azure Cosmos DB can support different isolation models in full technical terms.

Goal

The goal of this article is to discuss different data isolation models that can be used by multitenant applications, in plain English. We will discuss:

  • Partition key per tenant
  • Container per tenant
  • Database per tenant

Getting Started

Picture you own a startup and you are building a SaaS product. You anticipate this product will receive lots of attention from the businesses and consumers, so you want to make sure the SaaS product is ready to scale when it needs to. Now you are asking yourself questions like:

  • how can I quickly onboard a new tenant with minimal human work and minimal additional cost?
  • how can I ensure one tenant can only access their own data? Any data breach probably will kill the product…

Boring….

Ignore the technical terms above. Let’s try again…

You are a farm land owner. You want to let other people to pay a small fee to use your farm land. As people are anxiously waiting at the gate, you want to decide how they should use the land!

Isolation models for your SaaS product. Image by author. Icons from flaticon.com

Option 1 — Partition key per tenant

You still have your single farm land, i.e., a database. In order to achieve this:

  1. You manually draw a section on your land, name it “Container 1”. Note it is called container 1 because it is not reserved for any customer. We only have one container for data modeling simplicity. In fact, having one container to host all data for all customers is a pretty good idea and it simplifies your data modeling complexity!
  2. Inside container one, there are numerous sub areas (i.e. partitions) labeled by letters. You decide a very IMPORTANT logic that will assign Adam to partition A, Bob to partition B, Cathy to partition C, and Daniel to partition D. Every time a new customer comes, they are assigned to the partition starting with their first name initial.
Isolation models for your SaaS product. Image by author. Icons from flaticon.com

Discussion points:

  • You might ask, what if Colin comes later but Cathy is already planting her carrots partition C? We can not just put Colin in partition C since that breaks our option “partition per tenant”! Well, you are right! In real world, each customer should have a unique id, like a GUID. Instead of assigning Cathy to partition C, we assign Cathy to partition GUID1, which should never be the same as Colin’s id… As mentioned, we have numerous partitions in container one, so we can accommodate many many many new customers.
  • Note every time a new customer joins, you, as a owner, have to do almost nothing.
  • What is the additional cost of adding a new customer? Well, almost nothing too because they just squeeze into a new partition in the same container.
  • What is the very IMPORTANT logic mentioned above? Well, this is the partition key! For example, you may decide the container will use tenant id as a partition key.

Option 2 — Container per tenant

You still have your single farm land, i.e., a database. In order to achieve this:

  1. You manually draw a section on your land, name it “Container Adam”. Note it is called container Adam because it is reserved Adam. Repeat the same process for Bob, Cathy, Daniel, and any new customer.
Isolation models for your SaaS product. Image by author. Icons from flaticon.com

Discussion points:

  • Note now the customers are slightly more segregated from each other. They each have a little bit more space in between them, so when Bob sings terrible songs, Adam is not annoyed.
  • Note every time a new customer joins, you, as a owner, have to create a new section / container. Even if you can automate it or hire someone to do it, it still takes a little bit time to draw it. Call it “new tenant creation latency”!
  • What is the additional cost of adding a new customer? Well, there is cost associated with your labor and also the drawing materials. So, definitely more than option 1.

Option 3 — Database per tenant

Well, you still NO LONGER only have your single farm land, i.e., a database. In order to achieve this option:

  1. You allow Adam to use your farm land, but for any additional customer, you have to buy a new piece of farm land and let them use the new land!
Isolation models for your SaaS product. Image by author. Icons from flaticon.com

Discussion points:

  • Your customers are further apart. In fact, they might even not be on the same continent…
  • Assuming you can easily buy your neighbor's land, but you have to be really rich or your customers are better ready to pay big buck!
  • High maintenance. What if Bob and you do not get along and Bob leaves you? Well, the land you recently bought for him becomes vacant, Bob pays you nothing, and you still owe money on the land… Very expensive! Your best bet is to clean it up and reassign it to a new customer.

Conclusions

We have discussed three popular isolation models for multitenant applications. It is fun!

What is your favorite model?

Related resources:

This is part of a series of articles discussing multitenant applications. Other articles can be found here:

  1. Understanding Multitenancy Isolation Models in Plain English
  2. Building Multitenant App using Azure Cosmos DB in Clean Architecture

--

--

Shawn Shi
Geek Culture

Senior Software Engineer at Microsoft. Ex-Machine Learning Engineer. When I am not building applications, I am playing with my kids or outside rock climbing!