First NestJs Microservice using Redis
Overview of microservices
Microservices are smaller independent component of any API. Each has separate codebase , implementation and deployment due to which it is easier to handle development, releases, bug fixes etc. Also we can use different server end technology stack e.g. Java and Nodejs can be used to develop two separate microservices with different set of database
Implementing Microservices with NestJs
Nestjs natively support microservice architecture with support of multiple transport layer. These transporter are responsible for exchange messages between different microservices instance. It is quite easy to switch between different transport layer in NestJs.
Using Redis as transport layer
In this blog we are going to use Redis as transport layer. This transporter leverages the Pub/Sub feature of Redis. So each microservice can subscribe to multiple channel and each channel can be subscribed by multiple microservices.
Steps to create Micro service
- Installing Redis
- Create app for client and server
- Install Nestjs microservice in both apps
- Send message from client to server using Redis as transporter
Install Redis
$ brew install redis
After successful install of redis start redis service by hitting below command in terminal
$ brew services start redis
Create app for client and server
In order to create the nestjs app we first have to install nestjs cli. The subsequent command will install server and client NestJs app.
$ npm install -g @nestjs/cli
$ nestjs new server
$ nestjs new client
Configure NestJs Server
$ cd server
Open message.service.ts and paste the below code. Here we have created a message service which will receive the data from client via Redis Transporter and manipulate the data and return.
server/src/message.service.ts
Now we will configure redis in main.ts change the transport layer to redis and set the url in the option parameter. Replace the below code in main.ts
server/src/main.ts
Inject the message service in app.module.ts by importing from message.service.ts and add under the providers.
server/src/app.module.ts
The below file will get the data from the Redis transport layer and pass the same to MessageServcie
server/src/app.contoller.ts
If all goes fine then start the server by hitting the below command in terminal. You will able to see the below logs in the terminal
$ npm start
Configure NestJs Client
First we will configure Redis on client side so that it can communicate to server via Redis transport layer
client/src/message.service.ts
Inject the message service in app.module.ts by importing from message.service.ts and add under the providers.
client/src/app.module.ts
Here we will create a post route with endpoint user. This api will receive the data in body and send the same to Message Service
client/src/app.contoller.ts
Start the client api by running the below command
$ npm start
Now both server and client are running. To test this setup open postman prepare a post request as shown below in the screen shot. You will see the message retuned by server
Reference: