Skip to main content

Load Balance

策略

  • 静态策略 - 适用于冷启动
    • RR - Round Robin
    • WRR - Weighted Round Robin - 加权轮训
  • 简单动态策略
    • least_conn - 最少连接
      • 只知道“忙不忙”(连接数),但不知道“为什么忙”
      • 无法区分“处理快但连接多”和“处理慢但连接少”
      • 对网络抖动(导致连接建立慢)不敏感
  • Hash-Based
    • 适用于状态场景;需要会话保持
    • chash - consistent hashing - 一致性哈希
    • uri
    • source
    • url_param
    • hdr
    • request_id
  • SMA - simple moving average - 简单移动平均
    • 计算最近 N 次请求的平均延迟
    • 局限性
      • 反应慢,对峰值不敏感
      • 如果 N=10 上游突然变慢,需要 10 个慢请求才能完全意识到问题,对于恢复也是一样。
  • EWMA - Exponentially Weighted Moving Average - 指数加权移动平均
  • Peak EWMA
  • aperture
  • p2c - power of two choices
  • HAProxy balance
    • roundrobin, static-rr, leastconn, first, hash, source, uri, url_param, hdr, random, rdp-cookie, log-hash, sticky
  • Finagle Load Balancing
    • 计算型场景
    • Heap + Least Loaded
    • P2C + {Least Loaded, Peak EWMA}
    • Aperture + Least Loaded
# https://flugel.it/kubernetes/kubernetes-nginx-ingress-consistent-hash-subset-load-balancer/
nginx.ingress.kubernetes.io/load-balance: ewma
nginx.ingress.kubernetes.io/upstream-hash-by: ewma

NOTE

  • LB 需要考虑
    • 支持的协议层级
    • 支持的应用协议
    • 支持的负载权重因子
  • LB
    • 优化 性能/Latency/Throughput
    • 优化 可靠性/Reliability/Error Rate
  • 复杂场景 - 多维度目标优化策略
    • 价格
    • 性能
    • 可靠性