Load Balancing Là Gì?
Load Balancer là hệ thống phân phối network traffic giữa multiple servers để đảm bảo no single server quá tải, tối ưu hóa resource utilization, và maximize availability. Load balancer đứng giữa users và server group, act như traffic cop directing requests.
Load balancing ra đời khi một server không thể handle tất cả requests từ internet. Thay vì upgrade server lên max specs (vertical scaling), load balancing cho phép thêm nhiều servers nhỏ (horizontal scaling) và phân phối load giữa chúng.
Tại Sao Cần Load Balancer?
1. High Availability
Load balancer detect khi một server fails và automatically route traffic đến healthy servers. Users không experience downtime – failover diễn ra seamless.
2. Scalability
Khi traffic tăng, đơn giản thêm more servers vào pool. Load balancer tự động include chúng vào rotation. Không cần thay đổi code hay architecture.
3. Performance
Requests được distributed across multiple servers, mỗi server handle fewer requests. Response times improve, users get faster experience.
4. Security
Load balancer có thể hide internal server IPs, protect against DDoS, terminate SSL/TLS (offload encryption), và provide single entry point for access control.
Các Loại Load Balancer
1. Layer 4 Load Balancer (Transport Layer)
Hoạt động ở TCP/UDP level, route traffic dựa trên IP addresses và port numbers. Fast performance vì không inspect packet content. NAT-based load balancing hoặc direct server return (DSR) mode.
- Ưu điểm: Low latency, high throughput, simple processing
- Nhược điểm: Không biết application content, limited smart routing
2. Layer 7 Load Balancer (Application Layer)
Inspect HTTP/HTTPS headers và content, có thể make routing decisions dựa trên URL path, cookies, headers. More intelligent routing như path-based routing (/api vs /web).
- Ưu điểm: Content-aware routing, session persistence, SSL termination
- Nhược điểm: Higher latency, more resource intensive
3. Hardware Load Balancer
Physical appliance từ vendors như F5 BIG-IP, Citrix ADC. Cung cấp high performance và extensive features. Pricey nhưng provide dedicated throughput.
4. Software Load Balancer
Software-based solutions như NGINX, HAProxy, Envoy. Chạy trên commodity hardware hoặc virtual machines. Highly flexible và cost-effective.
5. Cloud Load Balancer
Managed services như AWS ELB/ALB/NLB, Google Cloud Load Balancing, Azure Load Balancer. Tự động scale, highly available, integrated với cloud platform features.
Load Balancing Algorithms
1. Round Robin
Requests được distribute sequentially to each server in rotation. Đơn giản nhất nhưng không account for server capacity differences.
2. Weighted Round Robin
Assign weight cho each server (VD: server A weight=3, server B weight=1). Server có weight cao hơn nhận nhiều requests hơn. Useful khi servers có different capacities.
3. Least Connections
Route request đến server có ít active connections nhất. Dynamic hơn round robin, better cho varying request processing times.
4. IP Hash
Hash source IP để determine which server receives request. User luôn được routed to same server (session persistence). Useful cho stateful applications.
5. Least Response Time
Route đến server với lowest average response time và fewest active connections. Optimal balance giữa performance và load.
6. Resource-based (Adaptive)
Load balancer query agents trên servers để check current load (CPU, memory). Route đến server có most available resources.
Health Checks
Load balancer thường xuyên check health của backend servers để đảm bảo only healthy servers nhận traffic:
- TCP connect check – Server responds on specified port
- HTTP/HTTPS check – Server returns expected status code (VD: 200)
- Custom health endpoint – /health returns JSON với system status
- Active vs passive checks – Active: periodic probes. Passive: analyze actual traffic patterns
Failed health checks trigger removal from pool. Recovery checks verify server back online trước khi re-add.
Session Persistence (Sticky Sessions)
Một số applications cần user stick với same server for duration of session (shopping carts, in-progress transactions):
- Cookie-based – Load balancer set cookie chỉ định server
- IP-based – Hash user’s IP to same server
- Application-level – Application tracks session, load balancer routes based on session ID
SSL/TLS Termination
Load balancer có thể terminate SSL connections, decrypt traffic, và forward requests plaintext đến backend servers. Benefits:
- Offload encryption overhead – Backend servers không CPU-intensive encryption
- Centralized certificate management – Chỉ manage certs ở load balancer
- Header inspection – Load balancer có thể read/modify HTTP headers
- Backend encryption – Encrypt internal traffic từ LB đến servers
Global Server Load Balancing (GSLB)
GSLB distribute traffic across geographically distributed data centers:
- GeoDNS – Return different IPs based on user location
- Anycast – Multiple locations share same IP, routing directs to nearest
- Health-based routing – Failover to healthy datacenter
- Latency-based routing – Direct users to lowest latency endpoint
Common Load Balancer Configurations
Active-Passive (Failover)
Một load balancer active, một standby. Heartbeat giữa hai. Khi active fails, standby takes over. Expensive vì half capacity idle.
Active-Active
Multiple load balancers active, share traffic load. Higher utilization, better performance. Requires good health checking để handle failures.
Clustered
Multiple load balancers act as single logical unit. Scale horizontally by adding nodes. Most enterprise solutions use clustering.
Load Balancer Metrics
| Metric | Description |
|---|---|
| Requests per second | Throughput capacity |
| Latency (p50, p95, p99) | Response time distribution |
| Active connections | Current concurrent users |
| Backend error rate | Percentage of failed requests |
| Health check success rate | Backend availability |
NGINX Load Balancing Configuration
http {
upstream backend {
least_conn; # Least connections algorithm
server 10.0.0.1:8080 weight=5;
server 10.0.0.2:8080 weight=3;
server 10.0.0.3:8080 backup; # Backup server
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
HAProxy Configuration Example
frontend http_front
bind *:80
default_backend web_servers
backend web_servers
balance roundrobin
option httpchk
http-check expect status 200
server web1 10.0.0.1:8080 check inter 3s fall 3 rise 2
server web2 10.0.0.2:8080 check inter 3s fall 3 rise 2
server web3 10.0.0.3:8080 check inter 3s fall 3 rise 2
Kết Luận
Load balancing là critical component cho scalable, highly available applications. Từ simple round-robin DNS đến complex multi-layer architectures, load balancer đảm bảo users get fast, reliable access to services. Modern cloud platforms provide managed load balancing that scales automatically, nhưng understanding underlying principles giúp design better systems.
Các Câu Hỏi Thường Gặp (FAQ)
1. Load balancer khác gì với reverse proxy?
Load balancer focus on distributing load across multiple servers. Reverse proxy focus on acting as intermediary for clients, often with caching, security, và SSL termination. Many products (NGINX, HAProxy) serve both roles. Technically, load balancing là một chức năng của reverse proxy.
2. Khi nào nên dùng Layer 4 vs Layer 7 load balancing?
Layer 4 (L4) tốt cho high throughput với low latency, phù hợp cho TCP-based applications (databases, raw sockets). Layer 7 (L7) tốt cho HTTP/HTTPS traffic khi cần content-based routing, cookie-based persistence, hoặc URL-based routing. Most web applications dùng L7 vì flexibility.
3. Load balancer có single point of failure không?
Nếu chỉ có một load balancer, nó là single point of failure. Mitigate bằng cách deploy redundant load balancers trong active-passive hoặc active-active config. Cloud load balancers (AWS ELB, GCP LB) inherently highly available với built-in redundancy.
4. Làm sao load balancer handle SSL certificates?
SSL termination: Load balancer decrypts traffic (using configured certificate và private key), forward plaintext to backends. SSL bridging: Load balancer re-encrypts when forwarding to backends. Pass-through: Load balancer không decrypt, backends handle SSL directly (less common).
5. Load balancer có affect latency không?
Load balancer thường thêm 1-2ms latency. Modern hardware/software load balancers có very low overhead. L4 load balancers faster hơn L7 vì less inspection. Benefits (distribution, health checking, failover) far outweigh small latency addition.