Common load balancing algorithms for distributed system design

Posted by Drebin on Sun, 30 Jan 2022 21:48:30 +0100

Common load balancing algorithms for distributed system design

0 what is load balancing?

Load Balance means to balance the load (work task) and allocate it to multiple operation units for operation, so as to complete the work task together.

What are the types of load balancing?

  • Software and hardware load balancing

    • Software load balancing
    • Hardware load balancing
  • Local and global load balancing

    • Local load balancing
    • Global load balancing

The load balancing algorithm in this paper belongs to software level load balancing.

1 polling

As the name suggests, the subtasks are queried one by one in the child nodes.

var list = make([]string, 0)
var servers = make(map[string]string)

func init() {
   servers = map[string]string{
      "stringA": "10.0.0.1",
      "stringB": "10.0.0.2",
      "stringC": "10.0.0.3",
   }
   for s := range servers {
      list = append(list, s)
   }
}

//polling 
var i = 0

func RoundRobin() string {
   if i >= len(list) {
      i = 0
   }
   str := servers[list[i]]
   i += 1
   return str
}

2 random

Requests are made randomly in child nodes.

var list = make([]string, 0)
var servers = make(map[string]string)

func init() {
   servers = map[string]string{
      "stringA": "10.0.0.1",
      "stringB": "10.0.0.2",
      "stringC": "10.0.0.3",
   }
   for s := range servers {
      list = append(list, s)
   }
}

//random
func Random() string {
   i := rand.Intn(len(list))
   return servers[list[i]]
}

3 weighted polling

Different from polling, the weight can be increased, that is, the node with the largest weight will have more requests (proportion).

var list = make([]string, 0)
var servers = make(map[string]string)

func init() {
   servers = map[string]string{
      "stringA": "10.0.0.1",
      "stringB": "10.0.0.2",
      "stringC": "10.0.0.3",
   }
   for s := range servers {
      list = append(list, s)
   }
   //Weighted polling
   var weight_map = map[string]int{
      "stringA": 1,
      "stringB": 2,
      "stringC": 3,
   }
   for s := range weight_map {
      for i := 0; i < weight_map[s]-1; i++ {
         list = append(list, s)
      }
   }
}

//Weighted polling
func WeightRoundRobin() string {
   if i >= len(list) {
      i = 0
   }
   str := servers[list[i]]
   i += 1
   return str
}

4 weighted random

Different from random, it increases the probability that a node is accessed randomly.

var list = make([]string, 0)
var servers = make(map[string]string)

func init() {
   servers = map[string]string{
      "stringA": "10.0.0.1",
      "stringB": "10.0.0.2",
      "stringC": "10.0.0.3",
   }
   for s := range servers {
      list = append(list, s)
   }
   //Weighted polling
   var weight_map = map[string]int{
      "stringA": 1,
      "stringB": 2,
      "stringC": 3,
   }
   for s := range weight_map {
      for i := 0; i < weight_map[s]-1; i++ {
         list = append(list, s)
      }
   }
}

//Weighted random
func WeightRandom() string {
   i := rand.Intn(len(list))
   return servers[list[i]]
}

5 source address hash

The method is to hash the source address of the request, take the remainder of the hash result, match the remainder result with the node, and finally make the request.

//Source Hash
func Hash() string {
   //Hash the client (source) address using the md5 hash algorithm
   has, err := md5.New().Write([]byte("127.0.0.1"))
   if err != nil {
      panic(err)
   }
   i := has % len(list)
   return servers[list[i]]
}

6 minimum number of connections

The minimum number of connections method performs load balancing according to the current connection of the server. When the request arrives, the server with the least number of current connections will be selected to process the request. Therefore, it can also be extended to select the server according to the minimum CPU occupation of the server and the high and low efficiency of processing requests per unit time. The minimum connection number method is only an algorithm for dynamic server allocation. Through the parameter calculation of various dimensions, we can find a more balanced dynamic server allocation scheme suitable for different scenarios.

7 all codes

package main

import (
   "crypto/md5"
   "math/rand"
   "net/http"
)

var list = make([]string, 0)
var servers = make(map[string]string)

func init() {
   servers = map[string]string{
      "stringA": "10.0.0.1",
      "stringB": "10.0.0.2",
      "stringC": "10.0.0.3",
   }
   for s := range servers {
      list = append(list, s)
   }
   //Weighted polling
   var weight_map = map[string]int{
      "stringA": 1,
      "stringB": 2,
      "stringC": 3,
   }
   for s := range weight_map {
      for i := 0; i < weight_map[s]-1; i++ {
         list = append(list, s)
      }
   }
}

//polling 
var i = 0

func RoundRobin() string {
   if i >= len(list) {
      i = 0
   }
   str := servers[list[i]]
   i += 1
   return str
}

//random
func Random() string {
   i := rand.Intn(len(list))
   return servers[list[i]]
}

//Source Hash
func Hash() string {
   //Hash the client (source) address using the md5 hash algorithm
   has, err := md5.New().Write([]byte("127.0.0.1"))
   if err != nil {
      panic(err)
   }
   i := has % len(list)
   return servers[list[i]]
}

//Weighted polling
func WeightRoundRobin() string {
   if i >= len(list) {
      i = 0
   }
   str := servers[list[i]]
   i += 1
   return str
}

//Weighted random
func WeightRandom() string {
   i := rand.Intn(len(list))
   return servers[list[i]]
}

//----------Web test---------------//

func main() {
   //httpServer(WeightRandom)
}

func httpServer(fun func() string) {
   http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
      w.Write([]byte("Request Node is " + fun()))
   })
   http.ListenAndServe(":8888", nil)
}

Reference article:

https://blog.csdn.net/claram/article/details/90290397

Topics: Operation & Maintenance Load Balance Algorithm Distribution