A modern microservices-based e-commerce application demonstrating event-driven architecture with FastAPI, Redis, and React. This project showcases real-world patterns including asynchronous message processing, inventory management, and inter-service communication.
This application follows a microservices architecture with three main components:
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Frontend UI │ │ Inventory API │ │ Payment API │
│ (React) │ │ (FastAPI) │ │ (FastAPI) │
│ │ │ │ │ │
│ • Product List │◄──►│ • CRUD Products │ │ • Create Orders │
│ • Order Mgmt │ │ • Inventory Mgmt│ │ • Process Payments│
│ • User Interface│ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │
│ ┌─────────────────┐ │
└───►│ Redis Streams │◄┘
│ │
│ • Event Queue │
│ • Data Storage │
│ • Pub/Sub │
└─────────────────┘
- Inventory Service (
inventory/): Manages product catalog and stock levels - Payment Service (
payment/): Handles order processing and payment workflows - Frontend UI (
inventory-ui/): React-based user interface - Redis: Serves as both database and message broker for inter-service communication
- 🔄 Event-Driven Architecture: Asynchronous communication using Redis Streams
- 📦 Inventory Management: Real-time stock updates and product management
- 💰 Order Processing: Complete order lifecycle with automatic fee calculation
- 🔄 Background Processing: Async order completion simulation
- 🔒 Error Handling: Automatic refund mechanism for failed inventory updates
- 🌐 CORS-Enabled: Full frontend-backend integration support
- 🎯 RESTful APIs: Clean, well-structured API endpoints
- Python 3.8+
- Node.js 16+
- Redis Server
- Git
-
Clone the repository
git clone https://gitlab.com/ValentineOO/fastapi-redis-react-microservice.git cd fastapi-redis-react-microservice -
Set up Redis
# Using Docker (recommended) docker run -d -p 6379:6379 --name redis redis:latest # Or install locally (Ubuntu/Debian) sudo apt-get install redis-server redis-server
-
Configure Environment Variables
Create
.env.inventoryin theinventory/directory:REDIS_HOST=localhost REDIS_PORT=6379 REDIS_USERNAME= REDIS_PASSWORD=
Create
.env.paymentin thepayment/directory:REDIS_HOST=localhost REDIS_PORT=6379 REDIS_USERNAME= REDIS_PASSWORD= PRODUCT_SERVICE_URL=http://localhost:8000
-
Start the Inventory Service
cd inventory pip install -r requirements.txt uvicorn main:app --host 0.0.0.0 --port 8000 --reload -
Start the Payment Service
cd payment pip install -r requirements.txt uvicorn main:app --host 0.0.0.0 --port 8001 --reload -
Start Redis Consumers (in separate terminals)
# Inventory consumer cd inventory python consumer.py # Payment consumer (if exists) cd payment python consumer.py
-
Start the Frontend
cd inventory-ui npm install npm start
The application will be available at:
- Frontend: http://localhost:3000
- Inventory API: http://localhost:8000
- Payment API: http://localhost:8001
| Method | Endpoint | Description | Request Body |
|---|---|---|---|
| GET | /products |
List all products | - |
| POST | /products |
Create a new product | {"name": str, "price": float, "quantity": int} |
| GET | /products/{pk} |
Get specific product | - |
| DELETE | /products/{pk} |
Delete a product | - |
| Method | Endpoint | Description | Request Body |
|---|---|---|---|
| GET | /orders/{pk} |
Get order details | - |
| POST | /orders |
Create a new order | {"id": str, "quantity": int} |
Create a Product:
curl -X POST "http://localhost:8000/products" \
-H "Content-Type: application/json" \
-d '{"name": "Laptop", "price": 999.99, "quantity": 10}'Place an Order:
curl -X POST "http://localhost:8001/orders" \
-H "Content-Type: application/json" \
-d '{"id": "product_id_here", "quantity": 2}'- Order Creation: User places order via frontend → Payment service
- Product Validation: Payment service fetches product from Inventory service
- Order Processing: Order saved with "pending" status, background task triggered
- Order Completion: After 5 seconds, order status → "completed"
- Event Publishing:
order_completedevent published to Redis Stream - Inventory Update: Inventory consumer processes event, updates stock
- Error Handling: If inventory update fails → refund event created
{
"id": "string",
"name": "string",
"price": float,
"quantity": int
}{
"id": "string",
"product_id": "string",
"price": float,
"fee": float, # 20% of product price
"total": float, # price + fee
"quantity": int,
"status": "string" # pending, completed, refunded
}- FastAPI: Modern, fast web framework for Python APIs
- Redis: In-memory data structure store for caching and messaging
- Redis-OM: Object mapping library for Redis
- Uvicorn: ASGI server for Python web applications
- Python-dotenv: Environment variable management
- React: JavaScript library for building user interfaces
- Create React App: Toolchain for React development
- SonarQube: Code quality and security analysis
- Python typing: Type hints for better code quality
fastapi-redis-react-microservice/
├── inventory/ # Inventory microservice
│ ├── main.py # FastAPI app and routes
│ ├── consumer.py # Redis stream consumer
│ ├── requirements.txt # Python dependencies
│ └── .env.inventory # Environment variables
├── payment/ # Payment microservice
│ ├── main.py # FastAPI app and routes
│ ├── consumer.py # Redis stream consumer
│ ├── requirements.txt # Python dependencies
│ └── .env.payment # Environment variables
├── inventory-ui/ # React frontend
│ ├── src/
│ │ ├── components/ # React components
│ │ └── App.js # Main app component
│ └── package.json # Node.js dependencies
└── README.md # This file
# Backend tests (when implemented)
cd inventory && python -m pytest
cd payment && python -m pytest
# Frontend tests
cd inventory-ui && npm testThis project uses SonarQube for code quality analysis. The current quality gate status is shown in the badge above.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow PEP 8 for Python code
- Use meaningful commit messages
- Add tests for new features
- Update documentation as needed
This project is licensed under the MIT License - see the LICENSE file for details.
- Valentine OO - Initial work - ValentineOO
- FastAPI community for excellent documentation
- Redis team for powerful data structures
- React team for the amazing UI library
- All contributors who helped improve this project
If you have any questions or need help:
- Create an issue
- Check the documentation
- Contact the maintainer
⭐ Star this repository if you found it helpful!