Skip to content

vaibhavy06/scalable-api-system

Repository files navigation

🚀 Scalable Microservices Architecture

Python FastAPI PostgreSQL Redis RabbitMQ Docker Kubernetes License

A production-ready, highly scalable microservices backend built with FastAPI and Python. This architecture employs modern asynchronous programming, event-driven communication, API Gateway patterns, and robust containerization suitable for large-scale enterprise applications.


🌟 Key Features

  • Domain-Driven Design (DDD): Divided into logical bounded contexts (Auth, User, Product, Order, Notification).
  • API Gateway Pattern: Centralized entry point dynamically routing to backend microservices utilizing asynchronous HTTPX.
  • Asynchronous Stack: Leveraging asyncpg for PostgreSQL, redis.asyncio for caching, and aio-pika for RabbitMQ.
  • Hybrid Communication:
    • Synchronous: RESTful API via HTTPX through the Gateway.
    • Asynchronous: Event-driven message brokering via RabbitMQ for decoupled operations.
  • Resilient Infrastructure: Centralized shared core library for Authentication, Database, Messaging, and Caching.
  • Automated Migrations: SQLAlchemy and Alembic configured for zero-downtime database schema upgrades.
  • Kubernetes Ready: Out-of-the-box support for Deployments, Services, ConfigMaps, and Ingress routing.

🏗️ Architecture Diagram

graph TD
    Client([Client / Frontend]) -->|HTTP Requests| Gateway[API Gateway]
    
    subgraph Synchronous Layer
        Gateway -->|HTTPX Route| Auth[Auth Service]
        Gateway -->|HTTPX Route| User[User Service]
        Gateway -->|HTTPX Route| Product[Product Service]
        Gateway -->|HTTPX Route| Order[Order Service]
    end
    
    subgraph Data Layer
        Auth --> DB_Auth[(PostgreSQL)]
        User --> DB_User[(PostgreSQL)]
        User --> Redis_Cache[(Redis)]
        Product --> DB_Product[(PostgreSQL)]
        Order --> DB_Order[(PostgreSQL)]
    end

    subgraph Asynchronous Layer
        User -->|Publish Event| RMQ[[RabbitMQ]]
        Order -->|Publish Event| RMQ
        RMQ -->|Consume Event| Notification[Notification Service]
    end
Loading

📂 Repository Structure

.
├── auth-service/          # JWT Authentication and Authorization provider
├── user-service/          # User Management, profiles, and CRUD logic
├── product-service/       # Product Catalog and Inventory tracking
├── order-service/         # Order processing and orchestration
├── notification-service/  # Async consumer (e.g., Email, SMS via RabbitMQ)
├── gateway/               # Centralized entry point, rate limiting, and routing
├── shared/                # Core Python package (DB, Redis, MQ, Auth utils)
├── k8s/                   # Kubernetes manifest templates
├── docker-compose.yml     # Local orchestration map
└── README.md              # You are here

🚀 Quick Start (Local Development)

Prerequisites

  • Docker and Docker Compose installed.
  • Python 3.10+ (for local testing).

1. Boot up the Infrastructure

To start the services locally (including PostgreSQL, Redis, and RabbitMQ):

docker-compose up --build -d

Note: This will spin up all 6 microservices and 3 infrastructure containers. Wait a few moments for the databases to initialize.

2. Verify Services

Check the health of the API Gateway and underlying services:

  • Gateway Swagger UI: http://localhost:8000/docs
  • Gateway Health Check: http://localhost:8000/health
  • RabbitMQ Dashboard: http://localhost:15672 (guest / guest)

3. Running Unit Tests

Navigate to any microservice folder and run pytest:

cd user-service
pip install pytest httpx
pytest tests/

☸️ Kubernetes Deployment (Production)

This project contains complete manifestation templates for Kubernetes deployments under the k8s/ directory.

  1. Deploy Infrastructure:
    kubectl apply -f k8s/postgres.yaml
    kubectl apply -f k8s/redis.yaml
    kubectl apply -f k8s/rabbitmq.yaml
  2. Deploy Microservices:
    kubectl apply -f k8s/gateway.yaml
    kubectl apply -f k8s/auth-service.yaml
    kubectl apply -f k8s/user-service.yaml
    kubectl apply -f k8s/product-service.yaml
    kubectl apply -f k8s/order-service.yaml
    kubectl apply -f k8s/notification-service.yaml
  3. Deploy Ingress:
    kubectl apply -f k8s/ingress.yaml

🛡️ Security Best Practices Used

  • JWT Bearer Auth: Stateless tokens passed through headers, validated at the API Gateway or individual services.
  • Environment Variables: No hardcoded secrets. Relying on .env locally and ConfigMaps in K8s.
  • Rate Limiting: (Designed) utilizing Redis to prevent brute-force attacks at the Gateway layer.
  • Statelessness: All API nodes are 100% stateless, safely scaling up and down on demand.

🤝 Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

📜 License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors