A demonstration of microservices architecture using FastAPI and Redis for inter-service communication.
This project implements a simple e-commerce flow with two microservices:
- Inventory Service - Manages products (create, read, delete)
- Payment Service - Handles order creation and processing
Services communicate asynchronously via Redis Streams.
┌─────────────┐ ┌─────────────┐
│ Inventory │ │ Payment │
│ (port 8000)│ │ (port 8001) │
└──────┬──────┘ └──────┬──────┘
│ │
└─────────┬─────────┘
│
┌─────┴─────┐
│ Redis │
└───────────┘
Flow:
- Client creates an order via Payment service
- Payment service fetches product from Inventory service
- Order is created with "pending" status
- Background task marks order "completed" after 5 seconds
- Inventory decreases product quantity via Redis Stream
- On refund, quantity is restored
- Python 3.8+
- Redis (local or cloud instance)
# Inventory service
cd inventory
pip install -r requirements.txt
# Payment service
cd payment
pip install fastapi uvicorn redis-om python-dotenv requestsCreate a .env file in the root directory:
HOST=your-redis-host
PORT=your-redis-port
PASSWORD=your-redis-passwordStart each component in separate terminals:
# Terminal 1 - Inventory API
cd inventory
uvicorn main:app --reload --port 8000
# Terminal 2 - Payment API
cd payment
uvicorn main:app --reload --port 8001
# Terminal 3 - Inventory Consumer
cd inventory
python consumer.py
# Terminal 4 - Payment Consumer
cd payment
python consumer.py| Method | Endpoint | Description |
|---|---|---|
| GET | /products |
List all products |
| POST | /products |
Create a product |
| GET | /products/{pk} |
Get product by ID |
| DELETE | /products/{pk} |
Delete a product |
| Method | Endpoint | Description |
|---|---|---|
| GET | /orders/{pk} |
Get order by ID |
| POST | /orders |
Create a new order |
curl -X POST http://localhost:8001/orders \
-H "Content-Type: application/json" \
-d '{"id": "PRODUCT_ID", "quantity": 1}'.
├── inventory/
│ ├── main.py # FastAPI app & product models
│ └── consumer.py # Redis consumer for order events
├── payment/
│ ├── main.py # FastAPI app & order models
│ └── consumer.py # Redis consumer for refund events
└── .env # Environment variables
- FastAPI - Modern Python web framework
- Redis - In-memory data store
- redis-om - Redis object mapper for Python
MIT