Introduction to the

It is true that this article has dragged on for a long time. The last section covered the service load balancing implementation, but if you need to call a remote service, how do you ensure that the calls are not concentrated on one service? How do you ensure that the remote service calls are load-balanced? This is to realize the load balancing of Consumer calling RPC. Therefore, this article mainly explains the implementation of RPC load balancing algorithm.

algorithm

Here are a few of the main load balancing algorithms to implement, you can see the NPM package LOAD-balancer-algorithm I wrote.

const LBA = require('load-balancer-algorithm');

const weightRandomPool = [
  { host: "127.0.0.2".weight: 2 },
  { host: "127.0.0.1".weight: 3 },
  { host: "127.0.0.3".weight: 5},];const weightedList = []
const loadBalance = new LBA.WeightedRoundRobin(weightRandomPool);

for(let i = 0; i < 10; i++){
  const address = loadBalance.pick();
  weightedList.push(loadBalance.getWeight(address.host))
}
// [5, 5, 3, 5, 2, 3, 5]
console.log(weightedList)
Copy the code

Round Robin

The Round Robin algorithm, which polls the nodes of the service queue in turn, goes Round and Round, is relatively simple to implement.

  • The consumer invokes balancing
  • The current subscript +1 is used to modulo the number of data pool lengths to obtain the next node subscript

Disadvantages:

  • The service provider nodes are required to perform consistently

Weighted Round Robin

Weighted round robin algorithm, which adds weight judgment based on round robin to reduce traffic for low-performance service nodes.

  • Dynamic weight adjustment real-time synchronization
  • A relatively balanced algorithm
  • However, heavy service nodes can suddenly increase the load, so there are againSmooth weight polling(Smooth Weighted Round Robin), which is the smoothly distributed acquisition node
[{host: "127.0.0.1".weight: 2 },
  { host: "127.0.0.2".weight: 3 },
  { host: "127.0.0.3".weight: 5},]// normal
// [5, 5, 5, 5, 5, 3, 3, 3, 2, 2]
// smooth
// [5, 5, 3, 5, 2, 3, 5]
Copy the code

Source IP Hash

The Source IP Hash (consistent Hash) algorithm is recommended for consistently allocating client IP requests to one server so that the same session can be obtained.

  • Through the nodeIPGenerate NhashAnd then throughhashGets the values of N virtual nodes, which are then generated with the request data (IP or parameters)hashCompare the closest nodes
  • Nodejs mainsticky sessionsPattern use to ensure access to the same servicesession

Disadvantages:

  • Compared with other algorithms, load balancing and performance are lower
  • Fixed IP service down, usersessionIt will be lost, so the best way to do it issessionShared

For example, as shown in the figure, two virtual nodes are generated for each node, and the hash value is generated based on the request data to compare which value is the most similar to determine which node to access

Random

Random algorithm, randomly generate an integer in the range of pool length, so as to randomly obtain machines.

  • The collision rate is high, but the larger the dosage, the more even the distribution
  • A random value in the range 0 to pool.length

Weighted Random

** Weighted random ** algorithm. If the performance of the machine is inconsistent, weight calculation is added on the basis of randomness.

  • A random value of 0 to totalWeighted is then subtractedpoolIf the node weight is less than 0, it means the random value falls in the current weight range
let offset = randomInteger(totalWeight);
for (let i = 0; i < len; i++) {
  // The random value minus the weight belongs to which weight fragment
  offset -= this.getWeight(pool[i]);
  if (offset < 0) {
    address = pool[i];
    break; }}Copy the code

Least Connections

Least connections algorithm, service nodes with the Least number of connections are preferentially called.

  • The number of connections or requests needs to be counted
  • Consistent Server Performance

Weighted Least Connections

Weighted least Connections algorithm adds weight judgment based on the least connected service nodes.

Nodejs Load Balancing (3) : RPC Implementation

See the NPM package load-balancer-algorithm for more details