# albasyir/nestjs-microservice architecture

> Architecture diagram of the GitHub repository albasyir/nestjs-microservice, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/albasyir/nestjs-microservice
- Repository: https://github.com/albasyir/nestjs-microservice

## Overview

##### Project type and purpose
- This is a microservices system built with NestJS implementing a fan-out event pattern.
- It consists of three core services—Order, Kitchen, Notification—communicating via RabbitMQ, backed by a MySQL database, and containerized via Docker (development and production).

##### File structure
- Top-level “apps” folder: three microservices (order, kitchen, notification).
- apps/order:
– Domain layer (menu & order modules, entities, repos, services, controllers)
– Infrastructure layer (TypeORM config/migrations for MySQL, RabbitMQ config)
– Main entrypoint (`main.ts`)
- apps/kitchen and apps/notification: each has its own module/controller/service/config files and a `main.ts`.
- “libs/keep-alive-microservice”: shared library that likely kicks off a heartbeat publisher to RabbitMQ.
- Root-level compose.yml and docker/*: Docker Compose setup for dev and production, including RabbitMQ, MySQL, and service containers.
- Config files: `.env` per service, Dockerfiles, Nest CLI, TS configs.

##### README for insights
- Services run on ports 3000 (Order), 3001 (Kitchen), 3002 (Notification) in dev.
- RabbitMQ and MySQL management UIs exposed (ports 15672, 5100).
- In production, a single multi-stage Docker image runs each service based on `APP_TYPE` env var.
- Fan-out pattern: Order service publishes events to RabbitMQ; Kitchen and Notification services subscribe.

##### Main components
a. API Clients (Users or UI)
b. Order Microservice
c. Kitchen Microservice
d. Notification Microservice
e. Keep-Alive Microservice (heartbeat)
f. RabbitMQ Broker + Management UI
g. MySQL Database + Admin UI
h. Docker Compose / Container Orchestrator

##### Relationships & interactions
- Clients → Order Service (HTTP/REST requests)
- Order Service ↔ MySQL (TypeORM reads/writes)
- Order Service —publishes→ RabbitMQ Exchanges
- Kitchen & Notification Services —consume→ RabbitMQ Queues
- Keep-Alive Service —publishes→ RabbitMQ (ensures open connections)
- Orchestrator (Docker Compose) brings up containers and networking

##### Architectural patterns & technologies
- Microservice decomposition: each bounded context (order, kitchen, notification) lives in its own NestJS app.
- Fan-out messaging pattern via RabbitMQ (exchange → multiple queues).
- Domain-driven design in Order service (separation of domain vs. infra).
- TypeORM for persistence.
- Config modules for environment-agnostic settings.
- Containerization: Docker Compose for developer workflow; multi-stage Docker builds for production.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    Clients("API Clients / UI"):::external

    subgraph "Docker Compose Network"    
        direction TB

        %% Services
        OrderService["Order Service (NestJS, Port:3000)"]:::service
        KitchenService["Kitchen Service (NestJS, Port:3001)"]:::service
        NotificationService["Notification Service (NestJS, Port:3002)"]:::service
        KeepAliveService["Keep-Alive Service (Heartbeat)"]:::shared

        %% Messaging & DB
        RabbitMQ["RabbitMQ Broker"]:::infra
        MySQLDB["MySQL Database"]:::infra
        RabbitMgmt["RabbitMQ Management UI"]:::infra
        MySQLAdmin["MySQL Admin UI"]:::infra

    end

    %% Data Flow
    Clients -->|"HTTP/REST"| OrderService
    OrderService -->|"TypeORM Reads/Writes"| MySQLDB
    OrderService -->|"Publishes Events"| RabbitMQ
    KeepAliveService -->|"Publishes Heartbeat"| RabbitMQ

    subgraph "Fan-out Pattern"    
        direction TB
        RabbitMQ -->|"Exchange → Kitchen Queue"| KitchenQueue["Kitchen Queue"]:::infra
        RabbitMQ -->|"Exchange → Notification Queue"| NotificationQueue["Notification Queue"]:::infra
        KitchenQueue -->|"Delivers to"| KitchenService
        NotificationQueue -->|"Delivers to"| NotificationService
    end

    %% Docker Compose and Dockerfiles
    ComposeOrch["Docker Compose Orchestrator"]:::infra
    ComposeOrch -.-> OrderService
    ComposeOrch -.-> KitchenService
    ComposeOrch -.-> NotificationService
    ComposeOrch -.-> KeepAliveService
    ComposeOrch -.-> RabbitMQ
    ComposeOrch -.-> MySQLDB

    %% Click Events
    click OrderService "https://github.com/albasyir/nestjs-microservice/tree/main/apps/order"
    click OrderMain "https://github.com/albasyir/nestjs-microservice/blob/main/apps/order/src/main.ts"
    click OrderDomain "https://github.com/albasyir/nestjs-microservice/tree/main/apps/order/src/domain"
    click OrderInfra "https://github.com/albasyir/nestjs-microservice/tree/main/apps/order/src/infrastructure"
    click KitchenService "https://github.com/albasyir/nestjs-microservice/tree/main/apps/kitchen"
    click KitchenMain "https://github.com/albasyir/nestjs-microservice/blob/main/apps/kitchen/src/main.ts"
    click KitchenMod "https://github.com/albasyir/nestjs-microservice/blob/main/apps/kitchen/src/kitchen.module.ts"
    click KitchenCtrl "https://github.com/albasyir/nestjs-microservice/blob/main/apps/kitchen/src/kitchen.controller.ts"
    click KitchenSvc "https://github.com/albasyir/nestjs-microservice/blob/main/apps/kitchen/src/kitchen.service.ts"
    click KitchenCfg "https://github.com/albasyir/nestjs-microservice/blob/main/apps/kitchen/src/kitchen.config.ts"
    click NotificationService "https://github.com/albasyir/nestjs-microservice/tree/main/apps/notification"
    click NotificationMain "https://github.com/albasyir/nestjs-microservice/blob/main/apps/notification/src/main.ts"
    click NotificationMod "https://github.com/albasyir/nestjs-microservice/blob/main/apps/notification/src/notification.module.ts"
    click NotificationCtrl "https://github.com/albasyir/nestjs-microservice/blob/main/apps/notification/src/notification.controller.ts"
    click NotificationSvc "https://github.com/albasyir/nestjs-microservice/blob/main/apps/notification/src/notification.service.ts"
    click NotificationCfg "https://github.com/albasyir/nestjs-microservice/blob/main/apps/notification/src/notification.config.ts"
    click KeepAliveService "https://github.com/albasyir/nestjs-microservice/tree/main/libs/keep-alive-microservice"
    click KeepIndex "https://github.com/albasyir/nestjs-microservice/blob/main/libs/keep-alive-microservice/src/index.ts"
    click ComposeOrch "https://github.com/albasyir/nestjs-microservice/blob/main/compose.yml"
    click DevEntry "https://github.com/albasyir/nestjs-microservice/blob/main/docker/development/entrypoint.sh"
    click ProdCompose "https://github.com/albasyir/nestjs-microservice/blob/main/docker/production/compose-example.yml"
    click ProdEnv "https://github.com/albasyir/nestjs-microservice/blob/main/docker/production/.env.example"
    click ProdEntry "https://github.com/albasyir/nestjs-microservice/blob/main/docker/production/entrypoint.sh"
    click TypeORMCfg "https://github.com/albasyir/nestjs-microservice/blob/main/apps/order/src/infrastructure/typeorm/typeorm.config.ts"
    click Migrations "https://github.com/albasyir/nestjs-microservice/tree/main/apps/order/src/infrastructure/typeorm/migrations"
    click Dockerfile "https://github.com/albasyir/nestjs-microservice/tree/main/Dockerfile"

    %% Styles
    classDef service fill:#cce5ff,stroke:#004085,color:#004085;
    classDef infra fill:#d4edda,stroke:#155724,color:#155724;
    classDef shared fill:#fff3cd,stroke:#856404,color:#856404;
    classDef external fill:#f8d7da,stroke:#721c24,color:#721c24;
```
