blog icon indicating copy to clipboard operation
blog copied to clipboard

负载均衡-P2C算法

Open TFdream opened this issue 4 years ago • 0 comments

常见的负载均衡算法

  • WRR (Weighted Round Robin):权重轮询
  • p2c:Power of Two Choices (P2C,两次随机选择)

P2C 算法介绍

Power of Two Choices (P2C,两次随机选择) 负载均衡算法,主要用于为每个 RPC 请求返回一个 Server 节点以供调用,该算法策略出自论文 《The Power of Two Random Choices: A Survey of Techniques and Results》 ,主要思路如下:

  1. 从可用后端节点列表中做 2 次选择操作(随机算法 or 依据一定策略来选择),得到节点 nodeA、nodeB
  2. 比较 nodeA、nodeB 两个节点,选出负载最低(一般是正在处理的连接数 / 请求数最少)的节点作为被选中的节点

伪代码如下:

nodeA = random_choice(nodes)
nodeB = random_choice(nodes)
best = least_connection_choice([nodeA, nodeB])

参考资料

TFdream avatar Aug 30 '21 07:08 TFdream