A production-ready, scalable microservices architecture built with NestJS, Apache Kafka, and Nx Monorepo. This project demonstrates modern backend development practices including event-driven architecture, domain-driven design, and containerized infrastructure.
- Overview
- Architecture
- Tech Stack
- Microservices
- Getting Started
- Project Structure
- Event Patterns
- Database Schema
- Development
- API Documentation
This project is a research and development initiative exploring microservices architecture and event-driven design patterns using Apache Kafka. It implements a real-time chat platform with server/channel management, user authentication, file handling, and messaging capabilities.
โ
Event-Driven Architecture - Asynchronous communication using Apache Kafka
โ
Microservices Pattern - Loosely coupled, independently deployable services
โ
Nx Monorepo - Efficient code sharing and dependency management
โ
Type-Safe DTOs - Shared data transfer objects across services
โ
Database per Service - Each microservice manages its own PostgreSQL database
โ
Docker Compose - Complete infrastructure orchestration
โ
Kafka UI - Real-time monitoring of message streams
โ
API Gateway - Centralized entry point with Swagger documentation
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ API Gateway โ
โ (HTTP REST Interface) โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Apache Kafka Broker โ
โ (Event Bus & Message Queue) โ
โโโฌโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโโ
โ โ โ โ
โผ โผ โผ โผ
โโโโโโโ โโโโโโโโ โโโโโโโโ โโโโโโโโ
โAuth โ โServerโ โ Msg โ โ File โ
โSvc โ โ Svc โ โ Svc โ โ Svc โ
โโโโฌโโโ โโโโฌโโโโ โโโโฌโโโโ โโโโฌโโโโ
โ โ โ โ
โผ โผ โผ โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PostgreSQL Databases โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Client โ HTTP Request โ API Gateway
- API Gateway โ Kafka Event โ Microservice
- Microservice โ Process Event โ Database Operation
- Microservice โ Kafka Response โ API Gateway
- API Gateway โ HTTP Response โ Client
| Technology | Purpose | Version |
|---|---|---|
| NestJS | Backend framework | 11.x |
| Apache Kafka | Event streaming platform | 7.6.1 |
| Nx | Monorepo management | 21.5.2 |
| TypeScript | Programming language | 5.9.2 |
| Prisma | ORM & Database toolkit | 6.16.2 |
| PostgreSQL | Relational database | Latest |
- Docker & Docker Compose - Containerization
- Zookeeper - Kafka coordination service
- Kafka UI - Stream monitoring (Port 8080)
- Swagger/OpenAPI - API documentation
- class-validator & class-transformer - DTO validation
- argon2 - Password hashing
- AWS SDK - S3 file storage
- BullMQ - Job queue management
- uuid - Unique identifier generation
- Port: 3001
- Type: HTTP REST API
- Responsibilities:
- Client-facing REST endpoints
- Request routing to microservices
- Swagger API documentation
- Bearer token authentication
- Endpoints:
/api/docs(Swagger UI)
- Transport: Kafka Microservice
- Consumer Group:
auth-consumer-group - Responsibilities:
- User authentication & authorization
- JWT token generation & validation
- Password hashing with Argon2
- User profile management
- Message Patterns:
user.signin- User loginuser.signup- User registrationuser.verifyToken- Token validation
- Transport: Kafka Microservice
- Consumer Group:
server-consumer-group - Responsibilities:
- Discord-like server management
- Channel creation & management
- Member roles & permissions
- Invite code generation
- Message Patterns:
create.server,delete.server,update.serverget.server,get.involved.serverleave.server,is.server.existschannel.create,channel.update,channel.delete,channel.get
- Transport: Kafka Microservice
- Consumer Group:
message-consumer-group - Responsibilities:
- Real-time message handling
- Message persistence
- Message history retrieval
- Port: 3002
- Transport: Hybrid (HTTP + Kafka)
- Consumer Group:
file-service-consumer - Responsibilities:
- File upload/download
- AWS S3 integration
- Presigned URL generation
- File metadata management
- Node.js (v18 or higher)
- Docker & Docker Compose
- npm or yarn
-
Clone the repository
git clone <repository-url> cd node_chat
-
Install dependencies
npm install
-
Start infrastructure services
docker compose up -d
This starts:
- Zookeeper (Port 2181)
- Kafka Broker (Ports 9092, 29092)
- Kafka UI (Port 8080)
-
Set up environment variables
Each service has its own
.envfile. Configure:- Database URLs (PostgreSQL)
- AWS credentials (for file service)
- JWT secrets (for auth service)
-
Run database migrations
# For each service with Prisma npx nx run auth-service:prisma-migrate npx nx run server-service:prisma-migrate npx nx run message-service:prisma-migrate npx nx run file-service:prisma-migrate -
Start all services
# API Gateway npx nx serve api_geteway # Microservices (in separate terminals) npx nx serve auth-service npx nx serve server-service npx nx serve message-service npx nx serve file-service
- API Gateway: http://localhost:3001/api
- Swagger Docs: http://localhost:3001/api/docs
- Kafka UI: http://localhost:8080
node_chat/
โโโ apps/
โ โโโ api_geteway/ # HTTP REST API Gateway
โ โโโ auth-service/ # Authentication microservice
โ โโโ server-service/ # Server & channel management
โ โโโ message-service/ # Message handling
โ โโโ file-service/ # File storage & retrieval
โ โโโ *-e2e/ # End-to-end tests
โโโ shared/ # Shared library
โ โโโ src/
โ โโโ lib/
โ โโโ dto/ # Data Transfer Objects
โ โโโ guards/ # Auth guards
โ โโโ interfaces/ # TypeScript interfaces
โ โโโ types/ # Type definitions
โ โโโ pipes/ # Validation pipes
โโโ compose.yaml # Docker Compose configuration
โโโ nx.json # Nx workspace configuration
โโโ package.json # Root dependencies
The shared library provides:
- DTOs - Type-safe data transfer objects for all services
- Guards - Microservice authentication guards
- Interfaces - Common TypeScript interfaces
- Types - Shared type definitions
- Utilities - Common helper functions
This ensures consistency across microservices and reduces code duplication.
// user.signin
{ email: string, password: string }
// user.signup
{ name: string, email: string, password: string }
// user.verifyToken
{ token: string }// create.server
{ name: string, imageUrl: string, profileId: string }
// channel.create
{ name: string, type: 'TEXT' | 'AUDIO' | 'VIDEO', serverId: string }model Profile {
id String @id @default(uuid())
name String
email String @unique
password String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}model Server {
id String @id @default(uuid())
name String
imageUrl String
inviteCode String @unique
profileId String
members Member[]
channels Channel[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Channel {
id String @id @default(uuid())
name String
type ChannelType @default(TEXT)
serverId String
profileId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
enum ChannelType { TEXT, AUDIO, VIDEO }
enum MemberRole { ADMIN, MODERATOR, GUEST }npx nx serve <service-name>npx nx build <service-name>npx nx test <service-name>
npx nx e2e <service-name>-e2enpx nx graphnpx nx lint <service-name>npx nx run <service-name>:prisma-generateOnce the API Gateway is running, access the interactive Swagger documentation:
URL: http://localhost:3001/api/docs
The Swagger UI provides:
- Complete API endpoint documentation
- Request/response schemas
- Try-it-out functionality
- Bearer token authentication
This project demonstrates proficiency in:
- โ Designing and implementing microservices architecture
- โ Event-driven communication with Apache Kafka
- โ Managing monorepos with Nx
- โ Database design and ORM usage (Prisma)
- โ Docker containerization and orchestration
- โ RESTful API design and documentation
- โ Authentication and authorization patterns
- โ Type-safe development with TypeScript
- โ Code sharing and reusability strategies
- โ Scalable backend architecture patterns
- WebSocket integration for real-time messaging
- Redis caching layer
- Kubernetes deployment manifests
- CI/CD pipeline with GitHub Actions
- Distributed tracing with Jaeger
- Rate limiting and API throttling
- Comprehensive integration tests
- Message encryption
- Multi-tenancy support
MIT
Built with โค๏ธ as a research project exploring modern microservices architecture