Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

12 Commits

Folders and files

Repository files navigation

πŸ›’ Cloud-Native Auto-Scaling E-Commerce Backend

A production-style microservices backend with auto-scaling, Redis caching, and Kubernetes orchestration β€” built entirely with free, open-source tools.


πŸ— Architecture

Client (Postman/K6)
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   API Gateway    β”‚  :8080 / NodePort 30080
β”‚  (JWT Issue/Val) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚         β”‚               β”‚
         β–Ό         β–Ό               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚Product Serviceβ”‚  β”‚ Order Serviceβ”‚  β”‚ User Service β”‚
β”‚   :8081      β”‚  β”‚   :8082      β”‚  β”‚   :8083      β”‚
β”‚ Redis Cache  β”‚  β”‚ β†’ calls      β”‚  β”‚ PostgreSQL   β”‚
β”‚ PostgreSQL   β”‚  β”‚   Product    β”‚  β”‚ (Auth data)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                β”‚                β”‚
         β–Ό                β–Ό                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  PostgreSQL  β”‚  β”‚    Redis     β”‚  β”‚  Monitoring  β”‚
β”‚  :5432       β”‚  β”‚   :6379      β”‚  β”‚  (Actuator)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

ecommerce-cloud-native/
β”œβ”€β”€ api-gateway/            # Spring Cloud Gateway + JWT
β”‚   β”œβ”€β”€ src/main/java/...
β”‚   β”œβ”€β”€ src/main/resources/application.yml
β”‚   β”œβ”€β”€ pom.xml
β”‚   └── Dockerfile
β”œβ”€β”€ product-service/        # Product CRUD + Redis caching
β”‚   β”œβ”€β”€ src/main/java/...
β”‚   β”œβ”€β”€ src/main/resources/application.yml
β”‚   β”œβ”€β”€ pom.xml
β”‚   └── Dockerfile
β”œβ”€β”€ order-service/          # Order management
β”‚   β”œβ”€β”€ src/main/java/...
β”‚   β”œβ”€β”€ src/main/resources/application.yml
β”‚   β”œβ”€β”€ pom.xml
β”‚   └── Dockerfile
β”œβ”€β”€ k8s/                    # Kubernetes manifests
β”‚   β”œβ”€β”€ namespace.yaml
β”‚   β”œβ”€β”€ configmap.yaml
β”‚   β”œβ”€β”€ secrets.yaml
β”‚   β”œβ”€β”€ gateway/            # Deployment, NodePort, HPA
β”‚   β”œβ”€β”€ product/            # Deployment, ClusterIP, HPA
β”‚   β”œβ”€β”€ order/              # Deployment, ClusterIP, HPA
β”‚   β”œβ”€β”€ postgres/           # Deployment, ClusterIP, PVC
β”‚   └── redis/              # Deployment, ClusterIP
β”œβ”€β”€ load-test/
β”‚   └── k6-script.js        # K6 load test (500 VUs)
β”œβ”€β”€ docker-compose.yml      # Local development
└── init-db.sql             # PostgreSQL init script

πŸš€ Quick Start β€” Local Development (Docker Compose)

Prerequisites

  • Docker Desktop
  • Java 17
  • Maven 3.9+

1. Start Infrastructure + All Services

# Start PostgreSQL and Redis only (for local Maven run)
docker-compose up postgres redis -d

# OR start everything (requires Docker images to be built)
docker-compose up --build -d

2. Run Services Locally (without Docker)

# Terminal 1 β€” Product Service
cd product-service
mvn spring-boot:run

# Terminal 2 β€” Order Service
cd order-service
mvn spring-boot:run

# Terminal 3 β€” API Gateway
cd api-gateway
mvn spring-boot:run

3. Get a JWT Token

curl -X POST http://localhost:8080/auth/token \
  -H "Content-Type: application/json" \
  -d '{"username":"testuser","password":"test123"}'

4. Test the APIs

# Create a product
curl -X POST http://localhost:8081/products \
  -H "Content-Type: application/json" \
  -d '{"name":"Laptop","description":"Gaming Laptop","price":999.99,"stockQuantity":100}'

# Get all products (try twice - 2nd call is served from Redis cache)
curl http://localhost:8080/products

# Create an order (requires JWT)
TOKEN="<your-jwt-token>"
curl -X POST http://localhost:8080/orders \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"productId":1,"quantity":2,"customerName":"John Doe"}'

5. Swagger UI


☸️ Kubernetes Deployment (Minikube)

Prerequisites

Step 1: Start Minikube

minikube start --memory=4096 --cpus=4

Step 2: Enable Metrics Server (required for HPA)

minikube addons enable metrics-server
kubectl get deployment metrics-server -n kube-system  # Wait until Ready

Step 3: Build Docker Images Inside Minikube

# Point Docker to Minikube's daemon
eval $(minikube docker-env)       # Linux/Mac
# OR on Windows PowerShell:
# minikube docker-env | Invoke-Expression

# Build all service images
docker build -t api-gateway:1.0.0 ./api-gateway
docker build -t product-service:1.0.0 ./product-service
docker build -t order-service:1.0.0 ./order-service

Step 4: Deploy to Kubernetes

# Create namespace, secrets, and configmap first
kubectl apply -f k8s/namespace.yaml
kubectl apply -f k8s/secrets.yaml
kubectl apply -f k8s/configmap.yaml

# Deploy infrastructure
kubectl apply -f k8s/postgres/
kubectl apply -f k8s/redis/

# Wait for DB to be ready
kubectl wait --for=condition=ready pod -l app=postgres -n ecommerce --timeout=120s

# Deploy microservices
kubectl apply -f k8s/gateway/
kubectl apply -f k8s/product/
kubectl apply -f k8s/order/

Step 5: Verify Deployment

# Check all pods
kubectl get pods -n ecommerce

# Check services
kubectl get services -n ecommerce

# Check HPAs
kubectl get hpa -n ecommerce

# Get gateway URL
minikube service api-gateway -n ecommerce --url

πŸ“ˆ Load Testing & HPA Verification

Install K6

# Windows (Chocolatey)
choco install k6

# Or download from https://k6.io/docs/get-started/installation/

Run Load Test

# Against local Docker Compose
k6 run load-test/k6-script.js

# Against Kubernetes (get URL first)
MINIKUBE_URL=$(minikube service api-gateway -n ecommerce --url)
k6 run -e GATEWAY_URL=$MINIKUBE_URL load-test/k6-script.js

Watch HPA Scaling in Real Time

# Terminal 1: Watch HPA
kubectl get hpa -n ecommerce -w

# Terminal 2: Watch pods
kubectl get pods -n ecommerce -w

# Terminal 3: Run load test
k6 run load-test/k6-script.js

Expected behavior:

  • Pod count stays at 1 initially
  • After CPU > 60%, new pods are created (up to 5)
  • After load ends, pods scale back down after ~2 minutes

πŸ” Authentication

User Password Role
admin admin ADMIN
user user USER
testuser test123 USER

Protected routes (require JWT): POST /products, PUT /products, DELETE /products, all /orders/**

Public routes: GET /products/**, GET /auth/**


🧩 API Endpoints

Via API Gateway (port 8080)

Method Path Auth Description
POST /auth/token ❌ Get JWT token
GET /products ❌ List all products
GET /products/{id} ❌ Get product by ID
POST /products βœ… Create product
PUT /products/{id} βœ… Update product
DELETE /products/{id} βœ… Delete product
POST /orders βœ… Create order
GET /orders βœ… List all orders
GET /orders/{id} βœ… Get order by ID

βš™οΈ Technology Stack

Layer Technology
Language Java 17
Framework Spring Boot 3.2
Gateway Spring Cloud Gateway
Database PostgreSQL 15
Cache Redis 7
Containerization Docker
Orchestration Kubernetes (Minikube)
Auto-Scaling HPA + Metrics Server
Load Testing K6
API Docs Swagger UI (SpringDoc)

🎯 Key Engineering Features

βœ… Microservices Architecture β€” 3 independent services
βœ… Redis Caching β€” @Cacheable + @CacheEvict with 10-min TTL
βœ… Java Serialization β€” Products implement Serializable for Redis
βœ… Multi-Stage Docker Builds β€” Minimal JRE 17 runtime images
βœ… Kubernetes HPA β€” Scales 1β†’5 pods at 60% CPU
βœ… Health Probes β€” Readiness/liveness on all services
βœ… Resource Limits β€” Prevents CPU/memory abuse
βœ… Global Exception Handling β€” Structured error responses
βœ… JWT Security β€” Token validation at gateway, user headers forwarded

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages