System Design Basics for Backend Developers · 课时

负载均衡策略

学习负载均衡器如何将流量分配到多个服务器以实现水平扩展,并探索常见的路由算法和健康检查。

第 4 / 4 课13 个步骤

负载均衡策略 是 CoddyKit 上的免费 System Design Basics for Backend Developers 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师学习 System Design Basics for Backend Developers — 免费

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

课程
12
课程
48

常见问题解答

「负载均衡策略」课时是免费的吗?

是的 — 「负载均衡策略」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 System Design Basics for Backend Developers 课程的其余内容,请升级到 CoddyKit PRO。 System Design Basics for Backend Developers 课程共包含 4 节课。

「负载均衡策略」这节课中我会学到什么?

学习负载均衡器如何将流量分配到多个服务器以实现水平扩展,并探索常见的路由算法和健康检查。 你通过在浏览器中直接运行的动手代码来练习 System Design Basics for Backend Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 System Design Basics for Backend Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 System Design Basics for Backend Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 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