Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TrafficShield πŸ›‘οΈ

Intelligent Load Balancing, Performance Optimization & Auto-Healing System for WanderLust

TrafficShield is the infrastructure layer built around the WanderLust Travel Booking App β€” a real-world FastAPI + React application. It demonstrates how to handle high user traffic using multiple backend instances, NGINX load balancing, Redis caching, rate limiting, auto-healing, and real-time monitoring via Prometheus and Grafana.


πŸ“Œ Project Objective

Most basic web applications fail or slow down when many users access them simultaneously due to single-server limitations. TrafficShield solves this by introducing horizontal scaling, traffic management, auto-healing, and live monitoring β€” running multiple instances of the WanderLust backend and distributing incoming requests across them using NGINX.


πŸ—οΈ System Architecture

         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚               Client Side                    β”‚
         β”‚                                              β”‚
         β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
         β”‚   β”‚ React Frontend  β”‚   β”‚  k6 Load       β”‚   β”‚
         β”‚   β”‚  (Port 5173)    β”‚   β”‚  Tester        β”‚   β”‚
         β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
         └────────────┼───────────────────-β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚                   β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚ HTTP Requests
                                β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚             TrafficShield Layer              β”‚
         β”‚                                              β”‚
         β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
         β”‚   β”‚        NGINX  (Port 80)              β”‚   β”‚
         β”‚   β”‚  Load Balancer + Rate Limiting       β”‚   β”‚
         β”‚   β”‚  Auto-Failover + Health Checks       β”‚   β”‚
         β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
         β”‚                     β”‚ Round Robin            β”‚
         β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”             β”‚
         β”‚          β–Ό          β–Ό          β–Ό             β”‚
         β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
         β”‚   β”‚Wanderlustβ”‚ β”‚Wanderlustβ”‚ β”‚Wanderlustβ”‚     β”‚
         β”‚   β”‚   API    β”‚ β”‚   API    β”‚ β”‚   API    β”‚     β”‚
         β”‚   β”‚Port 8001 β”‚ β”‚Port 8002 β”‚ β”‚Port 8003 β”‚     β”‚
         β”‚   β”‚(healthy) β”‚ β”‚(healthy) β”‚ β”‚(healthy) β”‚     β”‚
         β”‚   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜     β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β–Ό                         β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   MongoDB    β”‚         β”‚    Redis     β”‚
          β”‚  wanderlust  β”‚         β”‚   Cache      β”‚
          β”‚  (Port 27017)β”‚         β”‚  (Port 6379) β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β–Ό                         β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚  Prometheus  β”‚         β”‚   Grafana    β”‚
          β”‚  (Port 9090) │────────▢│  (Port 3000) 
          β”‚  Metrics     β”‚         β”‚  Dashboard   β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Features

  • Multi-Instance Backend β€” Same WanderLust API runs on ports 8001, 8002, 8003 simultaneously
  • NGINX Load Balancing β€” Round-robin traffic distribution across all instances
  • Auto-Failover β€” NGINX automatically stops routing to crashed instances
  • Rate Limiting β€” Restricts excessive requests per client IP via NGINX
  • Redis Caching β€” Reduces repeated MongoDB queries on high-traffic endpoints
  • Auto-Healing β€” Docker automatically restarts any crashed container within seconds
  • Prometheus Monitoring β€” Scrapes metrics from all 3 instances every 5 seconds
  • Grafana Dashboard β€” Live visualization of requests, response time, and errors
  • Load Testing β€” Simulates 100 / 1000 / 10000 concurrent users using k6
  • Docker Compose β€” One command starts the entire system

πŸ› οΈ Tech Stack

Layer Technology Why Used
Frontend React + TypeScript (Vite) Fast modern UI
Backend FastAPI (Python) Async, fast REST API framework
Load Balancer NGINX Round-robin + rate limiting + failover
Database MongoDB 7.0 Flexible JSON document storage
Cache Redis In-memory caching for fast responses
Load Testing k6 Simulates thousands of virtual users
Containerization Docker + Docker Compose One command startup
Monitoring Prometheus Collects metrics from all instances
Dashboard Grafana Live visual monitoring dashboard
Environment Windows + WSL2 (Ubuntu) Development environment

πŸ“ Project Structure

TrafficShield/
β”‚
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ main.py               # WanderLust FastAPI application
β”‚   β”œβ”€β”€ Dockerfile            # Docker build instructions
β”‚   β”œβ”€β”€ .dockerignore         # Files excluded from Docker build
β”‚   β”œβ”€β”€ .env                  # Environment variables (not committed)
β”‚   β”œβ”€β”€ venv/                 # Python virtual environment (not committed)
β”‚   └── requirements.txt      # Python dependencies
β”‚
β”œβ”€β”€ nginx/
β”‚   └── nginx.conf            # Load balancer + rate limiting + failover config
β”‚
β”œβ”€β”€ monitoring/
β”‚   β”œβ”€β”€ prometheus.yml        # Prometheus scrape configuration
β”‚   └── grafana-datasource.yml # Grafana auto data source config
β”‚
β”œβ”€β”€ load-tests/
β”‚   └── test.js               # k6 load testing scripts
β”‚
β”œβ”€β”€ frontend/                 # Vite + React + TypeScript (WanderLust UI)
β”‚   └── .env                  # VITE_API_URL points to NGINX port 80
β”‚
β”œβ”€β”€ .gitignore
β”œβ”€β”€ docker-compose.yml        # Orchestrates all 7 services
└── README.md

βš™οΈ Prerequisites

  • Windows with WSL2 (Ubuntu)
  • Python 3.12+
  • Node.js 18+
  • NGINX
  • k6 v1.7.1+
  • Docker + Docker Compose

πŸš€ Getting Started

1. Clone the Repository

git clone https://github.com/Rajyadav999/TrafficShield.git
cd TrafficShield

2. Start All Services with Docker

docker-compose up -d

This automatically starts all 7 services:

Service Port
WanderLust Instance 1 8001
WanderLust Instance 2 8002
WanderLust Instance 3 8003
MongoDB 27017
Redis 6379
Prometheus 9090
Grafana 3000

3. Start NGINX Load Balancer

sudo systemctl start nginx

4. Run Frontend

cd frontend
npm install
npm run dev

Frontend available at http://localhost:5173 βœ…


πŸ”€ Load Balancing

NGINX distributes traffic across 3 WanderLust instances using round-robin strategy:

Request 1 β†’ port 8001
Request 2 β†’ port 8002
Request 3 β†’ port 8003
Request 4 β†’ port 8001 (cycles back)

Every API response includes an instance_port field to prove which server handled it:

{
  "message": "WanderLust Travel API is live 🌍",
  "instance_port": 8001,
  "status": "healthy"
}

🚦 Rate Limiting

NGINX restricts each client IP to a maximum of 1000 requests per second. Clients exceeding this receive a 429 Too Many Requests response.


⚑ Redis Caching

High-traffic endpoints /destinations and /hotels are cached in Redis for 60 seconds:

First request  β†’ Check Redis β†’ MISS β†’ Query MongoDB β†’ Save to Redis β†’ Return
Next requests  β†’ Check Redis β†’ HIT  β†’ Return instantly (no MongoDB) βœ…

πŸ”„ Auto-Healing

Every backend container has restart: always in Docker Compose. If any instance crashes:

Instance crashes ❌
        ↓
Docker detects it within seconds
        ↓
Container automatically restarts βœ…
        ↓
NGINX routes to healthy instances during restart
        ↓
System heals itself β€” no human needed βœ…

Test it yourself:

# Terminal 1 β€” watch containers
watch -n 2 docker-compose ps

# Terminal 2 β€” kill an instance
docker stop wanderlust_8001

# Watch it restart automatically within 10-15 seconds!

πŸ“Š Monitoring

Prometheus β€” http://localhost:9090

Scrapes metrics from all 3 instances every 5 seconds including total requests, duration, and error rates.

Grafana β€” http://localhost:3000

Login: admin / admin123

Live dashboard showing requests per second per instance, average response time, and traffic distribution.


πŸ“ˆ Load Testing

# Run staged load test (100 β†’ 1000 users over 2.5 minutes)
k6 run load-tests/test.js

# Quick stress test
k6 run --vus 1000 --duration 30s load-tests/test.js

Performance Results

Scenario Avg Response Time Failure Rate Throughput
Single instance (no LB) ~360ms ~39% ~898 req/s
3 instances + NGINX ~360ms ~4.46% ~898 req/s
3 instances + NGINX + Redis ~17ms ~4.46% ~7915 req/s

πŸ”‘ Environment Variables

backend/.env:

MONGO_URL=mongodb://mongodb:27017
DB_NAME=wanderlust
PORT=8001
REDIS_HOST=redis

frontend/.env:

VITE_API_URL=http://localhost:80

πŸ“‘ API Endpoints

Method Endpoint Description Cached
GET / Health check + instance info ❌
GET /health Detailed health + timestamp ❌
GET /destinations Fetch all destinations βœ… 60s
GET /destinations/{id} Fetch single destination ❌
GET /hotels Fetch all hotels βœ… 60s
GET /hotels/{id} Fetch single hotel ❌
GET /packages Fetch all travel packages ❌
GET /packages/{id} Fetch single package ❌
POST /bookings/hotel Create hotel booking ❌
POST /bookings/package Create package booking ❌
POST /bookings/view View bookings by email ❌
GET /search Search everything ❌
GET /metrics Prometheus metrics endpoint ❌

πŸ‘€ Author :- Raj Yadav


πŸ“„ License

This project is open source and available under the MIT License.

About

Intelligent Load Balancing and Performance Optimization System

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages