Home » Network Load Balancing: L4 vs L7, HAProxy, Nginx และ F5
Network Load Balancing: L4 vs L7, HAProxy, Nginx และ F5
Network Load Balancing: L4 vs L7, HAProxy, Nginx และ F5
Network Load Balancing กระจาย traffic ไปยัง backend servers หลายตัวเพื่อเพิ่ม availability, scalability และ performance Load balancer ทำงานได้ทั้ง Layer 4 (TCP/UDP) และ Layer 7 (HTTP/HTTPS) HAProxy และ Nginx เป็น open-source solutions ที่นิยมมาก ส่วน F5 BIG-IP เป็น enterprise-grade hardware/virtual appliance
Single server มี capacity จำกัด — ถ้า traffic เกินจะ crash หรือ response time สูงจนใช้งานไม่ได้ Load balancer แก้ปัญหานี้: กระจาย requests ไปหลาย servers + health check เพื่อตรวจสอบว่า server ยังทำงานอยู่ + failover อัตโนมัติเมื่อ server ล่ม
L4 vs L7 Load Balancing
| Feature |
Layer 4 (Transport) |
Layer 7 (Application) |
| Works on |
TCP/UDP (IP + Port) |
HTTP/HTTPS (URL, headers, cookies) |
| Decision based on |
Source/dest IP, port |
URL path, hostname, headers, cookies, content |
| Performance |
Faster (less processing) |
Slower (parse application data) |
| SSL Termination |
ไม่มี (pass-through) |
มี (terminate SSL at LB) |
| Content Routing |
ไม่ได้ (ไม่เห็น content) |
ได้ (/api → server A, /static → server B) |
| Session Persistence |
Source IP based |
Cookie-based (more reliable) |
| Protocol Support |
Any TCP/UDP protocol |
HTTP, HTTPS, WebSocket, gRPC |
| Use Case |
Database, mail, non-HTTP |
Web apps, APIs, microservices |
Load Balancing Algorithms
| Algorithm |
วิธีทำงาน |
เหมาะกับ |
| Round Robin |
วนส่ง request ไปทีละ server ตามลำดับ |
Servers เหมือนกัน (equal capacity) |
| Weighted Round Robin |
Round robin แต่ server แรงกว่าได้ weight มากกว่า |
Servers ต่าง capacity |
| Least Connections |
ส่งไป server ที่มี active connections น้อยที่สุด |
Long-lived connections (WebSocket, DB) |
| Weighted Least Connections |
Least connections + weight factor |
Mixed server sizes + long connections |
| IP Hash |
Hash source IP → same server ทุกครั้ง |
Session persistence without cookies |
| Least Response Time |
ส่งไป server ที่ response time ต่ำที่สุด |
Performance-sensitive applications |
| Random |
สุ่มเลือก server |
Simple, stateless |
HAProxy
| Feature |
รายละเอียด |
| Type |
Open-source L4/L7 load balancer + reverse proxy |
| Performance |
Very high (C-based, event-driven, millions of connections) |
| Config |
haproxy.cfg (frontend → backend → server definitions) |
| Health Checks |
TCP, HTTP, custom scripts |
| SSL |
SSL termination + SSL passthrough |
| ACLs |
Content-based routing (URL, headers, cookies) |
| Stats |
Built-in stats dashboard (real-time monitoring) |
| Used by |
GitHub, Stack Overflow, Reddit, Airbnb |
Nginx (as Load Balancer)
| Feature |
รายละเอียด |
| Type |
Web server + reverse proxy + L7 load balancer |
| Performance |
High (event-driven, low memory) |
| Config |
nginx.conf (upstream block → server definitions) |
| Algorithms |
Round robin, least_conn, ip_hash, hash |
| SSL |
SSL termination (strong TLS support) |
| Caching |
Built-in content caching (proxy_cache) |
| Plus (Commercial) |
Active health checks, session persistence, API, dashboard |
| Used by |
Netflix, Dropbox, WordPress.com |
F5 BIG-IP
| Feature |
รายละเอียด |
| Type |
Enterprise ADC (Application Delivery Controller) |
| Form Factors |
Hardware appliance, Virtual Edition (VE), Cloud (AWS/Azure) |
| LTM (Local Traffic Manager) |
L4-L7 load balancing + SSL offloading + connection optimization |
| GTM (Global Traffic Manager) |
DNS-based global load balancing (GSLB) |
| ASM (Application Security) |
WAF (Web Application Firewall) |
| iRules |
TCL-based scripting สำหรับ advanced traffic manipulation |
| Monitors |
Advanced health checks (custom scripts, content matching) |
| Used by |
Banks, telecom, large enterprises |
Comparison
| Feature |
HAProxy |
Nginx |
F5 BIG-IP |
| Cost |
Free (open-source) |
Free (Plus = paid) |
Expensive (license + hardware) |
| L4 LB |
Excellent |
Good (stream module) |
Excellent |
| L7 LB |
Excellent |
Excellent |
Excellent |
| Performance |
Very high |
Very high |
Very high (hardware offload) |
| SSL Offload |
Good |
Good |
Excellent (hardware SSL) |
| WAF |
ไม่มี (ใช้ ModSecurity) |
Basic (Plus) / ModSecurity |
Built-in ASM |
| GSLB |
ไม่มี (ใช้ DNS) |
ไม่มี (Plus limited) |
Built-in GTM |
| Config Complexity |
ปานกลาง |
ต่ำ |
สูง |
Cloud Load Balancers
| Cloud |
L4 LB |
L7 LB |
Global LB |
| AWS |
NLB (Network LB) |
ALB (Application LB) |
Global Accelerator |
| Azure |
Azure Load Balancer |
Application Gateway |
Front Door / Traffic Manager |
| GCP |
Network LB |
HTTP(S) LB |
Cloud CDN + Global LB |
Health Check Types
| Type |
วิธีเช็ค |
ตรวจอะไร |
| TCP |
TCP connect to port |
Port เปิดอยู่ (basic) |
| HTTP |
HTTP GET /health → check status code |
Application responding (200 OK) |
| Content |
HTTP GET → check response body contains string |
Application returning correct data |
| Custom Script |
Run script → check exit code |
Complex checks (DB connectivity, queue depth) |
Best Practices
| Practice |
รายละเอียด |
| ใช้ L7 สำหรับ web/API |
Content routing, SSL termination, cookie persistence |
| ใช้ L4 สำหรับ non-HTTP |
Database, mail, custom TCP protocols |
| Health checks ต้อง meaningful |
อย่าแค่ check TCP port — check application health endpoint |
| SSL termination at LB |
Offload SSL ที่ LB → backend ใช้ HTTP (ลด CPU backend) |
| HA สำหรับ LB เอง |
LB เป็น single point of failure — ต้อง HA pair (active-standby) |
| Connection draining |
เมื่อ remove server → drain existing connections ก่อน |
ทิ้งท้าย: Load Balancing = Availability + Scalability
Load Balancing L4 = TCP/UDP (fast, simple), L7 = HTTP (content routing, SSL) HAProxy = best open-source LB, Nginx = web server + LB, F5 = enterprise ADC Round robin (simple), least connections (long-lived), IP hash (persistence) Health checks ต้อง meaningful (HTTP /health, not just TCP) LB ต้อง HA pair + SSL termination + connection draining
อ่านเพิ่มเติมเกี่ยวกับ Reverse Proxy Architecture และ High Availability Design ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com