A production-grade API infrastructure combining:
- Rate Limiting with real-time monitoring
- Load Balancing with multiple strategies
- Caching Layer for performance optimization
- Health Monitoring with automatic failover
- Request Queuing for handling spikes
- Real-time Dashboards for both systems
- JWT Authentication for secure token-based access
- Geographic Routing for optimal latency and compliance
Internet → Load Balancer (port 8080) → Backend Servers (5001, 5002, 5003)
↓
Rate Limiter
Cache Layer
Health Checks
Geo Routing
JWT Auth
- Rate limiting with tiered access (free, basic, premium, enterprise)
- Multiple load balancing strategies (round-robin, least connections, weighted, IP hash, adaptive)
- In-memory caching with TTL
- Health monitoring with automatic failover
- Request queuing for traffic spikes
- Circuit breaker pattern
- Database connection pooling
- Real-time WebSocket dashboards
- Per-IP rate limiting
- Admin operations (block/unblock, tier upgrades)
- JWT token-based authentication
- Token refresh mechanism
- User registration and login endpoints
- Cryptographically signed tokens
- Token expiration (access: 1 hour, refresh: 7 days)
- Backward compatibility with API key migration
- Protected endpoints with
@require_jwtdecorator
- IP geolocation resolution
- Distance-based datacenter selection
- Multiple datacenter support
- Haversine distance calculation
- Regional routing (US, EU, Asia Pacific, etc.)
- Geo-statistics tracking
- Response headers with datacenter info
- Prometheus metrics export
- Grafana dashboard integration
- Custom metrics for rate limiting
- Backend performance metrics
- Cache hit/miss tracking
- Geographic routing analytics
- Docker containers for all services
- Docker Compose orchestration
- Multi-container networking
- Volume persistence
- Environment configuration
- Redis for distributed rate limiting
- PostgreSQL for production database
- Distributed caching across instances
- Session synchronization
- Multi-instance deployment
- API key rotation mechanism
- Password hashing with bcrypt
- Secure admin endpoints
- Environment variable management
- Database migrations with Alembic
- Kubernetes deployment manifests
- Message queue integration (RabbitMQ/Kafka)
- DDoS protection enhancements
- Bot detection
- IP whitelisting/blacklisting
- OAuth2 authentication
- Multi-region geographic distribution
- CDN integration
- Log aggregation (ELK stack)
- Error tracking (Sentry)
# Install all dependencies
pip install -r requirements.txtThis project uses a SQLite database (ratelimiter.db). The schema might evolve over time. If you encounter any database-related errors, you can use the fix_database.py script to migrate your database to the latest schema.
To create a fresh database (deleting all existing data):
python fix_database.py freshTo migrate an existing database:
python fix_database.pyTerminal 1: Start Backend Servers
python mock_backends.pyThis starts 3 backend servers on ports 5001, 5002, 5003
Terminal 2: Start Load Balancer
python load_balancer.pyLoad balancer runs on port 8080
Terminal 3: Start Rate Limiter
python app.pyRate limiter runs on port 5000
- Distributes requests evenly across all backends
- Simple and predictable
- Routes to backend with fewest active connections
- Best for long-running requests
- Backends with higher weights get more traffic
- Useful for different server capacities
- Same client IP always goes to same backend
- Maintains session affinity
- Routes to fastest backend
- Optimizes for performance
- Considers multiple factors:
- Active connections
- Response time
- Failure rate
- Dynamically adjusts to conditions
- Automatic health checks every 10 seconds
- Unhealthy backends removed from rotation
- Automatic recovery when backend comes back
- In-memory cache with TTL (Time To Live)
- Cache hit/miss tracking
- Reduces backend load by ~30-50%
- Per-IP rate limiting (100 requests/minute)
- Tiered limits based on user subscription
- Prevents abuse and DDoS
- Automatic retry-after headers
- Progressive ban system with multipliers
- Priority queue for handling traffic spikes
- Prevents backend overload
- Graceful degradation under load
- Fails fast when backend is down
- Automatic retry logic
- Prevents cascade failures
- Reuses database connections
- Reduces overhead
- Better performance
- WebSocket-based live updates
- Beautiful dashboards
- Historical data tracking
- Cryptographically signed tokens
- Self-contained (stateless)
- Automatic expiration
- Refresh token mechanism
- User metadata in tokens (tier, permissions)
- Backward compatible with API keys
- IP geolocation using multiple providers
- Distance-based datacenter selection
- Support for multiple regions (US, EU, Asia)
- Reduced latency for global users
- Data residency compliance ready
- Dashboard: http://localhost:8080/dashboard
- Stats API: http://localhost:8080/stats
- Geo Stats: http://localhost:8080/geo/stats
- Proxy Endpoint: http://localhost:8080/data?api_key=YOUR_KEY
- Dashboard: http://localhost:5000/dashboard
- Usage API: http://localhost:5000/usage?api_key=YOUR_KEY
- Data Endpoint: http://localhost:5000/data?api_key=YOUR_KEY (legacy)
- JWT Data Endpoint: http://localhost:5000/api/v2/data (with Bearer token)
- Register: POST http://localhost:5000/auth/register
- Login: POST http://localhost:5000/auth/login
- Refresh Token: POST http://localhost:5000/auth/refresh
- User Info: GET http://localhost:5000/auth/me
- Logout: POST http://localhost:5000/auth/logout
- Backend 1: http://localhost:5001
- Backend 2: http://localhost:5002
- Backend 3: http://localhost:5003
python test_loadbalancer.pyThis tests:
- Basic proxying
- Load distribution
- Different strategies
- Caching
- Concurrent requests
- Rate limiting
- Health checks
- Sticky sessions
python test_enhanced.pyThis tests:
- Rate limiting logic
- Tier upgrades
- IP limiting
- Admin operations
- Real-time monitoring
python test_jwt.pyThis tests:
- User registration
- Token generation
- Protected endpoints
- Token refresh
- API key migration
curl -X POST http://localhost:5000/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "secure_password",
"tier": "premium"
}'Response:
{
"access_token": "eyJhbGc...",
"refresh_token": "eyJhbGc...",
"expires_in": 3600,
"user_id": "abc123",
"tier": "premium"
}curl http://localhost:5000/api/v2/data \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"curl -X POST http://localhost:5000/auth/refresh \
-H "Content-Type: application/json" \
-d '{"refresh_token": "YOUR_REFRESH_TOKEN"}'curl -X POST http://localhost:8080/change_strategy \
-H "Content-Type: application/json" \
-d '{"strategy": "least_connections"}'curl "http://localhost:8080/data?api_key=my_key"curl http://localhost:8080/statscurl http://localhost:8080/geo/statscurl -X POST http://localhost:5000/admin/upgrade_tier \
-H "Content-Type: application/json" \
-d '{"api_key": "my_key", "tier": "premium"}'curl -X POST http://localhost:5000/admin/block_key \
-H "Content-Type: application/json" \
-d '{"api_key": "malicious_key"}'- Live backend status
- Request distribution
- Strategy selector
- Performance metrics
- Health status
- Geographic routing statistics
- Real-time request stream
- Success/failure rates
- Active API keys
- Request charts
- Recent logs
- JWT token activity
# Rate limiting
self.rate_limit = 50 # requests per minute
# Cache TTL
self.cache = CacheLayer(default_ttl=30) # seconds
# Health check interval
self.health_check_interval = 10 # seconds
# Backend servers
self.lb.add_backend("localhost", 5001, weight=2)
self.lb.add_backend("localhost", 5002, weight=1)
# Geographic routing
self.geo_router = GeoRouter()# Tier limits
RATE_LIMITS = {
'free': {'requests': 5, 'window': 60},
'basic': {'requests': 20, 'window': 60},
'premium': {'requests': 100, 'window': 60},
'enterprise': {'requests': 1000, 'window': 60}
}
# IP rate limit
RATE_LIMIT_IP = 100 # per minute
# Ban settings
TEMP_BAN_SECONDS = 300 # 5 minutes
BAN_MULTIPLIER = 2 # escalation# Token expiration
refresh_token_expiry = 604800 # 7 days
# Algorithm
algorithm = 'HS256'
# Secret key (use environment variable in production)
secret_key = os.getenv('JWT_SECRET_KEY', 'your-secret-key')With 3 backends and optimal configuration:
- Throughput: ~500-1000 requests/second
- Latency: <50ms (with cache hits)
- Cache Hit Rate: 30-50%
- Failover Time: <10 seconds
- Max Concurrent: 1000+ connections
- JWT Verification: <1ms per request
- Geo Lookup: <5ms (cached), <50ms (uncached)
- Rate limiting per IP
- JWT token-based authentication
- Token expiration
- HTTPS/TLS
- Secure admin endpoints
- Password hashing (bcrypt)
- API key rotation
- Environment variable management
- Connection pooling
- Async operations
- Geographic routing
- Redis for distributed cache
- Message queue (RabbitMQ/Kafka)
- Horizontal scaling support
- Database replication
- Real-time dashboards
- Health checks
- Geographic routing metrics
- Prometheus metrics
- Grafana dashboards
- Error tracking (Sentry)
- Log aggregation (ELK stack)
- Automatic failover
- Health monitoring
- Circuit breaker pattern
- Multiple load balancer instances
- Database replication
- Multi-region deployment
API-RATE-LIMITER/
├── app.py # Rate limiter server with JWT support
├── load_balancer.py # Load balancer with geo-routing
├── auth.py # JWT authentication system
├── geo_router.py # Geographic routing engine
├── mock_backends.py # Backend server simulators
├── fix_database.py # Database migration utility
├── test_enhanced.py # Rate limiter tests
├── test_loadbalancer.py # Load balancer tests
├── test_jwt.py # JWT authentication tests
├── requirements.txt # Python dependencies
└── README.md # This file
| Method | Endpoint | Description |
|---|---|---|
| GET | /dashboard |
Interactive dashboard |
| GET | /stats |
JSON statistics |
| GET | /geo/stats |
Geographic routing statistics |
| POST | /change_strategy |
Change LB strategy |
| ANY | /* |
Proxied to backends |
| Method | Endpoint | Description |
|---|---|---|
| GET | /dashboard |
Real-time dashboard |
| GET | /data |
Protected endpoint (deprecated) |
| GET | /usage |
Check usage limits |
| GET | /admin/users |
List all users |
| POST | /admin/upgrade_tier |
Upgrade user tier |
| POST | /admin/block_key |
Block API key |
| POST | /admin/unblock_key |
Unblock API key |
| GET | /logs |
View recent logs |
| Method | Endpoint | Description |
|---|---|---|
| POST | /auth/register |
Register new user |
| POST | /auth/login |
Login and get tokens |
| POST | /auth/refresh |
Refresh access token |
| POST | /auth/logout |
Logout and revoke token |
| GET | /auth/me |
Get current user info |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v2/data |
Get protected data |
| GET | /api/v2/premium |
Premium tier only |
# Check if ports are available
lsof -i :5001
lsof -i :5002
lsof -i :5003
# Kill processes if needed
kill -9 <PID># Verify backends are running
curl http://localhost:5001/health
curl http://localhost:5002/health
curl http://localhost:5003/health# Stop all instances and delete database
rm ratelimiter.db
python app.py # Recreates database- Check firewall settings
- Ensure CORS is enabled
- Verify port 8080/5000 is accessible
- Verify token format:
Authorization: Bearer <token> - Check token expiration
- Ensure JWT_SECRET_KEY is consistent across instances
- Verify token signature
- Check internet connection for GeoIP API
- Verify IP is not localhost/private
- Review geo routing logs
- Confirm datacenters are configured
This project demonstrates:
- Reverse Proxy Pattern
- Load Balancing Algorithms
- Circuit Breaker Pattern
- Caching Strategies
- Rate Limiting Techniques
- Health Check Mechanisms
- WebSocket Real-time Updates
- Async/Await Programming
- Database Connection Pooling
- Microservices Architecture
- JWT Authentication
- Geographic Routing
- Token-based Security
- IP Geolocation
- Stateless Architecture
- JWT authentication system
- Geographic routing engine
- Token management
- API migration support
- Prometheus metrics integration
- Grafana dashboard setup
- Custom metric exporters
- Alert configuration
- Docker container creation
- Docker Compose orchestration
- Multi-service networking
- Volume management
- Redis integration
- PostgreSQL migration
- Distributed rate limiting
- Session synchronization
- SSL/TLS implementation
- Security enhancements
- Performance optimization
- Load testing
- Kubernetes deployment
- Message queue integration
- Multi-region support
- Advanced analytics
This is a learning project showcasing enterprise patterns. Contributions welcome:
- Fork and experiment
- Add new features
- Submit pull requests
- Share improvements
- Report issues
MIT License - Feel free to use for learning and production projects.
For issues and questions:
- Check the troubleshooting section
- Review the integration guides
- Open an issue on GitHub
- Contact the maintainers
Last Updated: Week 2 - JWT Authentication & Geographic Routing Implementation