Load Balancing
Load Balancing Nədir?
Load Balancing - gələn şəbəkə traffic-ini bir neçə server arasında bərabər paylaşdırmaq üçün istifadə olunan texnikadır.
Məqsədlər:
- Traffic-in bərabər paylaşdırılması
- Server overload-un qarşısının alınması
- High availability (yüksək əlçatanlıq)
- Fault tolerance
- Scalability
- Performance optimization
Load Balancer Types
1. Hardware Load Balancer
Xüsusiyyətlər:
- Dedicated physical device
- Yüksək performans
- Bahalı
- F5 BIG-IP, Citrix NetScaler
Üstünlüklər:
- High throughput
- Low latency
- Advanced features
Dezavantajlar:
- Yüksək qiymət
- Limited flexibility
- Vendor lock-in
2. Software Load Balancer
Xüsusiyyətlər:
- Standard server-də işləyir
- Cost-effective
- Flexible configuration
- HAProxy, NGINX, Apache
Üstünlüklər:
- Cheaper
- Easy to scale
- Open source options
Dezavantajlar:
- Lower performance (hardware-a nisbətən)
- Resource consumption
3. Cloud Load Balancer
Xüsusiyyətlər:
- Cloud provider tərəfindən managed
- Auto-scaling
- Global distribution
- AWS ELB, Azure LB, GCP LB
Üstünlüklər:
- No hardware management
- Pay-as-you-go
- High availability built-in
Load Balancer Layers (OSI)
Layer 4 Load Balancing (Transport Layer)
İş prinsipi: IP address və port əsasında qərar verir.
Xüsusiyyətlər:
- Fast (header-ə baxır)
- Protocol-agnostic
- TCP/UDP support
- Connection-based routing
İstifadə sahələri:
- Simple HTTP traffic
- Database connections
- Any TCP/UDP service
Layer 7 Load Balancing (Application Layer)
İş prinsipi: HTTP header, URL, content əsasında qərar verir.
Xüsusiyyətlər:
- Content-aware routing
- SSL termination
- HTTP header manipulation
- Cookie-based routing
- URL rewriting
İstifadə sahələri:
- Web applications
- Microservices
- API gateways
- Content-based routing
Layer 4 vs Layer 7
| Feature | Layer 4 | Layer 7 |
|---|---|---|
| Decision | IP + Port | Content + Headers |
| Performance | Faster | Slower |
| Flexibility | Limited | High |
| SSL | Pass-through | Termination |
| Cost | Lower | Higher |
| Use case | Simple routing | Complex routing |
Load Balancing Algorithms
1. Round Robin
İş prinsipi: Hər request növbə ilə server-lərə göndərilir.
Üstünlüklər:
- Simple
- Fair distribution
- No state required
Dezavantajlar:
- Server capacity nəzərə alınmır
- Session persistence yoxdur
2. Weighted Round Robin
İş prinsipi: Server-lərə weight (çəki) verilir, güclü server-lərə daha çox request.
Konfiqurasiya:
upstream backend {
server backend1.example.com weight=3;
server backend2.example.com weight=2;
server backend3.example.com weight=1;
}
3. Least Connections
İş prinsipi: Ən az active connection-u olan server-ə göndərir.
İstifadə sahəsi:
- Long-lived connections
- WebSocket connections
- Database connections
4. Weighted Least Connections
İş prinsipi: Connection count + weight kombinasiyası.
Formula:
Score = Active Connections / Weight
Select server with lowest score
5. IP Hash
İş prinsipi: Client IP-yə əsasən həmişə eyni server-ə yönləndirir.
Formula:
server_index = hash(client_ip) % number_of_servers
Üstünlüklər:
- Session persistence
- Cache hit ratio yaxşı
Dezavantajlar:
- Server əlavə/silinməsi problemi
- Uneven distribution (bəzən)
6. Least Response Time
İş prinsipi: Ən az response time göstərən server seçilir.
7. Random
İş prinsipi: Random olaraq server seçilir.
İstifadə sahəsi:
- Simple setups
- Testing
8. URL Hash
İş prinsipi: URL-ə əsasən routing (cache efficiency üçün).
Health Checks
Məqsəd: Server-lərin sağlamlığını yoxlamaq və problem olduqda traffic göndərməmək.
Active Health Checks
İş prinsipi: Load balancer mütəmadi olaraq server-lərə request göndərir.
Health Check Types:
HTTP/HTTPS Health Check
upstream backend {
server backend1.example.com;
server backend2.example.com;
# Health check config
check interval=3000 rise=2 fall=3 timeout=1000 type=http;
check_http_send "GET /health HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
TCP Health Check
# Check if port is open
tcp_check:
interval: 5s
timeout: 2s
port: 8080
Custom Health Check
# Application health endpoint
@app.route('/health')
def health_check():
# Check database
if not db.is_connected():
return 'unhealthy', 503
# Check dependencies
if not cache.is_available():
return 'unhealthy', 503
return 'healthy', 200
Passive Health Checks
İş prinsipi: Real traffic-dən error-lara baxaraq qərar verir.
Parameters:
- Interval: Yoxlama intervalı (5s, 10s)
- Timeout: Response timeout (2s)
- Rise: Neçə successful check-dən sonra healthy (2, 3)
- Fall: Neçə failed check-dən sonra unhealthy (2, 3)
Session Persistence (Sticky Sessions)
Problem: User-in request-ləri müxtəlif server-lərə getdikdə session itir.
Solution 1: Cookie-based Persistence
NGINX Configuration:
upstream backend {
ip_hash; # Simple IP-based persistence
server backend1.example.com;
server backend2.example.com;
}
# Or cookie-based
sticky cookie srv_id expires=1h domain=.example.com path=/;
Solution 2: Centralized Session Store
Üstünlüklər:
- Server down olsa da session davam edir
- True load balancing
- Horizontal scaling
High Availability (HA) Setup
Active-Passive
İş prinsipi: Bir load balancer active, digəri standby.
Keepalived Configuration:
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
virtual_ipaddress {
203.0.113.10
}
}
Active-Active
İş prinsipi: Hər iki load balancer traffic handle edir.
Global Server Load Balancing (GSLB)
Məqsəd: Müxtəlif geografik location-lardakı datacenter-lər arasında load balancing.
Benefits:
- Geo-proximity routing
- Disaster recovery
- Latency reduction
- Regional compliance
Methods:
- GeoDNS - Location-based DNS
- Anycast - Same IP, multiple locations
- Latency-based routing
SSL/TLS Termination
SSL Termination at Load Balancer
Üstünlüklər:
- Centralized certificate management
- Reduced server CPU load
- Easier certificate rotation
Dezavantajlar:
- Internal traffic unencrypted
- Compliance issues (bəzi hallarda)
SSL Pass-through
Üstünlüklər:
- End-to-end encryption
- Better security
Dezavantajlar:
- No L7 inspection
- Server CPU overhead
- Certificate management per server
Rate Limiting
Məqsəd: API abuse və DDoS-dan qorunma.
NGINX Configuration:
http {
# Define rate limit zone
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api burst=20 nodelay;
# Return rate limit headers
add_header X-RateLimit-Limit 10;
add_header X-RateLimit-Remaining $limit_req_remaining;
proxy_pass http://backend;
}
}
}
Load Balancing Patterns
1. Simple Load Balancing
2. Multi-Tier Load Balancing
3. Microservices Load Balancing
Popular Load Balancers
HAProxy
Xüsusiyyətlər:
- High performance
- L4 + L7
- Advanced routing
- Free and open source
Configuration Example:
frontend http_front
bind *:80
default_backend http_back
backend http_back
balance roundrobin
option httpchk GET /health
server server1 10.0.1.10:80 check
server server2 10.0.1.11:80 check
server server3 10.0.1.12:80 check
NGINX
Xüsusiyyətlər:
- Web server + load balancer
- Reverse proxy
- High performance
- Free and commercial versions
Configuration Example:
upstream backend {
least_conn;
server backend1.example.com weight=3;
server backend2.example.com weight=2;
server backend3.example.com backup;
keepalive 32;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Cloud Load Balancers
AWS:
- ALB (Application Load Balancer) - L7
- NLB (Network Load Balancer) - L4
- CLB (Classic Load Balancer) - Legacy
Azure:
- Azure Load Balancer - L4
- Application Gateway - L7
GCP:
- HTTP(S) Load Balancer - L7
- TCP/UDP Load Balancer - L4
Monitoring və Metrics
Key Metrics:
Monitoring Tools:
- Prometheus + Grafana
- DataDog
- New Relic
- CloudWatch (AWS)
Important Metrics:
- Requests per second (RPS)
- Average response time
- P95, P99 latency
- 2xx, 4xx, 5xx response codes
- Backend server health status
- Connection pool utilization
Best Practices
-
Health Checks:
- Mütəmadi health check
- Application-level checks
- Multiple failure threshold
-
Session Management:
- Centralized session store
- Stateless applications (mümkün olduqda)
-
SSL/TLS:
- TLS 1.3 istifadə et
- Strong cipher suites
- Certificate automation (Let's Encrypt)
-
Monitoring:
- Real-time metrics
- Alerting configured
- Log aggregation
-
Capacity Planning:
- Load testing
- Auto-scaling rules
- Overprovisioning (20-30%)
-
Security:
- DDoS protection
- Rate limiting
- WAF integration
- Regular updates
-
High Availability:
- Multiple load balancers
- Cross-region deployment
- Regular failover testing
Troubleshooting
Common Issues:
1. Uneven Load Distribution:
- Check algorithm (use least connections)
- Verify weights
- Check sticky sessions
2. Backend Server Unavailable:
- Verify health checks
- Check firewall rules
- Test backend connectivity
3. High Latency:
- Check backend performance
- Monitor connection limits
- Analyze keepalive settings
4. SSL/TLS Issues:
- Verify certificate validity
- Check cipher compatibility
- Monitor SSL handshake time
Əlaqəli Mövzular
- High Availability Architecture
- Microservices Architecture
- CDN (Content Delivery Network)
- DNS and GeoDNS
- Network Security
- Auto-scaling
- Container Orchestration (Kubernetes)
- API Gateway