Skip to content

Repository files navigation

E-Commerce Microservices Platform

A production-grade e-commerce platform built with FastAPI microservices and Kubernetes orchestration. Features comprehensive observability, resilience patterns, and event-driven architecture.

πŸ—οΈ Architecture

Microservices

  • API Gateway - Smart routing, authentication, rate limiting, caching, circuit breaker, load balancing
  • User Service - User management, authentication, JWT token generation
  • Product Service - Product catalog, inventory management, gRPC API
  • Order Service - Order orchestration, coordinates product and payment services
  • Payment Service - Payment processing, transaction management
  • Notification Service - Event-driven notifications via Kafka consumers

Tech Stack

Component Technology
Backend FastAPI, Python 3.x
Databases PostgreSQL (per service)
Caching Redis
Message Queue Apache Kafka + Zookeeper
Communication REST, gRPC
Monitoring Prometheus, Grafana, ELK Stack (Elasticsearch, Logstash, Kibana, Fluent Bit)
Orchestration Kubernetes, Docker Compose
Load Testing Locust

πŸš€ Key Features

API Gateway Capabilities

  • Authentication & Authorization - JWT-based security
  • Rate Limiting - Token bucket algorithm
  • Response Caching - Redis-backed with intelligent cache keys
  • Circuit Breaker - Prevents cascade failures
  • Load Balancing - Round-robin with health checks
  • Service Discovery - Dynamic service registry
  • Request Metrics - Prometheus instrumentation

Microservices Patterns

  • Event-driven architecture with Kafka
  • Inter-service communication via REST and gRPC
  • Database per service pattern
  • Health checks and graceful degradation
  • Distributed tracing and logging

Observability Stack

  • Metrics: Prometheus scraping all services
  • Visualization: Grafana dashboards
  • Logging: Centralized logs with ELK stack
  • Tracing: Request/response logging

πŸ“¦ Project Structure

ecom-app/
β”œβ”€β”€ api-gateway/          # API Gateway with advanced patterns
β”œβ”€β”€ user-service/         # User management & auth
β”œβ”€β”€ product-service/      # Product catalog + gRPC
β”œβ”€β”€ order-service/        # Order orchestration
β”œβ”€β”€ payment-service/      # Payment processing
β”œβ”€β”€ notification-service/ # Kafka-based notifications
β”œβ”€β”€ k8s/                 # Kubernetes manifests
β”œβ”€β”€ monitoring/          # Prometheus, Grafana, ELK configs
β”œβ”€β”€ locust/              # Load testing scripts
β”œβ”€β”€ data/                # Persistent data volumes
└── docker-compose.yml   # Local development setup

πŸ› οΈ Getting Started

Prerequisites

  • Docker & Docker Compose
  • Python 3.9+
  • Kubernetes cluster (for K8s deployment)

Local Development with Docker Compose

  1. Clone the repository

    git clone <repository-url>
    cd ecom-app
  2. Start all services

    docker-compose up -d
  3. Verify services are running

    docker-compose ps

Service Ports

Service Port URL
API Gateway 8000 http://localhost:8000
User Service 8001 http://localhost:8001
Product Service 8002 http://localhost:8002
Payment Service 8003 http://localhost:8003
Order Service 8004 http://localhost:8004
Notification Service 8005 http://localhost:8005
Prometheus 9090 http://localhost:9090
Grafana 3000 http://localhost:3000
Kafka UI 8080 http://localhost:8080
Kibana 5601 http://localhost:5601

API Documentation

Each service exposes interactive API docs:

🎯 Usage Examples

1. Create a User

curl -X POST "http://localhost:8000/api/users/register" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "john_doe",
    "email": "john@example.com",
    "password": "SecurePass123",
    "full_name": "John Doe"
  }'

2. Login and Get Token

curl -X POST "http://localhost:8000/api/users/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "john_doe",
    "password": "SecurePass123"
  }'

3. Create a Product (with auth token)

curl -X POST "http://localhost:8000/api/products/" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Laptop",
    "description": "High-performance laptop",
    "price": 999.99,
    "stock_quantity": 50,
    "sku": "LAPTOP-001"
  }'

4. Place an Order

curl -X POST "http://localhost:8000/api/orders/" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "items": [
      {
        "product_id": 1,
        "quantity": 2,
        "price": 999.99
      }
    ],
    "payment_method": "credit_card"
  }'

☸️ Kubernetes Deployment

Deploy to Kubernetes

# Apply all manifests
kubectl apply -f k8s/

# Check deployment status
kubectl get pods
kubectl get services
kubectl get ingress

Access Services via Ingress

Configure your DNS or /etc/hosts to point to your cluster IP:

<cluster-ip> ecom.local

Access: http://ecom.local

πŸ“Š Monitoring & Observability

Prometheus

  • Access: http://localhost:9090
  • Metrics from all services automatically scraped
  • Custom business metrics (orders, payments, etc.)

Grafana

  • Access: http://localhost:3000
  • Default credentials: admin/admin
  • Pre-configured dashboards for each service

Elasticsearch & Kibana

  • Elasticsearch: http://localhost:9200
  • Centralized logging from all services
  • Search and analyze logs via Elasticsearch queries

Kafka UI

  • Access: http://localhost:8080
  • Monitor Kafka topics, messages, and consumer groups
  • View order and notification events in real-time

πŸ§ͺ Load Testing

Run load tests with Locust:

cd locust
pip install -r requirements.txt
locust -f locustfile.py --host=http://localhost:8000

Access Locust UI: http://localhost:8089

πŸ”§ Development

Running Individual Services

Backend Service

cd <service-name>
pip install -r requirements.txt
uvicorn main:app --reload --port <port>

Frontend

cd frontend
npm install
npm run dev

Environment Variables

Each service can be configured via environment variables. Check config.py in each service for available options.

Key variables:

  • DATABASE_URL - PostgreSQL connection string
  • REDIS_URL - Redis connection string
  • KAFKA_BOOTSTRAP_SERVERS - Kafka brokers
  • JWT_SECRET - Secret key for JWT tokens

πŸ›οΈ Architecture Patterns

Resilience Patterns

  • Circuit Breaker: Prevents cascading failures
  • Rate Limiting: Protects services from overload
  • Health Checks: Automatic service monitoring
  • Retry Logic: Automatic retry with exponential backoff

Data Patterns

  • Database per Service: Isolated data stores
  • Event Sourcing: Kafka for event streaming
  • Caching: Redis for frequently accessed data

Communication Patterns

  • API Gateway: Single entry point
  • Service Registry: Dynamic service discovery
  • gRPC: High-performance inter-service calls
  • Event-Driven: Asynchronous processing via Kafka

πŸ“ API Endpoints

User Service

  • POST /register - Register new user
  • POST /login - User login
  • GET /users/me - Get current user
  • PUT /users/{id} - Update user

Product Service

  • POST /products/ - Create product
  • GET /products/ - List products
  • GET /products/{id} - Get product
  • PUT /products/{id} - Update product
  • DELETE /products/{id} - Delete product
  • POST /products/{id}/stock - Update stock

Order Service

  • POST /orders/ - Create order
  • GET /orders/ - List orders
  • GET /orders/{id} - Get order
  • PUT /orders/{id}/cancel - Cancel order

Payment Service

  • POST /payments/ - Create payment
  • GET /payments/{id} - Get payment
  • PUT /payments/{id}/status - Update payment status

πŸ” Security

  • JWT-based authentication
  • Password hashing with bcrypt
  • CORS configuration
  • Rate limiting per endpoint
  • Input validation with Pydantic
  • SQL injection prevention via ORM

πŸ“ˆ Performance

  • Redis caching reduces database load
  • gRPC for fast inter-service communication
  • Connection pooling for databases
  • Async/await for non-blocking I/O
  • Load balancing across service instances

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License.

πŸ™‹ Support

For issues and questions:

  • Create an issue in the repository
  • Check existing documentation in each service
  • Review API docs at /docs endpoints

Built with ❀️ using FastAPI, and Kubernetes

About

An e-commerce built following standard microservices architecture

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages