A modern microservices architecture implementation using NestJS, Kafka, and Event Sourcing patterns. This project demonstrates how to build scalable and maintainable microservices using CQRS and Event Sourcing principles.
EVENT-DRIVEN-NESTJS/
├── api-gateway/ # API Gateway service
├── inventory-service/ # Inventory management service
├── order-service/ # Order management service
│ ├── src/
│ │ ├── api/
│ │ │ └── controllers/ # API Controllers
│ │ ├── application/
│ │ │ ├── handlers/ # Command and Event Handlers
│ │ │ └── sagas/ # Sagas for process management
│ │ ├── domain/
│ │ │ ├── aggregates/ # Domain Aggregates
│ │ │ ├── commands/ # Command Definitions
│ │ │ ├── events/ # Event Definitions
│ │ │ └── interfaces/ # Domain Interfaces
│ │ └── infrastructure/
│ │ ├── persistence/ # Database and Event Store
│ │ └── messaging/ # Kafka Integration
├── payment-service/ # Payment processing service
├── shared-lib/ # Shared code and utilities
│ ├── src/
│ │ ├── commands/ # Shared Commands
│ │ ├── events/ # Shared Events
│ │ ├── interfaces/ # Shared Interfaces
│ │ └── utils/ # Utility Functions
└── docker-compose.yml # Docker composition for services
- ✨ Event-Driven Architecture
- 🎯 CQRS Pattern Implementation
- 📊 Event Sourcing
- 🚀 Microservices Architecture
- 🔄 Apache Kafka Integration
- 🛡️ TypeScript Support
- 🐳 Docker Support
- Create Order API with Event Sourcing
- Command and Event Handlers
- Database persistence with TypeORM
- Event Store implementation
- Kafka integration for event publishing
- Node.js (v18 or higher)
- Docker and Docker Compose
- MySQL
- Apache Kafka
- Clone the repository:
git clone [your-repository-url]
cd event-driven-nestjs
- Install dependencies:
# Install shared library dependencies
cd shared-lib
npm install
# Install service dependencies
cd ../order-service
npm install
- Start the infrastructure services:
docker-compose up -d
- Run the services:
# Start order service
cd order-service
npm run start:dev
POST /api/orders
- Create a new order{ "userId": "string", "items": [ { "id": "string", "quantity": number, "price": number } ] }
This project follows these architectural patterns:
- Event Sourcing: All changes to the domain objects are stored as a sequence of events
- CQRS: Separation of command and query responsibilities
- Microservices: Distributed system with separate services for different business capabilities
- Framework: NestJS
- Message Broker: Apache Kafka
- Database: MySQL
- ORM: TypeORM
- API Documentation: Swagger (coming soon)
- Container: Docker
- Language: TypeScript
- Inventory Service Implementation
- Payment Service Integration
- API Gateway Implementation
- Service Discovery
- Distributed Tracing
- API Documentation
- Unit and Integration Tests
- CI/CD Pipeline
# Start development server
npm run start:dev
# Build the project
npm run build
# Run tests (coming soon)
npm run test
The project includes Docker support for easy development and deployment:
# Start all services
docker-compose up -d
# Stop all services
docker-compose down
# View logs
docker-compose logs -f
The services are containerized using Docker. Use the following commands to run the entire system:
- Start the services:
docker-compose up -d
- Check service status:
docker-compose ps
- View service logs:
docker-compose logs -f [service-name]
- Stop the services:
docker-compose down
Each service requires specific environment variables. Create a .env
file in each service directory:
# Database
DB_HOST=mysql
DB_PORT=3306
DB_USER=user
DB_PASS=password
DB_NAME=eventstore
# Kafka
KAFKA_BROKER=kafka:9092
KAFKA_GROUP_ID=order-service-group
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Reza Masoomi Niaragh - [email protected]
Project Link: https://github.com/rezamasoomin/event-driven-nestJs