Multi Tenancy in Node.js: A Complete Guide to Cloud-Based Architecture

Written by
Blueflame Labs
Published
May 28, 2025
Multi Tenancy
Custom Enterprise Solutions
Open Source Technologies
Software Development
Technology & IT

Introduction

Multi tenancy is a key architecture in modern SaaS and cloud applications that enables a single software instance to serve multiple customers (tenants). It reduces infrastructure costs, simplifies maintenance, and allows customizations per client. This guide explains the concept, benefits, and how to build a multi-tenant Node.js app using Express and PostgreSQL.

What is multi-tenancy?

Multi-tenancy is a software architecture in which a single instance of software runs on a server and serves multiple tenants. Here each client or customer is referred to as a tenant. A good example of multi-tenancy would be GitHub, where each organization has its separate work area. This concept is widely used when developing software that runs for different organizations. With the multi-tenancy architecture, tenants can customize parts of their system like applying UI themes, changing database schemas, etc. Tenants also get access to the system’s configurations, data, and software.

In the context of multi tenancy in cloud computing, many cloud service providers adopt multi-tenant architecture to reduce overall infrastructure costs. As all the customers are sharing resources like hardware and software, the overall license cost gets reduced. If you run all the requirements on a single software copy, then the service provider has to buy only a single software copy to serve all the customers. For data mining purposes, multi-tenant applications are very useful since all the data is either in the same database or shares the same schema. Therefore, developers find it easy to run multiple complex queries.

Multitenant apps are more complex to design and maintain. Providers need to focus on security since there are multiple customers using the system. Sometimes a software release intended for specific tenants could introduce a system failure that might affect all customers at once.

Why Multi-Tenancy?

  • It allows segregation of data across clients.
  • Customizing the app according to the client’s needs and feasibility without actually altering the source code. For example, providing separate branding for each tenant or using feature flags to provide different features for each tenant.
  • Maximizing efficiency while reducing the cost required for centralized updates and maintenance.

Methods of multi-tenancy

When designing a multi-tenant application, you must carefully choose the tenancy design.

Below are two models commonly used when partitioning tenant data in SaaS environments:

  1. Logical separation of data: In this method, there is only one database for all tenants. Their data is separated using a unique identity for each client. The codebase is responsible for storing and retrieving data using this unique identity.
  2. Physical separation of data: This method separates the data by provisioning different databases for different clients. This helps scale the application as the number of clients grows and allows scaling the database as per each client’s need.

Prerequisites for Implementing Multi-Tenancy

  1. Node JS and NPM: https://nodejs.org/en/download/
  2. Express JS: https://expressjs.com/en/starter/installing.html
  3. PostgreSQL: https://www.postgresql.org/

Implementation Steps of a Multi-Tenant System with Physical Separation of Data

We will create an application using NodeJS and ExpressJS that will identify the tenant from each request and provide the data for that particular tenant’s database.

Let’s start with a few definitions:

  • Tenant: Users with specific privileges to their data.
  • Common connection: An Object that holds information about the connection to the common database.
  • Tenant connection: Holds the information about the connection to the tenant database.
  • Common DB: Contains information about all tenant databases.
  • Tenant DB: Separate database for each client.

Booting up the App

  • Create a file named server.js inside the src folder.
  • Initialize the Express application and create a basic route.
  • Run the application using npm start.

Multi-Tenancy

Database Configuration

  • Create the following databases: common_db, tenant1_db, tenant2_db.
CREATE DATABASE common_db;
CREATE DATABASE tenant1_db;
CREATE DATABASE tenant2_db;
  • Make sure to create different credentials for these databases.
  • Create a table in common_db to hold connection information to tenant databases:
CREATE TABLE tenants (
    id SERIAL PRIMARY KEY,
    uuid VARCHAR (255) UNIQUE NOT NULL,
    db_name VARCHAR (100) UNIQUE NOT NULL,
    db_host VARCHAR (255),
    db_username VARCHAR (100),
    db_password TEXT,
    db_port INTEGER NOT NULL DEFAULT 5432,
    created_at TIMESTAMP DEFAULT NOW (),
    updated_at TIMESTAMP DEFAULT NOW ()
);
  • Insert tenant credentials into common_db:
INSERT INTO tenants (uuid, db_name, db_host, db_username, db_password, db_port)
VALUES ('tenant1', 'tenant1_db', 'localhost', 'tenant1_user', '***encrypted_password_for_tenant_1***', 5432);

INSERT INTO tenants (uuid, db_name, db_host, db_username, db_password, db_port)
VALUES ('tenant2', 'tenant2_db', 'localhost', 'tenant2_user', '***encrypted_password_for_tenant_2***', 5432);

Connection Configuration to Common Database

  • Create a .env file in your project root and add the common_db credentials.
  • Create a file commonDBConnection.js inside the utils folder and add connection configuration using Knex.

Multi-tenant Application on Node js

Adding a Service to Manage Connections

  • Create connectionManager.js in the utils folder. This will help connect to tenant databases and manage resolved connections.

Multi-tenant Application on Node js

Multi-tenant Application on Node js

Creating Connection Resolver

  • Create connectionResolver.js in the same directory. This will determine which connection to use during a request.

Multi-tenant Application on Node js

Create tenantDBPool.js in utils folder

  • This file provides dynamic connections based on the UUID.

Create getTenantConnection Function

  • Add this function in utils/commonfunctions.js to return the correct connection string.

Multi-tenant Application on Node js

Create a Service File to Get User Information

  • Create userService.js inside src/services. Use the function:
const postgressdbPool = getTenantConnection(uuid);
  • **Create **userController.js
    • Add this file to the controller folder. Call the service function and pass the uuid as a parameter.
  • **Update **server.js
    • Add the following code:
      const { connectAllDb } = require('./utils/connectionManager')();
      const connectionResolver = require('./utils/connectionResolver')();
      
      connectAllDb();
      app.use(connectionResolver.resolve);

Conclusion

In this tutorial, we created a multi-tenant application and learned how to provide and manage separate work areas (databases) for different organizations using the concept of multi-tenancy. Using the above steps, we successfully implemented a multi-tenant application, demonstrating key elements of multi-tenancy architecture and its role in scalable cloud-based applications.