SaaS Architecture & Startup Engineering · 课时

负载均衡与服务发现

了解 SaaS 后端如何借助负载均衡器、健康检查和服务发现,将流量分配到多个实例。

第 4 / 4 课13 个步骤

负载均衡与服务发现 是 CoddyKit 上的免费 SaaS Architecture & Startup Engineering 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师学习 SaaS Architecture & Startup Engineering — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「负载均衡与服务发现」课时是免费的吗?

是的 — 「负载均衡与服务发现」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SaaS Architecture & Startup Engineering 课程的其余内容,请升级到 CoddyKit PRO。 SaaS Architecture & Startup Engineering 课程共包含 4 节课。

「负载均衡与服务发现」这节课中我会学到什么?

了解 SaaS 后端如何借助负载均衡器、健康检查和服务发现,将流量分配到多个实例。 你通过在浏览器中直接运行的动手代码来练习 SaaS Architecture & Startup Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 SaaS Architecture & Startup Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 SaaS Architecture & Startup Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「负载均衡与服务发现」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 SaaS Architecture & Startup Engineering 课中编写并运行代码吗?

能。每节 SaaS Architecture & Startup Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 水平扩展技术
  2. 消息队列与事件驱动
  3. 无服务器架构基础
  4. 负载均衡与服务发现
← 返回 SaaS Architecture & Startup Engineering