การกระจายโหลดและการค้นหาบริการ
ทำความเข้าใจวิธีที่ระบบหลังบ้านของซอฟต์แวร์บริการกระจายทราฟฟิกไปยังอินสแตนซ์จำนวนมาก โดยใช้ตัวกระจายโหลด การตรวจสอบสุขภาพ และการค้นหาบริการ
การกระจายโหลดและการค้นหาบริการ เป็นบทเรียน SaaS Architecture & Startup Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SaaS Architecture & Startup Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SaaS Architecture & Startup Engineering มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Need for Load Balancing
When one server cannot handle all traffic, you run many copies. A load balancer sits in front and spreads incoming requests across these instances.
This is the backbone of horizontal scaling for SaaS backends.
How a Load Balancer Works
Clients connect to a single address. The load balancer accepts the request and forwards it to one of the backend servers, then relays the response back.
To the client, the cluster looks like one powerful server.
Round Robin
The simplest algorithm is round robin: requests are handed to servers in rotation. Each server gets an equal share.
const servers = ['s1', 's2', 's3'];
let i = 0;
function next() {
const s = servers[i % servers.length];
i++;
return s;
}
console.log(next(), next(), next(), next());Least Connections
Least connections routes each new request to the server currently handling the fewest active connections.
This adapts better than round robin when requests have uneven durations.
Layer 4 vs Layer 7
Load balancers operate at different network layers:
- Layer 4 (transport) — routes by IP and port, very fast
- Layer 7 (application) — inspects HTTP, can route by URL path or headers
Layer 7 enables smart routing like sending /api to one pool and /static to another.
Health Checks
A load balancer must avoid sending traffic to dead servers. It runs periodic health checks against each instance.
A common pattern is a /health endpoint returning 200 OK when the app is ready.
app.get('/health', (req, res) => {
res.status(200).json({ status: 'ok' });
});Sticky Sessions
Some apps store session state in server memory. Sticky sessions pin a client to the same server so their session persists.
Better practice: keep servers stateless and store sessions in a shared store, so any server can handle any request.
Service Discovery
In dynamic environments, servers come and go constantly. Service discovery keeps an up-to-date registry of which instances exist and are healthy.
Tools like Consul, etcd, or Kubernetes services automate this.
Client-Side vs Server-Side Discovery
Two models:
- Server-side — clients hit a load balancer that consults the registry
- Client-side — clients query the registry directly and pick an instance themselves
Server-side is simpler; client-side reduces a network hop.
Autoscaling Integration
Load balancers pair with autoscaling: when traffic rises, new instances spin up, register with discovery, and the balancer starts routing to them automatically.
When traffic drops, instances are removed gracefully after draining connections.
Graceful Draining
Before shutting down an instance, the balancer should stop sending new requests but let existing ones finish. This is connection draining.
It prevents dropped requests during deployments and scale-down events.
Quick Check
Test your load balancing knowledge.
Recap
You learned how SaaS backends distribute traffic:
- Load balancers with round robin and least connections
- Layer 4 vs Layer 7 routing and health checks
- Stateless design, service discovery, and graceful draining
Together these let a backend scale horizontally and survive failures.
คำถามที่พบบ่อย
บทเรียน “การกระจายโหลดและการค้นหาบริการ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกระจายโหลดและการค้นหาบริการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SaaS Architecture & Startup Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SaaS Architecture & Startup Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกระจายโหลดและการค้นหาบริการ”
ทำความเข้าใจวิธีที่ระบบหลังบ้านของซอฟต์แวร์บริการกระจายทราฟฟิกไปยังอินสแตนซ์จำนวนมาก โดยใช้ตัวกระจายโหลด การตรวจสอบสุขภาพ และการค้นหาบริการ คุณปฏิบัติ SaaS Architecture & Startup Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SaaS Architecture & Startup Engineering หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SaaS Architecture & Startup Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การกระจายโหลดและการค้นหาบริการ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SaaS Architecture & Startup Engineering นี้ได้ไหม
ได้ บทเรียน SaaS Architecture & Startup Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เทคนิคการปรับขนาดในแนวนอน
- คิวข้อความและสถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์
- พื้นฐานสถาปัตยกรรมไร้เซิร์ฟเวอร์
- การกระจายโหลดและการค้นหาบริการ