A robust, production-grade microservices system built with NestJS, RabbitMQ, and PostgreSQL, demonstrating advanced distributed systems patterns.
This system implements an Orchestration-based Saga Pattern to manage distributed transactions across three core microservices.
- Order Service: The orchestrator. manages order lifecycle and coordinates the saga.
- Payment Service: Handles transaction processing and refunds.
- Inventory Service: Manages stock reservation and release.
- Common Library: Shared types, entities (
IdempotencyEntity), and event contracts.
- Order Service: Creates order (PENDING) -> Publishes
order.created. - Payment Service: Receives
order.created-> Processes payment -> Publishespayment.processedorpayment.failed. - Inventory Service: Receives
payment.processed-> Reserves stock -> Publishesinventory.reservedorinventory.reservation_failed. - Order Service: Receives
inventory.reserved-> Completes order (COMPLETED).
Compensation Logic:
- If Inventory fails, a
payment.refundis triggered. - If an order fails,
order.failedis broadcasted to release any reserved stock.
- Distributed Idempotency: Prevents duplicate message processing using a shared idempotency layer.
- Dead Letter Queues (DLQ): Failed messages are automatically routed to DLX and stored in DLQs for analysis.
- Manual Acknowledgements: Ensures "At-Least-Once" delivery; messages are only acknowledged after successful DB commit.
- Saga Timeout Worker: Automatically resolves transactions stuck in intermediate states.
- Structured Logging: JSON logs via Pino, optimized for ELK/CloudWatch.
- Distributed Tracing: End-to-end traceability using
x-correlation-idpropagated across HTTP and RabbitMQ. - Swagger Documentation: Interactive API docs available at
/api/docs(Order Service). - Health Checks: Real-time monitoring of DB and RabbitMQ connectivity via
/health.
- Framework: NestJS
- Database: PostgreSQL (TypeORM)
- Messaging: RabbitMQ
- Logging: Pino
- Monitoring: Terminus
- Documentation: Swagger
- Node.js (v18+)
- PostgreSQL
- RabbitMQ
- Clone the repository.
- Install dependencies in each service:
cd order-service && npm install cd ../payment-service && npm install cd ../inventory-service && npm install
- Configure
.envfiles for each service (examples provided in each directory). - Run the services:
npm run start:dev
- Phase 1: Foundation (Core Microservices)
- Phase 2: Saga Implementation (Orchestration & Compensation)
- Phase 3: Resilience (DLQ, Retries, Timeouts)
- Phase 4: Observability (Logging, Tracing, Health)