Microservices in NestJS with RabbitMQ & PostgreSQL

In this blog post, we will walk you through the process of setting up a task manager application using NestJS and RabbitMQ. This application is composed of multiple microservices, each with a specific responsibility. We will cover the installation, configuration, and usage of these microservices.

Microservices Overview

Our task manager application consists of the following microservices โ€”

  • Nashville (Backend Facing Frontend) Microservice: Handles incoming requests from the client and forwards them to the Gallatin microservice.

  • Gallatin (Task Manager) Microservice: Handles task-related operations (CRUD) and emits events to the Ashland microservice.

  • Ashland (Logger) Microservice: Listens for events from the Gallatin microservice and logs them to the console.

  • Client Microservice: A simple client that interacts with the Nashville microservice over WebSocket.io.

+----------+     HTTP/WebSocket     +-------------------------+
|          | <--------------------- |                         |
|  Client  |                        | Backend Facing Frontend |
|          | ---------------------> |                         |
+----------+                        +-------------------------+
                                              |
                                              | gRPC
                                              |
                                              v
                              +------------------------+
                              |                        |
                              |     Task Manager       |
                              |                        |
                              +------------------------+
                                    |           |
                                    |           |
                              ------            ------
                              |                       |
                              v                       v
                        +------------+           +------------+
                        |            |           |            |
                        | PostgreSQL |           |  RabbitMQ  |
                        |            |           |            |
                        +----------- +           +------------+
                                                      |
                                                      |
                                                      v
                                              +------------+
                                              |            |
                                              |  Logger    |
                                              |            |
                                              +------------+

Features

The task manager application provides the following features โ€”

  • Add new tasks

  • Delete existing tasks

  • Event-driven architecture using RabbitMQ

Prerequisites

Before you begin, ensure you have met the following requirements โ€”

  • NodeJS (>= 20.16.0)

  • Yarn (>= 1.22.19)

  • Docker (for RabbitMQ & PostgreSQL)

Installation

Follow these steps to set up the task manager application โ€”

  1. Clone the repository โ€”
git clone https://github.com/faizahmedfarooqui/nestjs-rabbitmq-task-manager.git
cd nestjs-rabbitmq-task-manager
  1. Install dependencies โ€”
yarn install
  1. Setup RabbitMQ โ€”
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
# Access the RabbitMQ management console at http://localhost:15672
# Username: guest
# Password: guest
  1. Setup PostgreSQL โ€”
docker run -d --name postgres -e POSTGRES_PASSWORD=yourpassword -p 5432:5432 postgres
# Access the PostgreSQL database at localhost:5432
# Username: postgres
# Password: yourpassword
  1. Configure Variables โ€”
  • Update the RabbitMQ and PostgreSQL configurations in services/gallatin-task-manager/src/app.module.ts

  • Update the RabbitMQ configurations in services/ashland-logger/src/main.ts

  1. Run the Nashville (Backend Facing Frontend) Microservice โ€”
yarn start:nashville
  1. Run the Gallatin (Task Manager) Microservice โ€”
yarn start:gallatin
  1. Run the Ashland (Logger) Microservice โ€”
yarn start:ashland
  1. Run the Client Microservice โ€”
yarn start:client

Running Tests

To ensure everything is working correctly, you can tests for each microservice โ€”

  1. Run all tests of Gallatin Task Manager Microservice โ€”
yarn test:gallatin
  1. Run all tests for Nashville BFF Microservice โ€”
yarn test:nashville
  1. Run all tests for Ashland Logger Microservice โ€”
yarn test:ashland

Conclusion

In this blog post, we have covered the setup and configuration of a task manager application using NestJS, RabbitMQ and PostgreSQL.

By following the steps outlined above, you should be able to get the application up and running, and understand the role of each microservice in the system.

You can find the codebase here โ€”

Happy coding!


About Me ๐Ÿ‘จโ€๐Ÿ’ป

I'm Faiz A. Farooqui. Software Engineer from Bengaluru, India.
Find out more about me @ faizahmed.in

0
Subscribe to my newsletter

Read articles from Faiz A. Farooqui directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Faiz A. Farooqui
Faiz A. Farooqui

Software Engineer from Bengaluru, India. I make things at GeekyAnts.