0Pricing
System Design Basics for Backend Developers · บทเรียน

กลยุทธ์การกระจายภาระงาน

เรียนรู้ว่าตัวกระจายภาระงานกระจายปริมาณการรับส่งข้อมูลไปยังเซิร์ฟเวอร์หลายเครื่องเพื่อรองรับการขยายในแนวนอนได้อย่างไร พร้อมสำรวจอัลกอริทึมการกำหนดเส้นทางและการตรวจสอบสุขภาพที่พบบ่อย

กลยุทธ์การกระจายภาระงาน เป็นบทเรียน System Design Basics for Backend Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน System Design Basics for Backend Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Load Balancing?

Once you scale horizontally, you have many identical servers. But how do clients know which one to hit? A load balancer sits in front of your servers and spreads incoming requests across them.

  • Prevents any single server from being overwhelmed
  • Enables seamless scaling up and down
  • Improves availability if one server fails

Where the Balancer Sits

A load balancer is a reverse proxy: clients connect to one address, and the balancer forwards the request to a healthy backend. The client never sees the internal topology.

This indirection is what makes adding or removing servers invisible to users.

Client --> [Load Balancer] --> Server A
                            --> Server B
                            --> Server C

Round Robin

Round robin is the simplest algorithm: requests are handed to servers in rotation. Server A, then B, then C, then back to A.

It works well when all servers are equally powerful and requests cost roughly the same.

requests = ['r1','r2','r3','r4']
servers = ['A','B','C']
for i, r in enumerate(requests):
    print(r, '->', servers[i % len(servers)])

Least Connections

Least connections routes each new request to the server with the fewest active connections. This is smarter when request durations vary widely.

A server stuck on slow requests will not keep receiving new ones.

Weighted Algorithms

If servers have different capacity, assign weights. A server with weight 3 receives roughly three times as many requests as a server with weight 1.

  • Weighted round robin
  • Weighted least connections

IP Hash & Sticky Sessions

IP hash routes a given client consistently to the same server based on a hash of its IP. This creates sticky sessions, useful when a server holds in-memory session state.

Note: stickiness undermines stateless design. Prefer external session stores when possible.

def pick(ip, n):
    return hash(ip) % n
print('192.168.0.5 ->', pick('192.168.0.5', 3))

Health Checks

A load balancer periodically pings each backend with a health check (e.g. GET /health). Unhealthy servers are removed from rotation automatically.

This is how the system survives a server crash without manual intervention.

Layer 4 vs Layer 7

Layer 4 balancing operates on TCP/UDP, routing by IP and port — fast but blind to content. Layer 7 operates on HTTP, so it can route by URL path, headers, or cookies.

  • L4: high throughput, simple
  • L7: content-aware, supports path-based routing

DNS Load Balancing

At the largest scale, a single load balancer becomes a bottleneck. DNS-based balancing returns different server IPs to different clients, spreading load before traffic even reaches a balancer.

Often combined with regional balancers for global apps.

Avoiding the Single Point of Failure

The load balancer itself must not be a single point of failure. Run it in an active-passive or active-active pair, with a floating virtual IP that fails over if the primary dies.

Putting It Together

A typical scalable setup: DNS spreads clients across regions, regional L7 balancers do health-checked path routing, and least-connections distributes to a fleet of stateless app servers behind them.

Each layer removes a bottleneck and adds resilience.

Quick Check

Test your understanding of load balancing algorithms.

Recap

You learned how load balancing makes horizontal scaling practical:

  • Round robin, least connections, weighted, and IP hash algorithms
  • Health checks remove failed servers automatically
  • Layer 4 vs Layer 7, plus DNS balancing at scale
  • The balancer must itself be redundant

คำถามที่พบบ่อย

บทเรียน “กลยุทธ์การกระจายภาระงาน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กลยุทธ์การกระจายภาระงาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส System Design Basics for Backend Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กลยุทธ์การกระจายภาระงาน”

เรียนรู้ว่าตัวกระจายภาระงานกระจายปริมาณการรับส่งข้อมูลไปยังเซิร์ฟเวอร์หลายเครื่องเพื่อรองรับการขยายในแนวนอนได้อย่างไร พร้อมสำรวจอัลกอริทึมการกำหนดเส้นทางและการตรวจสอบสุขภาพที่พบบ่อย คุณปฏิบัติ System Design Basics for Backend Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน System Design Basics for Backend Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน System Design Basics for Backend Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “กลยุทธ์การกระจายภาระงาน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน System Design Basics for Backend Developers นี้ได้ไหม

ได้ บทเรียน System Design Basics for Backend Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การปรับขนาดแนวตั้งเทียบกับแนวนอน
  2. Service แบบไร้สถานะเทียบกับแบบมีสถานะ
  3. บทนำสู่ระบบแบบกระจาย
  4. กลยุทธ์การกระจายภาระงาน
← กลับไปที่ System Design Basics for Backend Developers