load balancer
四层/七层load balancing与扩展方式
Load balancing 让我们把 incoming network traffic 分配到多个 resources,只把请求发给在线的节点,从而保证 high availability 与 reliability。这也让我们可以按 demand 灵活增减 resources。
为了更高的 scalability 和 redundancy,系统的不同层都可以做 load balancing:

为什么需要 load balancing?
高流量网站可能要处理几十万甚至百万级并发请求。要以更低成本扩展,就需要增加更多 servers。
load balancer 位于 servers 前,按规则把请求路由到可处理的 server,上限利用 capacity 并提升速度,避免单台 server 过载导致 performance 降低。
当某台 server 掉线时,load balancer 会把流量导向其他 online servers;新增 server 时也能自动开始分流。
Health checks
为了避免把流量打到坏节点,load balancer 通常依赖 health checks(主动/被动)来判断节点状态。
- Active checks:定时探测(HTTP/TCP/ping),失败就摘除
- Passive checks:基于真实流量失败率/超时率判断

Static vs Dynamic algorithms
- Static:只用静态配置(权重、硬件规格),简单但不够鲁棒
- Dynamic:用实时指标(latency/active connections/CPU),更适合波动流量

Workload distribution
这是 load balancer 的核心能力,常见的分配方式包括:
- Host-based:按 hostname 分配
- Path-based:按完整 URL 路径分配
- Content-based:检查请求内容(如参数),按内容分配
Layers
load balancer 通常在两个层级工作:
Network layer
工作在 Transport layer(Layer 4),基于 IP 等网络信息路由,不能做 content-based routing。常见为高性能的专用硬件。
Application layer
工作在 Application layer(Layer 7),能读取完整请求,支持 content-based routing,从而更精细地管理流量。
Types
load balancer 常见类型:
Software
Software load balancer 更易部署、成本更低、灵活性更高,适合和 software 环境结合。灵活性提升的代价是 setup 工作更多。相比封闭的硬件设备,软件方案更容易改动与升级。
软件 load balancer 可以是自建可安装方案,也可以是 managed cloud service。
Hardware
Hardware load balancer 依赖 on-prem 物理设备,能处理很高流量,但成本高、灵活性较低。
硬件 load balancer 通常有专有 firmware,需要维护与升级补丁。
DNS
DNS load balancing 通过 DNS 把请求分配到多台 servers。
但 DNS load balancing 天生有局限:DNS 不会检测 server/network 是否故障,可能仍返回不可用的 IP,从而影响 reliability 与效率。
Routing Algorithms
常见 routing algorithms:
- Round-robin:轮询分配
- Weighted Round-robin:按权重分配
- Least Connections:分配给当前连接最少的 server
- Least Response Time:结合最快响应 + 最少连接
- Least Bandwidth:分配给当前带宽占用最少的 server
- Hashing:按 key(client IP / URL)做一致性分配
1) Random
最简单:随机挑一个 server。只在容量接近、负载稳定时效果好。
2) Round Robin
按序分发请求,简单但不感知负载。

3) Weighted Round Robin
给每台 server 分配权重(CPU/内存/性能),按比例分流。

4) Sticky Round Robin
同一 client 在 session 内固定打到同一台 server,常配合 session cache。

5) Least Connections
把新请求发给当前 active connections 最少的 server。

6) Weighted Least Connections
在 least connections 基础上再考虑权重:connections / weight 最小者获胜。
7) Least Response Time
选择最近窗口内 response time 最低的 server,可与 least connections 组合。

8) IP Hashing
把 client IP 做 hash 映射到固定 server,保证 locality。常与 consistent hashing 搭配,避免节点变动导致大规模 re-route。

9) URL Hashing
对 URL(或 path)做 hash,把相同资源的请求路由到同一 server。

10) Combination / Voting
多个策略投票选结果(例如按 3 个算法投票),兼顾稳定性与性能。

Advantages
load balancing 还能降低 downtime,优势包括:
- Scalability
- Redundancy
- Flexibility
- Efficiency
Redundant load balancers
load balancer 本身可能是 single point of failure,因此常用双机或多实例 cluster。
当 active load balancer 失效,passive 会接管,提升 fault tolerance。

Features
常见的 load balancer features:
- Autoscaling:按 demand 自动扩缩容
- Sticky sessions:同一用户固定路由到同一资源
- Healthchecks:剔除异常节点
- Persistent connections:支持 WebSocket 等长连接
- Encryption:处理 TLS/SSL
- Certificates:证书校验
- Compression:响应压缩
- Caching:应用层缓存
- Logging:请求/响应日志
- Request tracing:为请求分配唯一 id
- Redirects:按路径/规则重定向
- Fixed response:返回固定响应(如错误页)
Trade-offs
- Sticky sessions vs Stateless:sticky 简化 session,但 failover 更难
- Dynamic algorithms:更准确但有监控开销
- L7 routing:更智能但性能成本更高
Examples
常见方案: