Skip to content

Load Balancing + Gateway

Mở đầu

Web app traffic peak → server quá tải → user thấy "Service Unavailable". Load balancer giải bằng cách chia traffic equal đến nhiều server. Gateway = entry duy nhất, đảm nhận auth, rate limit, routing.


1. Sao cần load balancing?

Single server bottleneck:

  • Perf: CPU/memory limit
  • Availability: down = service mất
  • Scale: vertical (lớn hơn) đắt + có ceiling

Solution: thêm server, chia traffic. Đây là horizontal scale.

       ┌─→ Server A
Client → LB ─→ Server B
       └─→ Server C

Load balancer phân phối request theo strategy → mỗi server xử 1 phần, total throughput tăng.


2. Load balancer là gì?

LB = thiết bị/software phân request đến nhiều server backend.

Levels

LevelProtocolVd
L4 (Transport)TCP/UDPAWS NLB, IPVS, HAProxy (L4 mode)
L7 (Application)HTTP/HTTPS, gRPCNginx, AWS ALB, Traefik, Envoy
L4L7
Fast, low overheadSlower (parse HTTP)
Không content-awareURL/header/cookie routing
TCP forwardSmart routing
1M+ connectionFewer concurrent

Distribution strategies

StrategyNoteUse
Round-robinTuần tự lặpServer config giống nhau
Weighted RRTheo weightServer config khác
Least connectionServer connection ít nhấtTime xử khác nhau
IP hashSame IP → same serverSession sticky
Response timeServer respond nhanh nhấtHeterogeneous backend

3. Core 1: Tránh server "broken" tiếp tục serve

LB regularly health check backend:

nginx
upstream backend {
    server 10.0.0.1:8080 max_fails=3 fail_timeout=30s;
    server 10.0.0.2:8080 max_fails=3 fail_timeout=30s;
    server 10.0.0.3:8080 max_fails=3 fail_timeout=30s backup;  # Backup, chỉ dùng khi main fail
}

Logic: 30s liên tiếp 3 fail → mark unhealthy → bỏ khỏi pool. Định kỳ retry, healthy lại → add lại.

Active health check: LB chủ động GET /health định kỳ. Passive health check: từ real traffic detect fail.


4. Core 2: "Old customer" tới cùng cashier

Một số scenario cần session affinity (sticky session): same user → same backend (vì state lưu local).

3 cách

CáchNotePro/Con
IP hashHash source IP → serverĐơn giản, nhưng nat IP đổi sticky vỡ
CookieLB inject cookie record serverMạnh nhất, cần parse HTTP
Session in RedisStateless backendTốt nhất (không cần sticky)

Modern recommendation: dùng external session store (Redis), backend stateless → no need sticky session, scale dễ.


5. Core 3: Zero-downtime deploy

Update version không stop service?

Rolling update

Initial:  Server A (v1) | Server B (v1) | Server C (v1)
Step 1:   Server A (v2) | Server B (v1) | Server C (v1)
Step 2:   Server A (v2) | Server B (v2) | Server C (v1)
Step 3:   Server A (v2) | Server B (v2) | Server C (v2)

LB liên tục health check, server đang restart bị bỏ tạm → user không thấy fail.

Blue-Green deploy

  • Blue (current): traffic 100%
  • Green (new version): deploy nhưng 0% traffic
  • Test Green OK → switch traffic 0% → 100%
  • Rollback nhanh: switch lại Blue

Canary deploy

Cho 1% traffic vào version mới, monitor metric. OK → tăng dần (5%, 25%, 50%, 100%). Fail → rollback nhanh.


6. Core 4: System tự "thở"

Auto Scaling: theo load, LB + auto scaler:

CPU avg >70% trong 5 phút → +1 instance
CPU avg <30% trong 10 phút → -1 instance
CloudService
AWSAuto Scaling Group
GCPManaged Instance Group
K8sHorizontal Pod Autoscaler (HPA)

7. Selection thực

UseRecommend
Cloud-managedAWS ALB/NLB, GCP Cloud LB, Azure LB
Self-host L7 simpleNginx, Caddy
Self-host L7 powerfulEnvoy, Traefik
Self-host L4HAProxy, IPVS
K8s ingressNginx Ingress, Traefik, Envoy-based (Contour, Emissary)
Service meshIstio, Linkerd (Envoy under)

8. Tổng kết: core thinking LB

  1. Horizontal scale: traffic tăng = thêm server, không upgrade single
  2. Health check: auto remove broken server, không cho serve
  3. Stateless backend: avoid sticky session, deploy + scale dễ
  4. Zero-downtime deploy: rolling / blue-green / canary
  5. Auto scaling: theo metric tự thêm/xoá instance

2026 cho VN dev

  • VN context:
    • Cloud LB managed (AWS ALB, VNG LB) cho startup
    • On-prem dùng Nginx, HAProxy
    • K8s phổ biến → Ingress Controller
  • Modern trends 2026:
    • Service mesh (Istio, Linkerd): handle service-to-service LB + observability
    • Gateway API (K8s): standardize Ingress alternative
    • eBPF-based LB: Cilium, faster
  • AI scenario: LLM API gateway (LiteLLM, Portkey) làm LB cho multiple LLM provider, cost + perf optimization

9. Glossary

TermPlain
Load BalancerPhân request đến nhiều server
Sticky SessionSame user → same server
Health CheckCheck server alive
Backend / UpstreamServer thực xử request
L4Transport layer (TCP/UDP)
L7Application layer (HTTP)
Auto ScalingAuto add/remove instance theo load