Production-grade, multi-tenant Enterprise Hospital Management System (EHMS) structured as a clean-architecture monorepo with distinct frontend and backend services, fully integrated with a modern DevOps, DevSecOps, and secure authentication stack.
Architecture • Role Portals • Getting Started • Enterprise Stacks • Jenkins CI/CD • Observability
- 🏗️ Complete End-to-End System Architecture
- 👑 Role-Based Dashboards & Demo Credentials
- 🛡️ Secure Authentication System
- ⚙️ DevOps & DevSecOps Stack Overview
- 🚀 Getting Started
- 🐳 Enterprise Docker Infrastructure Stacks
- 🛠️ Workspace Command Summary
- 🔗 Jenkins CI/CD Integration (Port 8080)
- ⚡ How to Connect a Production Redis Instance
- ☁️ How to Use Terraform for AWS Cloud KYC Storage
- 🐛 Troubleshooting: Docker Compose Port 4000 Error
The project is structured as an enterprise-grade monorepo featuring a clean layered architecture, robust persistence mechanisms, and an automated DevOps & DevSecOps delivery pipeline:
graph TB
subgraph Client ["💻 Frontend Client (apps/web)"]
direction TB
WebUI["Next.js App Router (TypeScript)"]
Dashboards["Role Dashboards\n(SuperAdmin | HospitalAdmin | AmbulanceAdmin | Doctor | Nurse | Pharmacist | Patient)"]
WebPages["Core Modules\n(Auth | Appointments | EMR | Billing | Patients | Settings)"]
WebUI --> Dashboards
WebUI --> WebPages
end
subgraph API ["⚡ RESTful Backend API (apps/api)"]
direction TB
ExpressAPI["Express.js API Engine"]
subgraph Middlewares ["Middleware Layer"]
AuthGuard["Argon2id + Salt + Pepper Guard"]
RBAC["Role-Based Access Control"]
RateLimit["Rate Limiter & Audit Logger"]
end
subgraph CoreModules ["Domain Services"]
ModAuth["Auth & 2FA/OTP"]
ModPatient["Patient & EMR"]
ModAppt["Appointments"]
ModBilling["Billing & Pharmacy"]
ModLab["Lab & Inventory"]
ModAI["AI & Messaging"]
ModAudit["Audit & Staff"]
end
ExpressAPI --> Middlewares
Middlewares --> CoreModules
end
subgraph SharedPkg ["📦 Monorepo Packages (packages/)"]
SharedLib["packages/shared\n(Zod Schemas, Types, RBAC Constants)"]
ConfigLib["packages/config\n(ESLint & TypeScript Presets)"]
end
subgraph DataServices ["🗄️ Persistence & Async Processing"]
MongoDB[("MongoDB Primary DB\n(Patient Records, EMR, Invoices)")]
Redis[("Redis In-Memory Store\n(Sessions, OTP Codes, Rate Limits)")]
Mailpit["Mailpit / SMTP Server\n(Transactional Emails)"]
BullMQ["BullMQ\n(Async Queue Processing)"]
end
subgraph DevOps ["🚀 Infrastructure, DevOps & Security Layer"]
subgraph IaC ["Terraform Cloud Infra"]
Terraform["AWS Terraform\n(EKS Cluster, VPC, ECR, IAM)"]
end
subgraph K8sStack ["Kubernetes & GitOps"]
K8s["EKS Kubernetes Cluster"]
Helm["Helm Charts"]
ArgoCD["Argo CD (GitOps Delivery)"]
NginxIngress["Nginx Ingress Controller"]
NginxIngress --> K8s
Helm --> K8s
ArgoCD --> K8s
end
subgraph CICD ["Jenkins CI/CD & Security"]
Jenkins["Jenkins Pipeline (Port 8080)"]
SonarQube["SonarQube (Code Quality)"]
Trivy["Trivy (Security Audit Scanner)"]
Postman["Postman API Suite"]
Jenkins --> SonarQube
Jenkins --> Trivy
Jenkins --> Postman
end
subgraph Monitoring ["Observability Stack"]
Prometheus["Prometheus"]
Grafana["Grafana Dashboards"]
Prometheus --> Grafana
end
end
%% Dependencies
Client -. Shared Code .-> SharedLib
API -. Shared Code .-> SharedLib
Client -. Shared Config .-> ConfigLib
API -. Shared Config .-> ConfigLib
%% Network Flow
Client -- "HTTP / REST API" --> ExpressAPI
ExpressAPI --> MongoDB
ExpressAPI --> Redis
ExpressAPI --> Mailpit
ExpressAPI --> BullMQ
%% Infra Connections
Terraform -. Provisions .-> K8sStack
CICD -. Scans & Deploys .-> K8sStack
Monitoring -. Scrapes Telemetry .-> API
Monitoring -. Scrapes Metrics .-> K8sStack
Note
Blue Architecture Logic — Layered Domain Decoupling
- Frontend Application (
apps/web): Built on Next.js 14 App Router, dynamicuseAuthhook profile resolution, and Framer Motion micro-interactions. - Backend API Engine (
apps/api): Modular Express.js service-repository layout enforcing RBAC, rate-limiting, and SHA-256 clinical audit trails.
Tip
Green Component Best Practice — Shared Monorepo DTOs
packages/shared: Centralizes Zod DTO validation schemas, type definitions, and permission matrices so frontend and backend remain strictly synchronized.packages/config: Standardizes ESLint, Prettier, and TypeScript compilation targets.
- Frontend Web Application (
apps/web): Next.js (App Router) with TypeScript, Tailwind CSS, Framer Motion, and Axios. Features role-tailored dashboards for SuperAdmins, HospitalAdmins, AmbulanceAdmins, Doctors, Nurses, Pharmacists, Lab Techs, Blood Bank, and Patients, plus dedicated pages for Appointments, EMR, Billing, Patients, Settings, and Auth flows. - Backend REST API (
apps/api): Built with Express.js and TypeScript, following modular service-repository architecture. Implements domain services for Auth (Argon2id + Salt + Pepper + OTP), Patients, Appointments, EMR, Billing, Pharmacy, Inventory, Lab, Messaging, AI, and Auditing. - Shared Workspace Packages (
packages/):packages/shared: Shared Zod validation DTO schemas, TypeScript type declarations, and RBAC matrix constants.packages/config: Common ESLint, Prettier, and TypeScript base presets.
- Data & Persistence Layer:
- MongoDB: Primary NoSQL data store managed via Mongoose ODM for patient records, medical records, invoices, and system entities.
- Redis: In-memory caching layer for user session management, 5-minute OTP code TTL, rate-limiting counters, and BullMQ queue backend.
- Mailpit / SMTP: Captures transactional emails and OTP verification messages in development environments.
- BullMQ: Asynchronous background worker queue for non-blocking operations.
- DevOps & DevSecOps Infrastructure:
- Terraform (
infra/terraform): Infrastructure as Code (IaC) provisioning AWS cloud resources including VPC, subnets, EKS Kubernetes Cluster, ECR registries, and IAM roles. - Kubernetes & Helm (
infra/k8s,infra/helm): Production manifests and Helm chart releases for container orchestration with liveness/readiness probes, HPA (Horizontal Pod Autoscaler), and Nginx Ingress routing. - Argo CD (
infra/argo): GitOps continuous deployment controller reconciling cluster state with repository updates. - Jenkins Pipeline (
Jenkinsfile): CI/CD automation executing lint checks, SonarQube static code analysis, Trivy vulnerability scanning (repo & Docker image), and container builds. - Monitoring (
infra/monitoring): Integrated Prometheus metrics scraping and Grafana dashboard visualization for API performance, memory usage, and cluster health. - Automated Testing (
tests/postman): Postman API collection for automated integration testing across authentication and core endpoints.
- Terraform (
MedFlow provides 8 specialized role portals out-of-the-box. Access any portal via /login with 1-click preset authentication:
| Role Portal | Demo User ID / Email | Password | Primary Key Capabilities & Scope |
|---|---|---|---|
| 👑 Super Admin | SuperAdmin |
Admin@321 |
Multi-tenant governance, security audit logs, global EMR vault, enterprise settings |
| 🏢 Hospital Admin | HospitalAdmin |
Hospital@321 |
Real-time facility bed census (88%), department analytics, staff roster scheduling |
| 🚨 Ambulance Admin | AmbulanceAdmin |
Ambulance@321 |
Live GPS dispatch tracker map, emergency call queue, fleet telemetry & vehicle registration |
| 🩺 Doctor / Physician | Dr. Anup Singh |
Doctor@321 |
Clinical OPD workstation, EMR patient charts, prescription studio & lab orders |
| ❤️ Nurse & Caregiver | Sunita Patel |
Caregiver@321 |
Inpatient bed vitals log, triage queue, ward round management & care plans |
| 💊 Pharmacist | Pharmacist Dispensary |
Pharmacist@321 |
Prescription fulfillment studio, drug inventory stock control & dispensing |
| 🧪 Lab Technician | Rajesh Kumar |
Technician@321 |
Diagnostic pathology audits, specimen upload, lab report publishing |
| 🩸 Blood Bank | Blood Bank Reserve |
BloodBank@321 |
Blood group stock reserves (A+, O-, etc.), donor registry & emergency supply matching |
Important
Cryptographic Security Logic — Argon2id + Salt + Pepper
- Argon2id Key Derivation: High-memory, time-cost password hashing.
- Dynamic 16-Byte Salt: Cryptographically generated per user registration to eliminate rainbow table vectors.
- Server-Side Chili Pepper: High-entropy
APP_PEPPERsecret key prevents password leaks even during database exposure.
Warning
Red Security Alert — 5-Minute OTP Verification TTL
- Redis TTL Invalidation: 6-digit OTP verification codes expire after 5 minutes (300 seconds) in Redis.
- Single-Use Verification: Tokens are deleted from Redis upon single successful use to prevent replay attacks.
- Password Hashing (Argon2 + Dynamic Salt + Chili Pepper):
- Argon2id: Utilizes the secure Argon2 key derivation function.
- Dynamic Salt: Generates a cryptographically secure random 16-byte salt per user on registration.
- Chili (Pepper): Uses a server-side high-entropy pepper (
APP_PEPPER) configured in environment variables, protecting user passwords against database compromise.
- Multi-Factor OTP Verification:
- Generates a secure 6-digit verification code stored in Redis with a strict 5-minute time-to-live (TTL).
- Dispatches the code via SMTP to the user's email.
- Enforces single-use verification (invalidates OTP from Redis immediately upon use) and starts a secure 7-day session.
Note
Blue DevSecOps Protocol — Continuous Quality & Vulnerability Auditing
- Static Code Analysis: SonarQube scans TypeScript code quality, hotspots, and code smells on
http://localhost:9000. - CVE Container Auditing: Trivy scans filesystem dependencies and Docker container layers before pushing to registry.
Tip
Green Telemetry Protocol — Prometheus & Grafana Observability
- Metrics Scraper: Prometheus scrapes API response latencies, memory footprint, and HTTP request counters on port
9090. - Visual Telemetry: Grafana renders real-time color charts and SLA uptime metrics on port
3005.
- Docker: Multi-stage production container configuration for lightweight images (
api.prod.Dockerfile,web.prod.Dockerfile) and multi-container setups (docker-compose.yml,docker-compose.dev.yml). - Redis: Caches session records and tracks temporary verification states (OTP).
- SMTP (Mailpit): Captures transactional registration and authentication mails.
- Jenkins: Automates the CI/CD pipeline, building images, auditing repositories, running quality checkers, and deploying using Helm (configured in
Jenkinsfile). - Postman: Comprehensive integration testing suite for verifying endpoint validation logic and authentication states (located at tests/postman).
- SonarQube: Automatically analyzes static code quality, checking for security vulnerabilities, hotspots, and code smells.
- Trivy: DevSecOps scanner integrated into Jenkins pipeline stages to check dependency vulnerabilities (repo audit) and scan built Docker images for CVEs.
- Prometheus & Grafana: Monitors application telemetry, API latency, and database connectivity.
- Helm: Deploys the release components (Next.js, Express, databases) into Kubernetes namespaces.
- Kubernetes (K8s): Container orchestration configuration with resource specifications, readiness/liveness probes, and ingress ssl-redirect annotation settings.
- Argo CD: Implements GitOps deployment mechanics, reconciling Helm chart configurations directly into the EKS production cluster.
- Terraform: Provisions core cloud resources including VPC, routing tables, and the EKS Kubernetes cluster in AWS (located in infra/terraform).
Important
Prerequisites Checklist
- Ensure Node.js (v20+), pnpm (v10+), and Docker Desktop are installed and running on host machine.
Run this command from the root workspace directory to fetch and link all packages:
pnpm installWarning
Red Port Binding Conflict Notice
- Before starting Full Docker Mode, ensure no local Express API or Next.js app is occupying port
4000or3000.
- Stop conflicting local services on your machine (e.g., if you have native MongoDB, Redis, or Mailpit running).
- Start the containers from the root workspace directory:
docker compose -f docker-compose.dev.yml up -d
- Verify the running services:
- Frontend UI (Next.js): http://localhost:3000
- Backend API (Express): http://localhost:4000/ready (use this to check system health)
- Mailbox UI (Mailpit): http://localhost:8026 (this catches OTP emails for login)
Tip
Recommended Developer Workflow
- Mixed Mode provides hot-reloading for code edits while running backing databases (MongoDB, Redis, Mailpit) isolated in Docker.
- Start backing databases and services in Docker:
docker compose -f docker-compose.dev.yml up -d mongo redis mailpit
- Ensure dependencies are installed and built:
pnpm install pnpm run build
- Launch the backend and frontend dev servers concurrently:
Note: This command runs the Express API on port
pnpm run dev
4000and the Next.js Web Frontend on port3000concurrently. - Access the application URLs:
- Frontend App: http://localhost:3000
- Backend API: http://localhost:4000
- Mailpit Webbox: http://localhost:8026 (to fetch verification OTP codes)
| Docker Stack File | Services Included | Key Ports | Usage Command |
|---|---|---|---|
docker-compose.prod.yml |
Nginx, API, Web, Mongo, Redis, Mailpit | 80, 443, 4000, 3000 |
docker compose -f docker-compose.prod.yml up -d |
docker-compose.monitoring.yml |
Prometheus, Grafana, Node Exporter, Redis Exporter | 9090, 3001, 9100, 9121 |
docker compose -f docker-compose.monitoring.yml up -d |
docker-compose.ci.yml |
Jenkins CI/CD, SonarQube, Sonar PostgreSQL DB | 8080, 9000, 50000 |
docker compose -f docker-compose.ci.yml up -d |
docker-compose.security.yml |
Trivy CVE Scanner, OWASP Dependency-Check | Automated Scans | docker compose -f docker-compose.security.yml run trivy-scanner |
Run these scripts from the monorepo root:
pnpm run dev: Spin up all services concurrently on the host.pnpm run build: Compile and build all workspaces.pnpm run lint: Execute ESLint checks across all directories.pnpm run typecheck: Verify TypeScript compilations.pnpm run format: Format all codebase files using Prettier rules.
Note
Blue Jenkins Pipeline Architecture & Execution Logic
- Stage 1 (Checkout): Clones latest code from
mainbranch. - Stage 2 (Install & Audit): Restores workspace dependencies via
pnpm installand runs ESLint static code audits. - Stage 3 (SonarQube Analysis): Dispatches code metrics to SonarQube Server (
http://localhost:9000) and waits for Quality Gate approval. - Stage 4 (Trivy Security Gate): Scans monorepo dependencies for vulnerabilities (
trivy fs). - Stage 5 (Container Build): Constructs production multi-stage Docker images for API and Web.
- Stage 6 (Container Scan): Scans generated Docker image layers for CVEs (
trivy image). - Stage 7 (Helm Release): Deploys Helm chart releases into Kubernetes EKS cluster.
Important
Important SonarQube Token Setup
- Generate a token string (
sqp_...) in SonarQube (http://localhost:9000). - Add credential in Jenkins (
http://localhost:8080$\rightarrow$ Manage Jenkins$\rightarrow$ System$\rightarrow$ SonarQube servers) as Secret Text.
Tip
Trivy Vulnerability Threshold Tip
- Trivy automatically halts pipeline deployment if any
HIGHorCRITICALvulnerability severity is detected in container layers.
To connect your project to a Jenkins server running on port 8080:
Ensure the following tools are installed and configured on your machine:
- Node.js (v18 or v20): Required for building, linting, and workspace package management.
- Note: The updated
Jenkinsfileincludes auto-installation fallback for Node.js LTS in Unix/Linux Jenkins environments. - If running Jenkins via Docker container (
docker-compose.ci.yml), you can also pre-install Node.js in the running container:docker exec -u 0 -it medflow-jenkins apt-get update docker exec -u 0 -it medflow-jenkins apt-get install -y nodejs npm
- Note: The updated
- Docker Engine / Docker Desktop: Required to build and package production-ready images.
- Helm CLI: Kubernetes Package Manager CLI.
- Install on Windows via PowerShell:
winget install Helm.Helm
- Verify installation:
helm version
- Install on Windows via PowerShell:
- Trivy Security Scanner: Vulnerability & secret scanner for filesystem and container layers.
- Install on Windows via PowerShell:
winget install AquaSecurity.Trivy
- Verify installation:
trivy --version
- Install on Windows via PowerShell:
- Open your Jenkins console at http://localhost:8080.
- Click New Item on the top-left sidebar.
- Enter
MediCore360in the name box, select Pipeline, and click OK. - Scroll down to the Pipeline configuration panel:
- Definition: Choose
Pipeline script from SCMfrom the dropdown. - SCM: Select
Git. - Repository URL: Enter the GitHub repository URL:
https://github.com/SatyaCMD/MedFlow.git - Branch Specifier: Enter
*/main(or the branch you are actively developing on). - Script Path: Verify it is set to
Jenkinsfile.
- Definition: Choose
- Click Save.
3. Pipeline & SonarQube Server Setup (http://localhost:9000)
- Open your SonarQube dashboard at http://localhost:9000 (Log in with
admin/admin). - On the screen "How do you want to create your project?", click Create a local project at the bottom.
- Fill in the project parameters:
-
Project Display Name:
MedFlow -
Project Key:
MedFlow(matchessonar.projectKeyinsonar-project.properties) -
Main branch name:
main
-
Project Display Name:
- Click Next
$\rightarrow$ select Use global setting$\rightarrow$ click Create project. - Under How do you want to analyze your repository?, select Locally (or With Jenkins).
- Enter a Token Name (e.g.
jenkins-scanner-token)$\rightarrow$ click Generate$\rightarrow$ Copy the generated token string (sqp_...).
- Go to your Jenkins console at http://localhost:8080.
- Go to Manage Jenkins
$\rightarrow$ System$\rightarrow$ Scroll to SonarQube servers. - Click Add SonarQube, set:
-
Name:
SonarQubeServer -
Server URL:
http://127.0.0.1:9000 -
Server authentication token: Add Secret Text credential with your generated token
sqp_....
-
Name:
- Go to Manage Jenkins
$\rightarrow$ Tools$\rightarrow$ Scroll to SonarQube Scanner installations. - Click Add SonarQube Scanner, set the name to
SonarScanner, enable Install automatically, select versionSonarQube Scanner 8.1.0.6389, and click Save.
To launch your backend databases, monitoring (Prometheus & Grafana), Mailpit, and SonarQube quality scanner:
docker compose -f docker-compose.backend.yml up -d mongo redis mailpit prometheus grafana sonarqube| Service | Access URL | Default Credentials |
|---|---|---|
| MedFlow Web App | http://localhost:3000 | Workstation Login |
| Jenkins CI/CD | http://localhost:8080 | Local Windows Jenkins |
| Prometheus Telemetry | http://localhost:9090 | Public Metrics Scraper |
| Grafana Dashboards | http://localhost:3005 | User: admin | Pass: admin |
| SonarQube Scanner | http://localhost:9000 | User: admin | Pass: admin |
| Mailpit SMTP Portal | http://localhost:8025 | Web Mail Inspector |
5. Grafana Setup (http://localhost:3005)
- Open Grafana at http://localhost:3005 (Default login:
admin/admin). - Click Connections
$\rightarrow$ Data Sources on the left menu. - Click Add data source and select Prometheus.
- In the Prometheus server URL field, enter:
http://medflow-prometheus:9090. - Scroll down to the bottom and click Save & test. You will see a green badge: "Data source is working".
- In Grafana, click the
+icon at the top right$\rightarrow$ select Import dashboard. - Click Upload dashboard JSON file.
- Browse and select the file from your workspace:
infra/monitoring/medflow-dashboard.json. - Crucial Step: At the bottom dropdown under Prometheus, select your Prometheus data source (instead of leaving it default).
- Click Import.
- (If panels show "No data", click Dashboard Settings ⚙ at top right $\rightarrow$ select your Prometheus data source $\rightarrow$ click Save).
- You will now see live color graphs for HTTP Request Volume, p95 Response Latencies, Success vs Errors, and Process Memory & CPU Usage!
6. Prometheus Verification (http://localhost:9090)
- Open Prometheus at http://localhost:9090.
- Click Status on the top menu bar
$\rightarrow$ select Targets. - You will see your active scrape targets (
prometheusandmedflow-api) with state UP. - To test a metric query:
- In the search bar, type
upand click Execute. It will return1indicating services are healthy.
- In the search bar, type
7. Mailpit Email Inspector (http://localhost:8025)
- Open Mailpit at http://localhost:8025.
- This is your local SMTP mail server. Whenever you log in or request a 6-digit OTP code in MedFlow, the email will appear instantly in this inbox for verification testing.
Now that Helm, Trivy, Docker, Prometheus, and Grafana are ready, trigger your pipeline:
Run the following commands in your PowerShell terminal:
git add .
git commit -m "ci(jenkins): finalize pipeline stages, telemetry metrics, and devsecops tools"
git push origin main- Open Jenkins at http://localhost:8080.
- Click your MediCore360 project.
- Click Build Now on the left menu.
- All pipeline stages (Checkout, Dependencies, Lint, Build, SonarQube, Trivy, Docker, Trivy Container, Helm Deployment, and Prometheus/Grafana Monitoring) will run smoothly!
Tip
Green Upstash Cloud Redis Protocol
- TLS Double-S Protocol: When connecting to Upstash, use
rediss://(with doubles) to enable TLS encryption. - Connection String:
REDIS_URI=rediss://default:PASSWORD@your-db.upstash.io:6379.
Important
Production Driver Capabilities (ioredis)
- MedFlow's backend driver in
apps/api/src/lib/redis.tsusesioredis, which automatically handles SSL/TLS authentication, retry strategies, 5-min OTP TTL, and session management.
To switch from local Redis (redis://localhost:6380) to Production Redis, update line 10 in your .env file with your production Redis connection URI.
Here are the 3 standard options depending on your cloud provider:
Upstash provides a free, serverless Cloud Redis with TLS encryption:
- Create a free database at upstash.com.
- Copy your connection string and paste it into your
.envfile:Note theREDIS_URI=rediss://default:YOUR_UPSTASH_PASSWORD@your-db-name.upstash.io:6379
rediss://protocol (with doubles) which enables TLS/SSL encryption for cloud connections.
If using Redis Cloud (Redis Enterprise):
- Create a free database at redis.com/try-free.
- Copy your endpoint host, port, and password.
- Update
.env:REDIS_URI=redis://default:YOUR_REDIS_CLOUD_PASSWORD@redis-12345.c1.cloud.redislabs.com:12345
If deploying Redis on your cloud server with a password:
REDIS_URI=redis://:YOUR_SECURE_PASSWORD@your-production-server-ip:6379MedFlow's backend driver in apps/api/src/lib/redis.ts uses ioredis. As soon as you paste your production REDIS_URI into .env, ioredis automatically:
- Handles SSL/TLS encryption (
rediss://) - Authenticates cloud passwords
- Manages connection retry strategies
- Executes all session lockouts, failed login counters, and OTP caching in production!
We created infra/terraform/s3_kyc.tf which provisions a HIPAA-compliant AWS S3 Bucket (medflow-kyc-documents-production) with KMS Server-Side Encryption and IAM upload policies.
# 1. Navigate to terraform directory
cd infra/terraform
# 2. Initialize AWS providers
terraform init
# 3. Preview AWS infrastructure plan
terraform plan
# 4. Deploy to AWS Cloud
terraform applyPort 4000 is occupied because pnpm dev (the local Express API process) is running on your host machine in your terminal. Docker Compose tries to bind container port 4000 to host port 4000, causing Windows socket bind error bind: Only one usage of each socket address is normally permitted.
- Option A (If running dev server locally via
pnpm dev): Start database containers in Docker without the duplicate API container:docker compose -f docker-compose.dev.yml up -d mongo redis mailpit
Warning
Host Socket Error Diagnosis
- Port
4000is occupied becausepnpm devis running on host machine. Start databases in Docker without duplicate API containers:docker compose -f docker-compose.dev.yml up -d mongo redis mailpit
To run MedFlow in production so it can handle millions/billions of requests with high availability and sub-50ms latency, follow these step-by-step execution instructions:
flowchart TD
Client["Client Requests / Web Dashboard / Mobile"] --> ALB["AWS ALB / NGINX Load Balancer"]
subgraph ComputeLayer ["Horizontal Container / Cluster Layer"]
ALB --> WorkerNode1["Node.js API Container - Process 1..N"]
ALB --> WorkerNode2["Node.js API Container - Process 1..N"]
ALB --> WorkerNode3["Node.js API Container - Process 1..N"]
end
subgraph DataCachingLayer ["Data & Caching Layer"]
WorkerNode1 <-->|Read/Write Cache| RedisCluster[("Redis Sentinel / Cluster")]
WorkerNode1 <-->|Read Secondary / Write Primary| MongoReplica[("MongoDB Replica Set")]
WorkerNode2 <--> RedisCluster
WorkerNode2 <--> MongoReplica
WorkerNode3 <--> RedisCluster
WorkerNode3 <--> MongoReplica
end
subgraph AsyncProcessingLayer ["Async Processing Layer"]
WorkerNode1 -->|Enqueue PDF / Email / S3 Jobs| BullMQ["BullMQ Job Queues (Redis-backed)"]
WorkerNode2 -->|Enqueue Jobs| BullMQ
BullMQ --> WorkerPool1["Background Worker Service 1"]
BullMQ --> WorkerPool2["Background Worker Service 2"]
WorkerPool1 --> AWS_S3["AWS S3 Bucket"]
WorkerPool1 --> SMTP["SMTP / AWS SES Email"]
WorkerPool1 --> PDF["PDFKit Engine"]
end
Before launching cluster processes, compile TypeScript across all workspace packages and applications:
pnpm run buildSpin up high-performance database and caching containers using docker-compose.prod.yml:
docker-compose -f docker-compose.prod.yml up -d mongo redis mailpitImportant
MongoDB Replica Set Verification
- Ensure your MongoDB connection URI points to a replica set (
replicaSet=rs0) so read queries are automatically offloaded to secondary nodes (readPreference=secondaryPreferred).
Choose Option A (Docker Multi-Container) or Option B (PM2 Bare-Metal/VM Cluster Mode):
Scale the API to 4+ parallel container instances behind the NGINX Reverse Proxy:
# Spin up 4 API container replicas dynamically
docker-compose -f docker-compose.prod.yml up -d --scale api=4 --build
# Verify running containers
docker-compose -f docker-compose.prod.yml psUse PM2 to spawn 1 worker per CPU core on your server host using ecosystem.config.cjs:
# Start PM2 in cluster mode (auto-detects all available CPU cores)
npx pm2 start ecosystem.config.cjs
# Check cluster status and live resource usage
npx pm2 status
npx pm2 monitTo keep API response times < 50ms, heavy synchronous tasks (PDF prescription rendering, AWS S3 uploads, SMTP email dispatching) are offloaded to BullMQ background workers:
# Start BullMQ worker processes separately from API HTTP instances
NODE_ENV=production node apps/api/dist/workers/prescriptionWorker.jsTip
Worker Process Isolation
- Running background queue workers as independent processes ensures CPU rendering spikes never degrade HTTP API request latency.
Run headless distributed load testing using the k6 script (tests/load/api-load-test.js):
-
Install k6 (if not already installed):
- Windows (winget):
winget install k6 - macOS:
brew install k6 - Linux:
sudo apt-get install k6
- Windows (winget):
-
Execute high-concurrency load test (2,000+ Virtual Users / ~20,000 requests/sec):
k6 run tests/load/api-load-test.js
Target Threshold Benchmark Results:
http_req_duration (p95): < 50mshttp_req_failed: < 0.01%
Monitor server health, connection pools, and memory footprint during peak traffic:
# Docker Container Resource Usage (CPU, Memory, Network I/O)
docker stats
# PM2 Real-Time Process Monitor
npx pm2 monit
# Tail live application logs
npx pm2 logs medflow-api| Phase | Milestone | Tools / Technology | Target Metric / SLA |
|---|---|---|---|
| Phase 1 | Node.js Cluster + NGINX LB | PM2, Docker Compose (deploy.replicas), NGINX |
100% CPU core utilization across nodes |
| Phase 2 | DB Compound Indexes & Redis Caching | MongoDB Indexing, Redis Cache-Aside | Database query time < 10ms |
| Phase 3 | Offload PDF, S3, SMTP to Workers | BullMQ, Redis Queues, Isolated Worker Process | API HTTP response time < 50ms |
| Phase 4 | Headless Cloud Load Testing | k6, Newman, Grafana Dashboard | Zero request drops at 10,000+ RPS |