Load Balancer: Layer 4 vs Layer 7 และ Health Check Design

Load Balancer: Layer 4 vs Layer 7 และ Health Check Design

Load Balancer เป็น device/software ที่กระจาย traffic ไปยังหลาย backend servers เพื่อเพิ่ม availability, scalability และ performance Load balancer มี 2 ประเภทหลัก: Layer 4 (transport level) ที่ทำงานบน TCP/UDP และ Layer 7 (application level) ที่เข้าใจ HTTP/HTTPS content

ทุก web application ที่ต้องการ high availability ต้องมี load balancer เพราะถ้า server ตัวเดียว down ทุกอย่างหยุด Load balancer ไม่เพียงกระจาย traffic แต่ยัง health check servers อัตโนมัติ ถ้า server ไหน fail จะหยุดส่ง traffic ไปโดยอัตโนมัติ

Layer 4 vs Layer 7

Feature Layer 4 (Transport) Layer 7 (Application)
ทำงานที่ TCP/UDP level (IP + Port) HTTP/HTTPS level (URL, headers, cookies)
Decision Based On Source/Dest IP + Port URL path, hostname, headers, cookies, content
Performance เร็วกว่า (ไม่ parse content) ช้ากว่า (parse HTTP)
SSL Termination ไม่ได้ (pass-through) ได้ (terminate SSL ที่ LB)
Content Routing ไม่ได้ Route ตาม URL, host header, etc.
Caching ไม่ได้ Cache static content ได้
WAF Integration ไม่ได้ ได้ (inspect HTTP content)
Use Case TCP services, database, high-performance Web apps, APIs, microservices

Load Balancing Algorithms

Algorithm วิธีทำงาน เหมาะกับ
Round Robin วนส่งทีละ server Servers มี capacity เท่ากัน
Weighted Round Robin วนตาม weight (server แรงกว่าได้มากกว่า) Servers มี capacity ต่างกัน
Least Connections ส่งไป server ที่มี active connections น้อยสุด Long-lived connections (WebSocket, DB)
IP Hash Hash source IP → same server เสมอ Session persistence (ไม่มี cookie)
Least Response Time ส่งไป server ที่ response เร็วสุด Heterogeneous server performance
Random สุ่มส่ง Simple, stateless

Health Check Types

Type วิธีตรวจ Depth
TCP Check เปิด TCP connection → success = healthy ตื้น (แค่ port เปิด)
HTTP Check ส่ง GET /health → check HTTP 200 OK ปานกลาง (app responding)
HTTP Content Check ส่ง GET /health → check body contains “OK” ลึก (app logic working)
Custom Script Run custom script ตรวจ DB, queue, disk ลึกสุด (full app health)

Health Check Design

Parameter ค่าแนะนำ หมายเหตุ
Interval 5-10 วินาที ถี่เกินไป = load บน servers
Timeout 3-5 วินาที < interval
Unhealthy Threshold 3 consecutive failures ป้องกัน flapping
Healthy Threshold 2 consecutive successes ยืนยันว่า recovered จริง
Health Endpoint /health หรือ /healthz Dedicated endpoint ไม่ใช่ homepage

Session Persistence (Sticky Sessions)

Method วิธีทำงาน ข้อดี/ข้อเสีย
Source IP Same IP → same server ง่าย แต่ NAT ทำให้หลาย users ไป server เดียว
Cookie-based LB ใส่ cookie → route ตาม cookie แม่นยำ แต่ต้อง L7 LB
URL Parameter Route ตาม session ID ใน URL L7 only, ไม่ secure
Application-level Shared session store (Redis/DB) Best: ไม่ต้อง sticky sessions เลย

SSL/TLS Termination

Mode วิธีทำงาน ข้อดี/ข้อเสีย
SSL Termination LB decrypt → ส่ง HTTP ไป backend ง่าย, LB ทำ SSL offload, backend เบา
SSL Pass-through LB ส่ง encrypted traffic ตรงไป backend End-to-end encryption แต่ LB ดู content ไม่ได้
SSL Re-encryption LB decrypt → re-encrypt → ส่ง HTTPS ไป backend LB inspect ได้ + encrypted to backend (most secure)

Popular Load Balancers

Solution Type จุดเด่น
NGINX Software (L7) High performance, reverse proxy, free/plus
HAProxy Software (L4/L7) Very high performance, widely used, open-source
AWS ALB Cloud (L7) Managed, auto-scale, deep AWS integration
AWS NLB Cloud (L4) Ultra-low latency, static IPs, millions of RPS
F5 BIG-IP Hardware/Virtual (L4/L7) Enterprise, advanced features, iRules
Citrix ADC Hardware/Virtual (L4/L7) Enterprise, application delivery
Traefik Software (L7) Cloud-native, auto-discovery, Let’s Encrypt
Envoy Software (L7) Service mesh, gRPC, observability

Best Practices

Practice รายละเอียด
Redundant LBs Active-passive หรือ active-active LB pair (HA)
Health checks ลึก HTTP health check ที่ตรวจ app + DB connectivity
SSL termination ที่ LB Offload SSL ที่ LB → backend ทำงานเบาขึ้น
Stateless backends ใช้ shared session store → ไม่ต้อง sticky sessions
Connection draining เมื่อ remove server → drain existing connections ก่อน
Rate limiting L7 LB ทำ rate limiting ป้องกัน abuse

ทิ้งท้าย: Load Balancer = HA + Scalability

Load Balancer กระจาย traffic + health check servers L4 = fast (TCP/UDP), L7 = smart (HTTP content routing) Health checks: HTTP 200 + content verify + proper thresholds SSL termination ที่ LB + stateless backends = best design Redundant LB pair + connection draining + rate limiting

อ่านเพิ่มเติมเกี่ยวกับ SSL TLS Best Practices และ DDoS Protection ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com

จัดส่งรวดเร็วส่งด่วนทั่วประเทศ
รับประกันสินค้าเคลมง่าย มีใบรับประกัน
ผ่อนชำระได้บัตรเครดิต 0% สูงสุด 10 เดือน
สะสมแต้ม รับส่วนลดส่วนลดและคะแนนสะสม

© 2026 SiamLancard — จำหน่ายการ์ดแลน อุปกรณ์ Server และเครื่องพิมพ์ใบเสร็จ

SiamLancard
Logo
Free Forex EA Download — XM Signal · EA Forex ฟรี
iCafeForex.com - สอนเทรด Forex | SiamCafe.net
Shopping cart