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.
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.
ββββββββββββββββββββββββββββββββββββββββββββββββ
β 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 β
ββββββββββββββββ ββββββββββββββββ
- 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
| 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 |
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
- Windows with WSL2 (Ubuntu)
- Python 3.12+
- Node.js 18+
- NGINX
- k6 v1.7.1+
- Docker + Docker Compose
git clone https://github.com/Rajyadav999/TrafficShield.git
cd TrafficShielddocker-compose up -dThis 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 |
sudo systemctl start nginxcd frontend
npm install
npm run devFrontend available at http://localhost:5173 β
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"
}NGINX restricts each client IP to a maximum of 1000 requests per second. Clients exceeding this receive a 429 Too Many Requests response.
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) β
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!Scrapes metrics from all 3 instances every 5 seconds including total requests, duration, and error rates.
Login: admin / admin123
Live dashboard showing requests per second per instance, average response time, and traffic distribution.
# 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| 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 |
backend/.env:
MONGO_URL=mongodb://mongodb:27017
DB_NAME=wanderlust
PORT=8001
REDIS_HOST=redisfrontend/.env:
VITE_API_URL=http://localhost:80| 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 | β |
- GitHub: @Rajyadav999
This project is open source and available under the MIT License.