"매 LB = traffic 분산 + health check + sticky". 알고리즘은 균등성과 affinity의 trade-off.
매 핵심
매 알고리즘
Round-robin: 순차. 단순, 동질 서버.
Weighted RR: 용량 차등.
Least connections: 활성 연결 최소. long-lived 적합.
Least response time: 평균 응답 + connection 수.
IP hash: client IP→server. session affinity.
Consistent hash: cache/shard. node 추가 시 K/n key만 재배치.
Random / P2C: power of two choices, least-loaded 선택. 간단·강력.
매 Layer
L4 (TCP/UDP): 빠름, opaque. NLB, HAProxy TCP mode.
L7 (HTTP): header/path 기반 라우팅, TLS 종료. ALB, Nginx, Envoy.
매 Health check
Active: /health 주기적 GET
Passive: 실제 요청 실패 카운트
Slow start: 신규 노드에 점진적 traffic
Outlier detection (Envoy): 연속 5xx → eject
매 Session affinity (sticky)
Cookie-based (LB가 cookie 발급)
Source IP (NAT 뒤에 모이면 unbalanced)
Header-based (custom)
매 응용
Web app: ALB + multi-AZ
gRPC/HTTP2: Envoy (L7 multiplex)
DB read replica: ProxySQL, PgBouncer
Cache shard: consistent hash (Redis cluster)
Service mesh: Envoy sidecar (Istio, Linkerd)
💻 패턴
Nginx upstream
upstreambackend{least_conn;# algorithm
serverapp1:8080weight=3max_fails=3fail_timeout=30s;serverapp2:8080weight=1;serverapp3:8080backup;# only if others down
keepalive32;}server{location/{proxy_passhttp://backend;proxy_set_headerHost$host;proxy_next_upstreamerrortimeouthttp_502http_503;}}
HAProxy
backend web
balance leastconn
option httpchk GET /health
server s1 10.0.0.1:8080 check weight 100
server s2 10.0.0.2:8080 check weight 50
cookie SRV insert indirect nocache # sticky