Summary

Manjusaka implements common load balancing algorithms in Python using a Strategy pattern abstraction, covering: random selection, weighted random, round-robin, weighted round-robin (Smooth Weighted Round Robin), and consistent hashing. Each algorithm is presented with working code and analysis of its tradeoffs. The Strategy pattern allows algorithms to be swapped transparently.

Manjusaka 用 Python 以策略模式抽象實現常見的負載均衡算法:隨機選擇、加權隨機、輪詢、平滑加權輪詢和一致性哈希,每個算法附帶可工作的代碼和利弊分析。

Key Points

  • Strategy pattern: all algorithms share get_node(ctx) -> Node interface, enabling transparent algorithm swapping
  • Random: simple but unpredictable distribution — quality depends on RNG quality
  • Weighted Random: random.choices() with weights for non-uniform traffic distribution
  • Round Robin: predictable, fair distribution; simple index-based implementation
  • Smooth Weighted Round Robin: avoids burst traffic to highest-weight nodes by distributing weight proportionally over time
  • Consistent Hashing: maps nodes to hash ring; node addition/removal only redistributes a small fraction of keys

Insights

The key differentiator between weighted random and smooth weighted round-robin is temporal distribution: weighted random might send many consecutive requests to the highest-weight node (due to random clustering), while SWRR guarantees a smooth distribution over time. Consistent hashing solves the resharding problem elegantly: instead of remapping all keys when a node changes, only the keys that “belong” to the changed node need to move.

Connections

Raw Excerpt

Random 确实是我们非常常用的一套负载均衡算法,但是缺点也很明显,其负载均衡的效果有一定的不可预测性,是神是鬼全靠你使用的 Random 函数的质量。