Home » Network Load Balancing: L4 vs L7, HAProxy, Nginx, GSLB, DSR และ Health Checks
Network Load Balancing: L4 vs L7, HAProxy, Nginx, GSLB, DSR และ Health Checks
Network Load Balancing: L4 vs L7, HAProxy, Nginx, GSLB, DSR และ Health Checks
Network Load Balancing กระจาย traffic ไปยัง backend servers เพื่อเพิ่ม availability และ performance L4 Load Balancing ทำงานที่ transport layer (TCP/UDP), L7 Load Balancing ทำงานที่ application layer (HTTP/HTTPS), HAProxy เป็น high-performance open-source load balancer, Nginx ให้ทั้ง web server และ load balancing, GSLB กระจาย traffic ระหว่าง data centers และ DSR ส่ง response กลับ client โดยตรงไม่ผ่าน load balancer
Modern web applications ต้อง handle millions of concurrent connections : single server ไม่เพียงพอ load balancer กระจาย traffic → horizontal scaling, high availability (ถ้า server ตัวหนึ่งล่ม traffic ไปตัวอื่นอัตโนมัติ), zero-downtime deployments (rolling update ทีละ server) ทุก large-scale application ต้องมี load balancer
L4 vs L7 Load Balancing
Feature
L4 (Transport)
L7 (Application)
Layer
TCP/UDP (IP + port)
HTTP/HTTPS (URL, headers, cookies)
Decision Based On
Source/dest IP, port
URL path, host header, cookies, HTTP method
Performance
สูงมาก (simple packet forwarding)
ต่ำกว่า (must parse HTTP — but still very fast)
SSL Termination
ไม่ได้ (pass-through)
ได้ (terminate SSL → inspect HTTP → re-encrypt optional)
Content Routing
ไม่ได้
ได้ (/api → backend A, /static → backend B)
Sticky Sessions
Source IP only
Cookie-based, header-based (more flexible)
Use Case
TCP services (database, SMTP, gaming), ultra-high throughput
Web applications, API gateway, microservices
Load Balancing Algorithms
Algorithm
How
Best For
Round Robin
วนรอบ servers ทีละตัว (1→2→3→1→2→3)
Equal-capacity servers, stateless apps
Weighted Round Robin
Round robin แต่ server ที่ weight สูงได้ traffic มากกว่า
Servers ที่มี capacity ต่างกัน
Least Connections
ส่งไป server ที่มี active connections น้อยที่สุด
Long-lived connections, variable request processing time
IP Hash
Hash source IP → same client ไป same server เสมอ
Session persistence without cookies (L4)
Least Response Time
ส่งไป server ที่ response เร็วที่สุด
Performance-critical applications
Random
สุ่มเลือก server
Simple, surprisingly effective at scale
HAProxy
Feature
รายละเอียด
คืออะไร
High-performance open-source TCP/HTTP load balancer (C-based, single-threaded event-driven)
Performance
Millions of connections, 100K+ req/sec per core
L4 + L7
Both TCP (L4) and HTTP (L7) load balancing
SSL Termination
Native SSL/TLS termination with OpenSSL
ACLs
Powerful ACL system สำหรับ content-based routing
Health Checks
TCP, HTTP, custom script health checks
Stats Page
Built-in statistics dashboard (real-time metrics)
Users
GitHub, Reddit, Stack Overflow, Airbnb, Instagram
Nginx Load Balancing
Feature
รายละเอียด
คืออะไร
Web server + reverse proxy + load balancer (C-based, event-driven)
Upstream
Define backend server groups ใน upstream block
Methods
round_robin (default), least_conn, ip_hash, hash
SSL
SSL termination, SSL passthrough, mutual TLS
Caching
Built-in response caching (proxy_cache)
Plus
Nginx Plus (commercial): active health checks, session persistence, live dashboard
vs HAProxy
Nginx = web server + LB | HAProxy = dedicated LB (HAProxy usually faster for pure LB)
GSLB (Global Server Load Balancing)
Feature
รายละเอียด
คืออะไร
Load balancing ระหว่าง data centers/regions (DNS-based หรือ Anycast)
DNS-Based
DNS resolve ไป nearest/healthiest data center (geo-based, health-aware)
Anycast
Same IP announced จากหลาย locations → BGP route ไป nearest
Failover
ถ้า DC หนึ่งล่ม → DNS/Anycast route ไป DC อื่นอัตโนมัติ
Geo Routing
Route users ไป nearest DC (Asia → Singapore, US → Virginia)
Solutions
Cloudflare, AWS Route 53, F5 GTM, Citrix GSLB, NS1
DSR (Direct Server Return)
Feature
รายละเอียด
คืออะไร
Response จาก backend server ส่งกลับ client โดยตรง (ไม่ผ่าน LB)
How
LB เปลี่ยน MAC address → forward to backend | Backend ตอบกลับ client ตรงๆ
Benefit
LB handle แค่ inbound traffic → ลด LB bandwidth 10× (response มักใหญ่กว่า request)
Limitation
L4 only (ไม่ได้ inspect HTTP), same subnet required, more complex setup
Use Case
Video streaming, file downloads (large responses)
Health Checks
Type
How
Reliability
TCP Check
TCP connect to port → success = healthy
Basic (port open ≠ app working)
HTTP Check
Send HTTP request → check status code (200 OK)
Good (verify app responds)
Content Check
HTTP request → check response body contains expected string
Better (verify app returns correct content)
Custom Script
Run custom health check script
Best (check database, dependencies, queue depth)
Passive
Monitor real traffic → mark unhealthy if too many errors
Real-time (detect issues from actual traffic)
ทิ้งท้าย: Load Balancing = Scale and Survive
Network Load Balancing L4: fast (TCP/UDP, IP+port) | L7: smart (HTTP, URL, headers, cookies, SSL termination) Algorithms: round robin (simple), least connections (adaptive), IP hash (persistence) HAProxy: dedicated LB (millions conn, 100K+ req/s), ACLs, stats dashboard Nginx: web server + LB, upstream groups, caching, Nginx Plus (commercial) GSLB: multi-DC load balancing (DNS-based, Anycast, geo routing, failover) DSR: response bypasses LB → 10× bandwidth savings (video, downloads) Health Checks: TCP (basic), HTTP (good), content (better), custom script (best), passive (real-time)
อ่านเพิ่มเติมเกี่ยวกับ API Gateway Kong Apigee Rate Limiting และ CDN Architecture Edge Computing Caching ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com