Micro Services - How to do load balancing cluster server

Those load balancing face questions

Briefly about what is load balancing? Many people fear this conceptual problem

Load balancing with your company what?

Why is this?

Its advantages and disadvantages

There is a better choice?

You say this 5-linked ask, who can bear ah, Cong shallow to deep, inter-related, just do not, do not be afraid, read this article, these problems will be solved.

What is load balancing?

Explain load balancing saying : You have to select a restaurant to eat lunch at 10, then you choose this process is the process of balancing the load, (interview also may say so).
Formal jargon : it refers to a load balancing in a cluster to selected cluster machine processing the current request through some hardware or software algorithms, to achieve a dispersion machine to handle a large number of different rear cluster requests, so as to enhance high concurrent capacity and disaster recovery capabilities.
Baidu Encyclopedia : load balancing built on top of existing network infrastructure, which provides a cheap and effective and transparent method to expand the bandwidth of network devices and servers to increase throughput, enhance network data capacity, increase network flexibility and availability of

Detailed hardware and software load balancing

Load balancing is currently divided into three general categories: 1 hardware load balancing, load balancing software algorithms 2, 3 DNS based load balancing tell us about these three categories are different advantages and disadvantages.

Hardware load balancing solution are mounted directly between the server and the external network load balancing device, such devices commonly referred to as a load balancer, since the specialized equipment perform a dedicated task, independent of the operating system, substantial improvements in overall performance, plus on a variety of load balancing strategy, intelligent traffic management, can achieve the best load balancing requirements, which are used in large-scale server clusters, such as F5 load balancer.

Software load balancing refers to the load balancing software installed on the operating system of the server, the request issued from the server through the software load balancing routing algorithm to the rear end of one of the machines in the cluster.

DNS load balancing is generally used for load balancing on location, such as your website in the country has a huge number of users, the judge returned to the user through the DNS geographically different IP when different users visit the Web site domain name, so as to achieve the nearest access, traffic sharing, enhance the user experience.

What are their strengths and weaknesses is it?

Hardware load balancers usually only concerned about the load of network traffic, as the state and other back-end server, he does not worry about, and the cost of expensive, and often a single point, but it also has advantages, is a good performance, processing capability, and operating system independence.

Load balancing software more flexible, adjustable large, and software algorithms to achieve a relationship, to focus on the status of the application server to do another test summary statistics capacity, high cost, but the impact server performance by installing the software, not the hardware but also good performance, DNS load balancing also belongs to a software load balancing.

The main analysis in this paper is the software load balancing.

Common load balancing algorithms and implementation principle

Now a lot of load balancing middleware, we are most familiar, the most famous of the case of Nginx, and secondly there are many, such as Baidu earlier open source bfe (Baidu unified front-end), is Baidu layer 7 traffic forwarding platform, as well as apache, a variety of load balancing algorithms and other micro-services middleware

We analyze these major middleware load balancing strategy is how to achieve? What algorithm is used, the focus here

  1. Random Random
  2. Round Robin polling
  3. Weighted Round Robin WRR
  4. Least Connections least connections
  5. Latency-Aware perceived delay (minimum delay, that is to say that the best performance of the machine, to use that table)
  6. Source Hashing source address hashing
  7. Consistency Consistency hash hash (generally more common in distributed cache)

Random strategy refers to the back-end cluster IP list of the machine's random number selected in accordance with a IP as the respondent's request, when the random algorithm is good enough, fair enough when, under massive request, and ultimately individual machines carrying the back-end cluster the flow is balanced, randomized strategy will result in lower configure the machine Down machine, which may cause an avalanche, recommend the best back-end cluster machine configuration generally the same when using a random algorithm, performance random strategy depends on the performance of randomized algorithms.

Polling Strategy refers to the number of all machines in the cluster, assuming the machine 10, comes from the machine starts from 0 0-9, a request to a request for each subsequent number is incremented by 1, an endless loop, the above random in fact, the policy becomes final poll, and these two strategies do not care about the load and operation of the machine, but also to introduce variable operation lock operation, performance will drop down.

Weighted round-robin strategy refers back to the back-end cluster each machine is assigned a weight, the weight will take more high flow, low flow opposite the weight distribution will be less, this strategy allows the backend cluster machine configuration the difference, if there are three machines (a, b, C), their weights are (7,2,1), then the machine 10 requests a bear 7, b 2 bear the machine, the machine bear C 1 but in the end how does this bear allocation method? there are two cases as follows, we can see that the first time at the request of a, bc completely idle, while the second case is relatively uniform number, Nginx weighted polling policies adopted It is the second case

  1. (aaaaaaa,bb,c)
  2. (Aabaabaaca)

Minimum connection strategy will focus on the current number of connections to back-end cluster each server, connect the machine to select a minimum number of responses the current request, this strategy is actually concerned about the load of each server, select the least loaded machine to process the request, maximize the each machine utilization, relatively flexible and intelligent, the complex will also implement some.

Delayed perception strategies and least connections is the same idea, the delay perceived pursuit of extreme performance or user experience, can always pick the fastest return of the machine to access the results of the implementation, but the downside is that when all clients are considered a server The fastest time, all requests are sent this service but the service may cause excessive pressure drops in performance.

Source address hashing strategy can make the same request of the client or user's request is always the same request in the same machine on the back end, this algorithm Hash value calculated based on the client IP and then seek to end more than the total number of clusters is to get the value of a collection of servers subscripts, that is generally an algorithm for cache hit, the same session request or the like, but this method also has some disadvantages, a user views (black production) may cause the server when excessive pressure is very high or the rear end Down off the service, the client will be unable to access, so it requires a certain demotion strategy.

Consistency hash is developed on the basis of the source address to the hash, what does that mean? There is a back-end cluster 3 machines (a, b, c), the total number of client server hashed take over after total is a request to a machine, then when a new machine or reduce back-end cluster client after hashing after taking more than the total number of servers that no longer is the original machine, so original after all requests hash corresponding backend machines have changed, the consistency of the hash is to solve this problem.

Implement a load balancing algorithm

We picked a strategy with the above code to implement it, in order to make everyone a better understanding, a selection of frequently asked interview strategies, 1, weighted round-robin algorithm, this will be more, Nginx in default algorithm

Weighted Round Robin Each server has three weight: weight initial configured weight, current weight, effective weight, wherein the initial configuration of the weights and the effective weight is constant, by default the effective weight is equal to the initial configuration of the weights, when the initial configuration of the right profile weight when the change will trigger effective weight change, only the current weight is dynamic.

When a request comes in every select a current highest weight from the server list, after the selected servers current weight minus all servers weights and re-assigned to the server current weight, which total algorithm diminishing current weight so that all servers have the opportunity to service requests, relatively smooth, the code to achieve the following

First definition of a structure, the core element must be weighted round-robin algorithm weights the initial configuration of the server, current weights (weights are likely to change during actual operation)

type SeverWeight struct {
   //配置的权重
   ConfigWeight int
   //当前权重
   CurrentWeight int
   //有效权重(值等于ConfigWeight,不过该字段是用一个配置属性,供前端修改使用)
   EffectiveWeight int
   //服务器ip
   Ip string
}
//加权轮询算法
type WeightedRoundRobin struct {
   //机器ip和对应的权重
   IpAndWeightedConfig map[string]int
   //服务器和权重信息
   SwSlice []*SeverWeight
}

Creating responsible for balancing objects based on configuration information to initialize the value of each field

//初始化加权轮询对象
func NewWeightedRoundRobin(iwc map[string]int) *WeightedRoundRobin {
   if iwc == nil {
      return nil
   }
   SwSlice := make([]*SeverWeight, 0)
   for k, v := range iwc {
      sw := &SeverWeight{ConfigWeight: v, CurrentWeight: 0,
                                 EffectiveWeight: v, Ip: k}
      SwSlice = append(SwSlice, sw)
   }
   return &WeightedRoundRobin{IpAndWeightedConfig: iwc, SwSlice: SwSlice}
}

This method is the core, call this method to decide which server to provide services, core logic method is to select the current weight of the largest server to provide services, current weight is constantly changing, every heavy current weight is equal to the current value plus the effective value minus the effective rights of all servers and weight (this algorithm is diminishing the current value of the current weight to the server, so that changes in accordance with uniform so that all servers can provide services)

func (wrr *WeightedRoundRobin) Select() (sw *SeverWeight) {
   total := 0 //统计所有服务器权重和
   for _, v := range wrr.SwSlice { //遍历服务器
      //当前权重加上有效权重
      v.CurrentWeight += v.EffectiveWeight
      total += v.EffectiveWeight
      //当配置值修改的时候的,有效权重循序渐进的增加
      if v.EffectiveWeight < v.ConfigWeight {
         v.EffectiveWeight++
      }
      //把权重最大的赋值给sw(sw是需要返回的对象)
      if sw == nil || v.CurrentWeight > sw.CurrentWeight {
         sw = v
      }
   }
   //当前返回对象的权重-所有服务器权重和
   sw.CurrentWeight = sw.CurrentWeight - total
   return sw
}

Let us look at the results of the test performed, according to test results, we believe we will be able to understand, according to the following result we can really see the return of server IP is uniform, relatively smooth, low weight will not let the server waits.

func TestNewWeightedRoundRobin(t *testing.T) {
   //服务器ip和权重配置 
   config :=map[string]int{"10.1": 7, "10.2": 2, "10.3": 1}
   wrr := NewWeightedRoundRobin(config)
   //发送10次请求
   for i := 0; i < 10; i++ {
      sw := wrr.Select()
      t.Log(sw.Ip)//打印每次请求IP
   }
}
//结果:[10.1,10.1,10.2,10.1,10.1,10.3,10.1,10.1,10.2,10.1]

I have submitted to the whole code on github, you can download on github actually run down a bit, deepen understanding, I have to github at the following address:

https://github.com/sunpengwei1992/go_common/blob/master/algorithm/load_balance.go

Any of which could lead to a pile-depth research questions, you can write a separate article out herein, this focus is to let everyone know that these algorithms that after seeing no strangers, we need to continue to work in explore and upgrade their knowledge, improve thinking ability.

Guess you like

Origin www.cnblogs.com/sy270321/p/12581011.html
Recommended