Home » Network Load Balancing: Layer 4 vs Layer 7 และ Health Checks
Network Load Balancing: Layer 4 vs Layer 7 และ Health Checks
Network Load Balancing: Layer 4 vs Layer 7 และ Health Checks
Network Load Balancing คือการกระจาย traffic ไปยังหลาย servers เพื่อเพิ่ม capacity, availability และ performance ของ application Load balancer ทำหน้าที่รับ requests จาก clients แล้วส่งต่อไปยัง backend server ที่เหมาะสมที่สุด ตาม algorithm ที่กำหนด
การเลือก Layer 4 หรือ Layer 7 load balancing มีผลอย่างมากต่อ performance, flexibility และ cost Layer 4 ทำงานที่ TCP/UDP level (เร็ว) ส่วน Layer 7 ทำงานที่ HTTP/application level (ฉลาดกว่า) บทความนี้จะเปรียบเทียบทั้งสองและอธิบาย health check mechanisms
Layer 4 vs Layer 7
| คุณสมบัติ |
Layer 4 (Transport) |
Layer 7 (Application) |
| ตัดสินใจจาก |
IP, port, TCP/UDP header |
HTTP header, URL, cookie, content |
| Performance |
เร็วกว่า (less processing) |
ช้ากว่า (parse application data) |
| Content-based Routing |
ไม่ได้ (ไม่เห็น content) |
ได้ (route ตาม URL, header, cookie) |
| SSL Termination |
ไม่ได้ (pass-through) |
ได้ (decrypt → inspect → re-encrypt) |
| Session Persistence |
Source IP only |
Cookie-based, header-based |
| Health Check |
TCP port check (basic) |
HTTP status, content check (advanced) |
| Protocols |
ทุก TCP/UDP protocol |
HTTP, HTTPS, WebSocket, gRPC |
| Use Case |
Database, mail, gaming, non-HTTP |
Web applications, APIs, microservices |
Load Balancing Algorithms
| Algorithm |
วิธีทำงาน |
เหมาะกับ |
| Round Robin |
วนส่งทีละ server ตามลำดับ |
Servers spec เท่ากัน, stateless apps |
| Weighted Round Robin |
วนตามน้ำหนัก (server แรงกว่าได้มากกว่า) |
Servers spec ไม่เท่ากัน |
| Least Connections |
ส่งไป server ที่มี connections น้อยที่สุด |
Long-lived connections, varying request times |
| Weighted Least Connections |
Least connections + weight |
Mixed server specs + varying loads |
| IP Hash |
Hash source IP → same server เสมอ |
Session persistence (L4) |
| Least Response Time |
ส่งไป server ที่ respond เร็วที่สุด |
Performance-sensitive applications |
| Random |
สุ่มเลือก server |
Simple, stateless, many servers |
Health Check Types
| Health Check |
Layer |
วิธีตรวจ |
ตรวจอะไร |
| ICMP Ping |
L3 |
Ping server |
Server alive (ไม่รู้ว่า app ทำงานหรือไม่) |
| TCP Connect |
L4 |
TCP handshake ไป port |
Port open + service listening |
| HTTP GET |
L7 |
ส่ง HTTP GET /health |
HTTP 200 OK = healthy |
| HTTP Content Check |
L7 |
ตรวจ response body |
ดู content ว่ามีคำที่ต้องการ (e.g., “OK”) |
| Custom Script |
L7 |
รัน script ตรวจ app logic |
Database connection, queue depth, etc. |
Health Check Parameters
| Parameter |
ความหมาย |
ค่าแนะนำ |
| Interval |
ตรวจทุกกี่วินาที |
5-30 วินาที |
| Timeout |
รอ response นานเท่าไหร่ |
2-5 วินาที |
| Unhealthy Threshold |
fail กี่ครั้งถึงจะถือว่า unhealthy |
2-3 ครั้ง |
| Healthy Threshold |
pass กี่ครั้งถึงจะกลับมา healthy |
2-3 ครั้ง |
| Health Check Path |
URL สำหรับ HTTP health check |
/health หรือ /healthz |
Session Persistence (Sticky Sessions)
| Method |
Layer |
วิธีทำงาน |
ข้อดี/ข้อเสีย |
| Source IP |
L4 |
Same source IP → same server |
ง่าย / ปัญหาเมื่อผ่าน NAT (IP เดียว = หลาย users) |
| Cookie Insert |
L7 |
LB insert cookie → client ส่งกลับ |
แม่นยำ / ต้อง L7 LB |
| Cookie Rewrite |
L7 |
เปลี่ยน app cookie เพิ่ม server info |
ใช้ cookie ที่มีอยู่แล้ว |
| URL Rewrite |
L7 |
เพิ่ม server ID ใน URL parameter |
ไม่ต้อง cookie / URL ไม่สวย |
Load Balancer Solutions
| Solution |
Type |
L4/L7 |
ราคา |
| HAProxy |
Software |
L4 + L7 |
Free (open-source) |
| NGINX |
Software |
L4 + L7 |
Free / NGINX Plus ($$) |
| F5 BIG-IP |
Hardware/Virtual |
L4 + L7 |
$$$ (enterprise) |
| Citrix ADC (NetScaler) |
Hardware/Virtual |
L4 + L7 |
$$$ (enterprise) |
| AWS ALB/NLB |
Cloud |
ALB=L7, NLB=L4 |
Pay-per-use |
| Traefik |
Software |
L7 |
Free (cloud-native) |
| Envoy |
Software |
L4 + L7 |
Free (service mesh) |
Cloud Load Balancing
| Cloud |
L4 |
L7 |
Global |
| AWS |
NLB (Network LB) |
ALB (Application LB) |
Global Accelerator |
| GCP |
TCP/UDP LB |
HTTP(S) LB |
Global HTTP LB |
| Azure |
Azure Load Balancer |
Application Gateway |
Front Door |
ทิ้งท้าย: เลือก LB ให้ตรง Use Case
Layer 4 = เร็ว ง่าย สำหรับ non-HTTP protocols Layer 7 = ฉลาด content-based routing สำหรับ web/API Health checks สำคัญมาก: ใช้ HTTP check (L7) ดีกว่า TCP check (L4) HAProxy/NGINX = free + powerful สำหรับ most use cases Cloud LB = ง่าย scalable สำหรับ cloud workloads
อ่านเพิ่มเติมเกี่ยวกับ MPLS Traffic Engineering และ QoS Quality of Service ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com