Microservice with RabbitMQ and NestJS 🐈
June 9, 2024 (6 months ago)Large scale applications are often built using microservices architecture. Microservices are small, independent services that work together. They are built around business capabilities and can be independently deployed. The pros of this strategy are that it allows for better scalability, faster development, and easier maintenance since each service is small and focused on a specific task, so the teams can work on them independently plus if one service fails, it doesn't affect the whole system.
For this guide we are going to build a simple microservice application using RabbitMQ and NestJS. We will have two services, one that sends a message to a queue and another that consumes the message from the queue.
Prerequisites
We are going to use Docker to run RabbitMQ in a container, but you can also install RabbitMQ directly on your machine.
Setting up RabbitMQ
Firstly, we need to run RabbitMQ in a container. Create a docker-compose.yml
file with the following configuration:
Run the following command to start the RabbitMQ container:
System Design
Our application will have two services:
- Auth Service: This service will send a message to a queue when a user logs in.
- Notification Service: This service will consume the message from the queue and send a notification to the user.
Hopefully, you already have a basic understanding of nestjs. If not, you can check out the official documentation. Cause we are not going to take a deep dive into the basics of nestjs. At least you should know how to create controllers, services, and modules.
Auth Service
The Auth service will have a single endpoint /login
that will simulate a user login. When a user logs in, the service will send a message to a queue with the user's information to notify the Notification service, which will then send a notification to the user via email that they have logged in.
Install the required dependencies:
Firstly in main.ts
file, or wherever you bootstrap the application, create a microservice instance and connect to the RabbitMQ server.
Now in the module where you want to use the RabbitMQ, import the ClientsModule
and create a client proxy. Lets hope you are gonna do it on the AuthModule
.
Now we can use the client proxy in the service to send a message to the queue.
The
emit
method is used to send a message to the queue. The first argument is the event name, and the second argument is the payload. There is also asend
method that can be used to send a message and wait for a response.
Notification Service
Same as we did for the Auth service, we need to create a microservice instance and connect to the RabbitMQ server in the Notification service.
In the Notification module, import the ClientsModule
and create a client proxy.
Now we can use the client proxy in the service to consume the message from the queue.
Now in your notification service, you can implement the sendNotification
method to send a notification to the user using nodemailer or whatever you prefer.
That's it! You have successfully built a microservice application using RabbitMQ and NestJS. You can now run the application and test the services.