Dockerized NestJS Micro-service application with Redis

Tushar Roy Chowdhury
3 min readApr 23, 2021

Hello everyone! I am going a describe Microservices with Redis using an example of a simple TODO application where two different types of users are available, Admin & Client user.

N.B. This tutorial needs some knowledge about NestJS.

If you want to check out other repository regarding this application on top of NestJS GitHub link and you can also find few other branches and tutorials are
1) Simple to-do application
2) Refresh token generator
3) Two Factor Authentication(2FA)

What admin can do?
Admin can log in with their account & see the user logs of clients as well as their to-do lists.

What client can do?
The client can log in with their following account & can do CRUD operation.

How user log create?
Whenever clients successfully logged in to their respective accounts, log data will populate with users' unique email and date times.

What are the names of those services under the dockerized container?
1) admin-service
2) client-service
3) redis-server
4) db
5) pgadmin

How many databases are available in the application?
Two separate databases. One is for the client panel & the other for the admin panel.

What is the need for two separate databases?
Whenever the client successfully logged in, the logged data is always stored in admin-db using admin microservice. And clients’ data store in clients’ DB but the admin need to fetch all the clients’ to-do list through client microservice.

The Docker compose file:

What needs to be changed to do microservices workable?
Step-1: Install few modules

npm install @nestjs/microservices redis

Step-2: As we are using docker, so following command will create DB in your container for both service.

docker-compose exec db createdb ADMIN_DB -U postgres
docker-compose exec db createdb CLIENT_DB -U postgres

Step-3: Update main.ts file (admin-service & client-service)

N.B: Line 24–33 is swagger implementation which is for API documentation.

Step-4: Submit the user log from client-service to admin-service. Update client-service/src/auth/service/auth.service.ts, /src/admin-micro-service/admin-service.service.ts file

admin-service.service.ts file Line 18, createUserLog() function will send request to admin-service to create user log. Now let’s receive the request from admin-service using @MessagePattern({ cmd: ‘createUserLog’ })

Step-5: Create user log

Let’s create admin-service/src/user-log/user-log.controller.ts, /user-log.service.ts, /user-log.repository.ts

After successfully login into admin panel you can fetch the user log data through getUserLog() function (Line-18).

Step-6: Let’s fetch all to-do list from client service.
First, have to write few lines of code in admin service. Create admin-service/src/todo/todo.controller.ts, /todo/service/todo.service.ts file

In todo.service.ts file (Line-18) getAllTodo() will fetch to-do list from client-service. Now let’s create the receiver of to-do list.

Step-7: Create client-service/src/todo/controller/todo-microservice.controller.ts & update /todo/service/todo.service.ts and /todo/repository/todo.repository.ts file

Now admin can fetch data from client DB using micro-service. If everything setup as expected then run the following command- docker-compose up

So Here is the GitHub link for the code. GitHub link
Thank You !!!

--

--

Tushar Roy Chowdhury

I am a passionate programmer and always keep eye on new technologies and scrutinize them until getting into shape.