docker compose up --build
Due to the possibility of the invoice service crashing or the need to scale it as demanded, we decided to extract the CronJob
role into this separate service.
The Invoice Service is a microservice-based application designed to manage invoices. It is built using NestJS, MongoDB for data storage, and RabbitMQ for emitting daily sales reports via email. The project is containerized using Docker and orchestrated using Docker Compose.
The project consists of the following microservices:
-
Invoice Service: REST API to manage invoices.
- Depends on: MongoDB
-
Report Generator: Generates daily sales reports and pushes them to RabbitMQ.
- Depends on: MongoDB, RabbitMQ
-
Email Sender: Subscribes to the
daily_sales_report
queue and sends emails.- Depends on: RabbitMQ, Email Provider
-
MongoDB: Storage for invoices.
-
RabbitMQ: Hosts the
daily_sales_report
queue.
Please refer to requests.http to see possible API requests.
Running commands locally is an option, but using Docker ensures consistency.
# cd to project root then run:
docker run -it --mount type=bind,src=./invoice-service,dst=/app node:23 bash
# Ensure you are inside the /app directory before running these commands:
cd /app
# To run all tests
npm test
# To run unit tests only
npm run test:unit
# To run integration tests only
npm run test:integration
# To install a specific package
npm install [package]
# To inspect inserted invoices
docker compose exec -it mongo mongosh
use invoice
db.invoices.find()
docker compose exec -it rabbitmq bash
# list active queues
rabbitmqctl list_queues
# list messages in a specific queue
rabbitmqadmin get queue=daily_sales_report
Because CronJobs run once a day, to test salesReportCron, uncomment @Interval
and comment @Cron
.
- Authentication & Authorization: Implement authentication and authorization for the Invoice Service.
- MongoDB & RabbitMQ Authentication: Enable authentication for MongoDB and RabbitMQ to prevent unauthorized access.
- Input Validation: Ensure strict validation for API inputs to prevent injection attacks and data corruption.
- Helm Charts: Create Helm charts for each service to simplify Kubernetes deployments.
- CI/CD Pipelines: Automate build, test, and deployment processes for faster iteration.
- Load & Stress Testing: Perform load and stress tests to ensure the system can handle expected traffic.
- Optimize Dockerfiles: Use multi-stage builds and prebuilt NestJS applications (
npm build
,npm start:prod
) to improve container efficiency.
- Observability Tools: Use Prometheus and Loki for monitoring resource usage, logs, and service health.
- Message Acknowledgment: Implement manual acknowledgment for RabbitMQ messages to prevent data loss.
- Crash Recovery: Ensure services restart automatically after crashes and handle failures gracefully.