A robust demonstration of an Event-Driven Microservices Architecture built with Python, FastAPI, RabbitMQ, and Docker.
This project showcases how independent services communicate asynchronously to handle e-commerce workflows like user registration, order placement, and real-time notifications.
The system consists of three independent microservices and a message broker. They communicate through a mix of synchronous REST APIs and asynchronous event publishing.
graph TD
User((User)) -- HTTP POST/GET --> UserSvc[User Service]
User -- HTTP POST --> OrderSvc[Order Service]
OrderSvc -- Publish Event --> RMQ[RabbitMQ]
RMQ -- Consume Message --> NotifSvc[Notification Service]
subgraph "Infrastructure"
RMQ
end
subgraph "Internal Storage"
UserSvc --- UserDB[(SQLite)]
OrderSvc --- OrderDB[(SQLite)]
end
- Role: Manages user profiles and identity.
- Tech: FastAPI, Async SQLAlchemy, SQLite.
- Key Feature: Validates email uniqueness and handles user registration.
- Role: Handles the lifecycle of an order.
- Tech: FastAPI, Async SQLAlchemy, RabbitMQ (aio-pika).
- Key Feature: When an order is successfully created, it publishes an
order_createdevent to RabbitMQ.
- Role: Reacts to system events to "notify" users.
- Tech: Python, aio-pika.
- Key Feature: Runs as a background worker. It consumes messages from the
order_eventsqueue and logs details to the console.
- Framework: FastAPI (High-performance Python web framework)
- Message Broker: RabbitMQ (Reliable asynchronous messaging)
- ORM: SQLAlchemy 2.0 (Async support)
- Database: SQLite (Local file-based for simplicity)
- Containerization: Docker & Docker Compose
- Async Client: aio-pika (RabbitMQ) & httpx (Testing)
- Docker Desktop
- Python 3.12+ (Only if running tests or development locally)
-
Clone the repository:
git clone https://github.com/hadithedetonator/ecommerce-microservices.git cd ecommerce-microservices -
Spin up the infrastructure:
docker-compose up --build
-
Verify running containers:
docker-compose ps
-
Access RabbitMQ Dashboard:
- URL: http://localhost:15672
- Credentials:
guest/guest
To verify that the services are working together correctly, follow this sequence:
I have provided an async script that simulates the entire user-to-order flow.
# Setup virtual environment
python3 -m venv tests/venv
source tests/venv/bin/activate
pip install -r tests/requirements.txt
# Run the test
python tests/test_flow.pyYou can also test using curl or Postman:
-
Step 1: Create a User
curl -X POST http://localhost:5001/users \ -H "Content-Type: application/json" \ -d '{"name": "John Doe", "email": "john@example.com"}'
-
Step 2: Place an Order
curl -X POST http://localhost:5002/orders \ -H "Content-Type: application/json" \ -d '{"user_id": 1, "product_name": "MacBook Pro", "amount": 2499.99}'
-
Step 3: Check Notifications Watch the docker logs to see the event being consumed:
docker-compose logs -f notification_service
Expected Output:
[!] NOTIFICATION: Order #1 created for User 1: MacBook Pro ($2499.99)
.
├── docker-compose.yml # Orchestration
├── user_service/ # User Management API
├── order_service/ # Order Management API (Publisher)
├── notification_service/ # Event Consumer (Worker)
├── tests/ # Integration test suite
└── readme.md # Documentation
Feel free to open issues or submit pull requests to enhance the functionality!