Create a Multi-tenancy Application In Nest.js – Part 3

Recap

In the first part, create a Multi-tenancy Application In Nest.js - Part 1, we set up the Nest.js framework and configured and tested the microservices architecture application using Nest.js. In its second part, we used Sequelize and Mongoose to access the database and tested for both MySQL database and MongoDB.

Async Connection

In this part; we will see how to let the application connect to multiple databases depending on the request. Since it is a multi-tenancy application, each tenant has their own database containing their data accessing the same application, thus the application needs to connect to different databases. We will change the pass repository option method and use forRootAsync() instead of forRoot(), we need to use a custom class for configuration.

Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis

In this tutorial, we will learn how to implement multi-tenancy in a Spring Boot application with MongoDB and Redis.

Prerequisites

  • Spring Boot 2.4
  • Maven 3.6.+
  • JAVA 8+
  • Mongo 4.4
  • Redis 5

What Is Multi-tenancy?

Multi-tenancy is a software architecture in which a single instance of a software application serves multiple customers. Everything should be shared, except for the different customers’ data, which should be properly separated. Despite the fact that they share resources, tenants aren’t aware of each other, and their data is kept totally separate. Each customer is called a tenant.

Multi-tenancy authentication through Kong API Gateway

The API Gateway pattern implements a service that’s the entry point into a microservices-based application from external API clients or consumers. It is responsible for request routing, API composition, and other edge functions, such as authentication.

When working with a microservices architecture, either on a greenfield project or during migration from a monolith, a best practice is to start addressing cross-cutting concerns. Authentication is such a concern, and in this article, we’ll cover the authentication of a multi-tenancy application.