Microservices in Nesjs



  • NestJS is for building efficiant , scalable NodeJS Server side applications.
  • It uses progressive Javascript , is built with fully support Typescript.
  • It combines elements of OOP (Object Oriented Programming), FP (Functional Programming), FRP(Functional Reactive Programming).
  • Nest makes use of robust HTTP server framework like Express.

NestJS Features

  • NestJS was created to help developers to create Monoliths and Microservices
  • It’s very simple to use , quick to learn and easy to apply.
  • It leverages TypeScript — strongly typed language which is a superset of Javascript
  • Powerful Command Line Interface (CLI) to boost productivity and ease of development
  • Support the lot of specific modules , We can easily integrate with the concept of Type ORM , Mongoose , GraphQL, Logging , Validation, Caching , Websocket etc..,

Overview of Microservice

Microservice is a software architecture pattern where a large, complex application is broken down into many small, independent processes. These individual services are built for maximum performance, scalability, and maintainability. In Nest, a microservice is fundamentally an application that uses a different transport layer than HTTP.Nest supports several built-in transport layer implementations, called transporters, which are responsible for transmitting messages between different microservice instances. Most transporters natively support both request-response and event-based message styles.

Advantage of Microservice

1. Decoupled components — We can easily change or update a module without affecting other modules.

2. Scalability — Microservices don’t share the same memory space, which makes it easy to scale your application and increase resources for a particular service when needed.

3. Faster to build — Since you are splitting up your application into smaller pieces, you can start working on one module while still building other modules in parallel.

4. Language and technology agnostic — You can use different frameworks and programming languages for each service if needed. This also lowers the barrier to entry since each team only needs to be proficient in one language or framework instead of having knowledge about all languages and frameworks used in the system.

5. Reduces complexity — Microservices break down the application into smaller pieces that are easier to understand, maintain and test than a single monolithic codebase that does everything at once.

Create Application using NestCLI

To start the Nest CLI need to run the following command first. It will create the new project directory and populate with the initial nest files and the supporting modules.

The following modules are created automatically

Once the application has been fully initialized, we’re going to install the NestJS microservices library to help us modify the boilerplate application from an http-based REST API to a TCP-based microservice:

Create Client Service

We have to create two nest services , In this I created product-demo-main and product-microsrevices-main. NestJs have three basic concepts , that we will be working on all time. They are Modules , Controller and Services.

Here Main.ts file (product-demo-main / product-microservice-main) both are same. If you run the both you will get the same output as “Hello World!”




Once done , go ahead and replace the contents of your src/main.ts file with the following:



Inside `app.module.ts` we will add a ClientsModule with some option to allow our server to communicate to the microservice using TCP connection.


Inside `app.service.ts` we will add two additional methods to handle new endpoints by sending a message pattern and data to the corresponding microservice.


Inside `app.controller.ts` we will add two new endpoints that would allow us to test both READ and WRITE functionality for product.


The only unique part is how we’re initializing the application — instead of using the default NestFactory.create() method, we’re using NestFactory.createMicroservice() which provides us additional controls over the protocols and contracts our application responds to:



Database connection

NestJs, allowing you to easily integrate with any SQL or NoSQL database. To begin this we need to install typeorm and mysql. For integrating with SQL and NoSQL databases, Nest provides the @nestjs/typeorm package. Nest uses TypeORM because it’s the most mature Object Relational Mapper (ORM) available for TypeScript. Since it’s written in TypeScript, it integrates well with the Nest framework.

Once the installation process is complete, we can import the TypeOrmModule into the root AppModule.


Product entity

The TypeORM supports the repository design pattern, thus each entity has its own Repository. These repositories can be obtained from the database connection.Here we created product_entity. Here we added only two column id and name.

Product Dto

Start the application together

To run the all services , we should use the following commands in terminal.

1. Run `npm start` in the `product-microservice` project to start microservice.

2. Run `npm start` in the `product-demo` project to start API Gateway.

Sample API Request and Response — Create the Product

Sample API Request and Response — List the Product


In this article we used NestJs Microservices with realtime application. Also learned about NestJs with Database connection and TypeORM concepts , Some useful links are provided below and the sample application link also provide in git (https://github.com/Mohanavalli-Optisol/nest-microservices).

Connect With Us!