Load balancing on a high-performance architecture, the knowledge that most people do not know

In any case optimized single-server, regardless of how good the hardware, there is always a performance ceiling, when the performance of a single server can not meet the business needs, we need to design a high-performance clusters to improve the processing performance of the entire system.

The nature of high-performance cluster is very simple, to improve the system overall computing power by adding more servers. Due to the presence of a characteristic calculation itself: the same input data and logic, regardless of which server is performed, you should get the same output. Therefore, the complexity of high-performance cluster design is mainly reflected in this part of the task assignment, task assignment to design a reasonable strategy, allocate computing tasks to execute on multiple servers.

Complexity is mainly reflected in the high performance cluster need to add a task allocator, and select an appropriate task allocation algorithm for the task. For the task distributor, is now more popular is the generic name for "load balancer." But the name has some misleading, it will make people subconsciously believe that the purpose of the task assigned to keep the load of each computing unit reaches equilibrium. In fact assignments not just consider the load balancing calculation unit, different objectives and tasks allocation algorithm is not the same, some based on load considerations, and some based on performance (throughput, response time) to consider, and some based on business considerations . Considering the "load balancing" has become the de facto standard terminology, here I also used "load balancing" instead of "tasking", but please keep in mind, not just for load balancing calculation unit load reaches equilibrium.

Load balancing Category

Common load balancing system includes three kinds: DNS load balancing, load balancing hardware and software load balancing.

DNS Load Balancing

DNS is the easiest and most common load balancing methods, generally used to achieve a balanced geographic level. For example, a user's visit to Beijing in the north room, south to Shenzhen user's computer room. The nature of DNS load balancing DNS resolution is the same domain name can return different IP addresses. For example, the same www.baidu.com, address Northern user analyzing data acquired is 61.135.165.224 (This is the IP Beijing room), address South users analyzing data acquired is 14.215.177.38 (This is the IP Shenzhen room).

Here is a simple diagram DNS load balancing:

 

Load balancing on a high-performance architecture, the knowledge that most people do not know

 

 

DNS load balancing is simple, low cost, but there are too coarse, less load balancing algorithm shortcomings. Careful analysis of the advantages and disadvantages, its advantages are:

  • Simple, low cost: DNS server load balancing work to deal with, without having to develop their own load balancing or maintenance of equipment.
  • The nearest access, improve access speed: according to the request source IP, resolved to address the server closest to the user, you can speed up access when DNS resolution, improving performance.

There are disadvantages:

  • Not updating: DNS cache long time, modify the DNS configuration, due to the cache, there are still many users will continue to access the IP before the amendment, such a visit will fail, fail to load balancing purposes, and also influence the normal use of business users.
  • Poor scalability: DNS load balancing control over the domain name business, where, unable to do more customization features and extensions for their characteristics based on business characteristics.
  • Allocation strategy is simple: fewer DNS load balancing algorithm supported; the server can not tell the difference (load can not be judged based on the status of systems and services); status can not perceive the back-end server.

For some disadvantages DNS load balancing, and fault for delay-sensitive traffic, there are some companies own implements HTTP-DNS functions that use the HTTP protocol to implement a private DNS system. Such programs and common DNS advantages and disadvantages of just the opposite.

Hardware Load Balancing

Hardware load balancing load balancing is achieved by separate hardware devices, and equipment such as routers, switches the like, can be understood as a basis for network load balancing device. Typical of the industry there are two hardware load balancing devices: F5 and A10. Such equipment strong, powerful, but the price is not cheap, generally only "Tyrant," the company will consider the use of such equipment. First order of business ordinary company can not afford, the second is not so much the volume of business with these devices is a waste.

Advantage of the hardware load balancing are:

  • Powerful: full support for load balancing all levels, support a comprehensive load balancing algorithms, support for global load balancing.
  • Strong performance: compare, load-balancing software to support 100,000 concurrent been very powerful, hardware load balancing can support more than one million concurrent.
  • High stability: commodity hardware load balancing, after a good rigorous testing, through the use of large-scale, high stability.
  • Support Safety: In addition to having the hardware equalizer device load balancing function, but also have firewall, anti-DDoS attacks and other security features.

Shortcoming hardware load balancing are:

  • Expensive: the most common one F5 is a "horse 6", is a little better "Q7" the.
  • Extended poor: a hardware device that can be configured according to the business, but can not be extended and customized.

Load balancing software

Software load balancing is achieved through load balancing software load balancing, often with Nginx and LVS, which Nginx is a software layer 7 load balancing, LVS is a 4-layer load balancing Linux kernel. 4 and the difference between layer 7 and layer protocols is that flexibility, Nginx support HTTP, E-mail protocol; LVS is independent of the load balancing layer 4, and protocols for almost all applications can be done, for example, chat database.

The main difference is that software and hardware performance, load balancing hardware performance is much higher than software load balancing performance. Performance Ngxin is ten thousand, the average Linux server would be able to upload a Nginx 50,000 / sec; performance LVS is one hundred thousand, is said to be 800,000 / sec; and F5 performance is one million, from 2 million / sec to 8 million / sec are (source data network, for reference only, please For the use of performance testing based on real business scenarios). Of course, the biggest advantage of load balancing software is cheaper, a normal Linux server wholesale price is probably around $ 10,000, compared to the price of F5, that is the difference between a bicycle and a BMW.

In addition to using open-source systems for load balancing, if the business is rather special, it may be customized (for example, Nginx plugin) based on open source systems, and even self-study.

The following is a load balancing architecture diagram of Nginx:

 

Load balancing on a high-performance architecture, the knowledge that most people do not know

 

 

Advantages load balancing software:

  • Simple: Whether deployed or maintenance are relatively simple.
  • Cheap: Just buy a Linux server, you can install the software.
  • Flexible: 4 layer and a layer 7 load balancing can be selected according to the service; can also be compared to facilitate the expansion according to the service, for example, may be implemented by a service customized functions Nginx plug.

In fact, the following disadvantages are compared and hardware load balancing, load balancing does not mean that the software can not use.

  • General performance: a Nginx can support approximately 50,000 concurrent.
  • Hardware load balancing function is not so strong.
  • Generally do not have a firewall and anti-DDoS attacks and other security features.

A typical load balancing architecture

Earlier we introduced the three common load balancing mechanisms: DNS load balancing, load balancing hardware, software load balancing, each method has some advantages and disadvantages, but that does not mean only based on their strengths and weaknesses in the practical application either-or choice, but is used in combination based on their advantages and disadvantages. In particular, the combination of the basic principles are:

  • DNS load balancing to achieve the level of geographic load balancing;
  • Hardware used to implement load balancing cluster level load balancing;
  • Load balancing software used to implement machine-level load balancing.

以一个假想的实例来说明一下这种组合方式,如下图所示。

 

Load balancing on a high-performance architecture, the knowledge that most people do not know

 

 

整个系统的负载均衡分为三层。

  • 地理级别负载均衡:www.xxx.com部署在北京、广州、上海三个机房,当用户访问时,DNS会根据用户的地理位置来决定返回哪个机房的IP,图中返回了广州机房的IP地址,这样用户就访问到广州机房了。
  • 集群级别负载均衡:广州机房的负载均衡用的是F5设备,F5收到用户请求后,进行集群级别的负载均衡,将用户请求发给3个本地集群中的一个,我们假设F5将用户请求发给了“广州集群2”。
  • 机器级别的负载均衡:广州集群2的负载均衡用的是Nginx,Nginx收到用户请求后,将用户请求发送给集群里面的某台服务器,服务器处理用户的业务请求并返回业务响应。

需要注意的是,上图只是一个示例,一般在大型业务场景下才会这样用,如果业务量没这么大,则没有必要严格照搬这套架构。例如,一个大学的论坛,完全可以不需要DNS负载均衡,也不需要F5设备,只需要用Nginx作为一个简单的负载均衡就足够了。

负载均衡算法

负载均衡算法数量较多,而且可以根据一些业务特性进行定制开发,抛开细节上的差异,根据算法期望达到的目的,大体上可以分为下面几类。

  • 任务平分类:负载均衡系统将收到的任务平均分配给服务器进行处理,这里的“平均”可以是绝对数量的平均,也可以是比例或者权重上的平均。
  • 负载均衡类:负载均衡系统根据服务器的负载来进行分配,这里的负载并不一定是通常意义上我们说的“CPU负载”,而是系统当前的压力,可以用CPU负载来衡量,也可以用连接数、I/O使用率、网卡吞吐量等来衡量系统的压力。
  • 性能最优类:负载均衡系统根据服务器的响应时间来进行任务分配,优先将新任务分配给响应最快的服务器。
  • Hash类:负载均衡系统根据任务中的某些关键信息进行Hash运算,将相同Hash值的请求分配到同一台服务器上。常见的有源地址Hash、目标地址Hash、session id hash、用户ID Hash等。

接下来介绍一下负载均衡算法以及它们的优缺点。

轮询

负载均衡系统收到请求后,按照顺序轮流分配到服务器上。

轮询是最简单的一个策略,无须关注服务器本身的状态,例如:

  • 某个服务器当前因为触发了程序bug进入了死循环导致CPU负载很高,负载均衡系统是不感知的,还是会继续将请求源源不断地发送给它。
  • 集群中有新的机器是32核的,老的机器是16核的,负载均衡系统也是不关注的,新老机器分配的任务数是一样的。

需要注意的是负载均衡系统无须关注“服务器本身状态”,这里的关键词是“本身”。也就是说,只要服务器在运行,运行状态是不关注的。但如果服务器直接宕机了,或者服务器和负载均衡系统断连了,这时负载均衡系统是能够感知的,也需要做出相应的处理。例如,将服务器从可分配服务器列表中删除,否则就会出现服务器已经宕机了,任务还不断地分配给它,这明显是不合理的。

总而言之,“简单”是轮询算法的优点,也是它的缺点。

加权轮询

负载均衡系统根据服务器权重进行任务分配,这里的权重一般是根据硬件配置进行静态配置的,采用动态的方式计算会更加契合业务,但复杂度也会更高。

加权轮询是轮询的一种特殊形式,其主要目的就是为了解决不同服务器处理能力有差异的问题。例如,集群中有新的机器是32核的,老的机器是16核的,那么理论上我们可以假设新机器的处理能力是老机器的2倍,负载均衡系统就可以按照2:1的比例分配更多的任务给新机器,从而充分利用新机器的性能。

加权轮询解决了轮询算法中无法根据服务器的配置差异进行任务分配的问题,但同样存在无法根据服务器的状态差异进行任务分配的问题。

负载最低优先

负载均衡系统将任务分配给当前负载最低的服务器,这里的负载根据不同的任务类型和业务场景,可以用不同的指标来衡量。例如:

  • LVS这种4层网络负载均衡设备,可以以“连接数”来判断服务器的状态,服务器连接数越大,表明服务器压力越大。
  • Nginx这种7层网络负载系统,可以以“HTTP请求数”来判断服务器状态(Nginx内置的负载均衡算法不支持这种方式,需要进行扩展)。
  • 如果我们自己开发负载均衡系统,可以根据业务特点来选择指标衡量系统压力。如果是CPU密集型,可以以“CPU负载”来衡量系统压力;如果是I/O密集型,可以以“I/O负载”来衡量系统压力。

负载最低优先的算法解决了轮询算法中无法感知服务器状态的问题,由此带来的代价是复杂度要增加很多。例如:

  • 最少连接数优先的算法要求负载均衡系统统计每个服务器当前建立的连接,其应用场景仅限于负载均衡接收的任何连接请求都会转发给服务器进行处理,否则如果负载均衡系统和服务器之间是固定的连接池方式,就不适合采取这种算法。例如,LVS可以采取这种算法进行负载均衡,而一个通过连接池的方式连接MySQL集群的负载均衡系统就不适合采取这种算法进行负载均衡。
  • CPU负载最低优先的算法要求负载均衡系统以某种方式收集每个服务器的CPU负载,而且要确定是以1分钟的负载为标准,还是以15分钟的负载为标准,不存在1分钟肯定比15分钟要好或者差。不同业务最优的时间间隔是不一样的,时间间隔太短容易造成频繁波动,时间间隔太长又可能造成峰值来临时响应缓慢。

负载最低优先算法基本上能够比较完美地解决轮询算法的缺点,因为采用这种算法后,负载均衡系统需要感知服务器当前的运行状态。当然,其代价是复杂度大幅上升。通俗来讲,轮询可能是5行代码就能实现的算法,而负载最低优先算法可能要1000行才能实现,甚至需要负载均衡系统和服务器都要开发代码。负载最低优先算法如果本身没有设计好,或者不适合业务的运行特点,算法本身就可能成为性能的瓶颈,或者引发很多莫名其妙的问题。所以负载最低优先算法虽然效果看起来很美好,但实际上真正应用的场景反而没有轮询(包括加权轮询)那么多。

性能最优类

负载最低优先类算法是站在服务器的角度来进行分配的,而性能最优优先类算法则是站在客户端的角度来进行分配的,优先将任务分配给处理速度最快的服务器,通过这种方式达到最快响应客户端的目的。

和负载最低优先类算法类似,性能最优优先类算法本质上也是感知了服务器的状态,只是通过响应时间这个外部标准来衡量服务器状态而已。因此性能最优优先类算法存在的问题和负载最低优先类算法类似,复杂度都很高,主要体现在:

  • 负载均衡系统需要收集和分析每个服务器每个任务的响应时间,在大量任务处理的场景下,这种收集和统计本身也会消耗较多的性能。
  • 为了减少这种统计上的消耗,可以采取采样的方式来统计,即不统计所有任务的响应时间,而是抽样统计部分任务的响应时间来估算整体任务的响应时间。采样统计虽然能够减少性能消耗,但使得复杂度进一步上升,因为要确定合适的采样率,采样率太低会导致结果不准确,采样率太高会导致性能消耗较大,找到合适的采样率也是一件复杂的事情。
  • 无论是全部统计还是采样统计,都需要选择合适的周期:是10秒内性能最优,还是1分钟内性能最优,还是5分钟内性能最优……没有放之四海而皆准的周期,需要根据实际业务进行判断和选择,这也是一件比较复杂的事情,甚至出现系统上线后需要不断地调优才能达到最优设计。

Hash类

负载均衡系统根据任务中的某些关键信息进行Hash运算,将相同Hash值的请求分配到同一台服务器上,这样做的目的主要是为了满足特定的业务需求。例如:

  • 源地址Hash

将来源于同一个源IP地址的任务分配给同一个服务器进行处理,适合于存在事务、会话的业务。例如,当我们通过浏览器登录网上银行时,会生成一个会话信息,这个会话是临时的,关闭浏览器后就失效。网上银行后台无须持久化会话信息,只需要在某台服务器上临时保存这个会话就可以了,但需要保证用户在会话存在期间,每次都能访问到同一个服务器,这种业务场景就可以用源地址Hash来实现。

  • ID Hash

将某个ID标识的业务分配到同一个服务器中进行处理,这里的ID一般是临时性数据的ID(如session id)。例如,上述的网上银行登录的例子,用session id hash同样可以实现同一个会话期间,用户每次都是访问到同一台服务器的目的。

Transfer from: http: //developer.51cto.com/art/201910/603927.htm

Guess you like

Origin www.cnblogs.com/WIU1905/p/11802870.html