Back

Multi-Tenant SaaS Architecture

Nikita Khabya

Nikita Khabya

5 min read

|

2 days ago

Multi-tenancy is an architecture style where a single instance of a software application serves multiple customers (tenants). It got popular with the rise of Software as a Service (SaaS) applications where multiple users or organizations use the same application while keeping their data isolated. Before Multi-tenancy, the bread-and-butter of SaaS applications was Single-tenancy architecture where each customer had their own instance of the application. Let's explore these concepts in detail.

Table of Contents

What is a Tenant? đŸ™‹â€â™€ī¸

A tenant is simply a user (in a workspace), group of users or an entire organization that uses a shared application or a system. These tenants need to be managed effectively to ensure that their data is isolated and secure. All these come under Single and Multi Tenancy ways of managing tenants.

Let's dive deeper into these concepts.

Single Tenancy vs Multi Tenancy âš’ī¸ ī¸

Single Tenancy vs Multi Tenancy Architecture Diagram


Diving deeper into Multi Tenancy 🌊

Building - Apartment Analogy đŸĸ

An analogy that is often used while discussing Multi-tenancy is that of an Apartment building and Individual apartments.

Why Multi Tenancy? ❓

Multi tenancy does not mean that tenants are sharing data but rather sharing the same application instance while keeping their data logically separated. This is preferred when

  1. Not breaking the bank is a priority.
  2. Spend less time in maintenance and managing infra.

It's much easier to make a Multi-Tenant application act like a Single Tenant application (by providing dedicated resources) than the other way around.

Noisy Neighbor Problem đŸ˜ī¸

Handling Customization in Multi Tenancy âš™ī¸

Ways of routing tenants 🚏

Routing tenants in a Multi-tenant application can be done in various ways depending on the requirements and architecture of the application.

Some common ways include:

Impact on SEO while using Subdomain vs Path-based routing 📈

Subdomains often have a concern regarding its influence on the website's rankings and overall SEO performance. Search engines treat subdomains as separate entities from the main domain, which means each subdomain requires its own SEO efforts including backlinks, content optimization, and authority building. In Path-based routing, all the content resides under the main domain, allowing them to inherit the SEO benefits of the main domain.

Subdomains is a better choice when the tenants require a distinct identity or branding (e.g., different products or services), while path-based routing is more suitable for applications where SEO is a primary concern (e.g content platforms, blogs etc).


Scalability in Multi Tenancy 📐

The two core things to keep in mind while working with Multi Tenancy is Data Isolation and Resource Management.

1. Data Isolation:

This simply means that I (as a tenant) should not be able to access other tenant's data and vice versa. This is important for security and privacy of tenants. Below are the different approaches to achieve data isolation:

  1. Silo Model / Database level isolation: Each tenant has their own database instance. This provides the highest level of data isolation but can be expensive to maintain.
  2. Separate Schema Model / Schema level isolation: Each tenant has their own schema within a shared database. This provides a good balance between data isolation and resource utilization.
  3. Pool Model / Row level isolation: All tenants share the same database and schema, but their data is logically separated using tenant identifiers. This is the most cost-effective and common approach while building small to medium scale Multi-tenant applications.

The choice of data isolation approach depends on factors like security requirements, customization needs, scalability goals, and budget.

2. Resource Management:

This refers to how the shared resources (compute, storage, network etc) are allocated and managed among tenants in a Multi-tenant architecture.

We discussed about the noisy neighbor problem earlier which is one of the challenges of resource management in Multi-tenancy where one tenant's heavy usage can impact the performance of other tenants.

To address this, resources need to be managed effectively and allocated based on tenant requirements and usage patterns. Once this is achieved, it leads to better performance, cost and operational efficiency, and scalability of the application.

Real world examples 🌐

Multi-tenancy is widely used in various domains like Project Management, CRM, Content Management, LMS etc. It's used by many popular SaaS applications like:


Multi Tenancy in Next.js 🚀

Next.js is a popular React framework that can be used to build Multi-tenant applications.

It facilitates this by the custom subdomain routing along with using middleware to handle tenant-specific logic.

Check out this video by Lee Robinson implementing Multi-tenancy in Next.js:

Multi-Tenant SaaS in Next.js

Multi-tenancy Demo

Github

TL;DR