Load balancing details - implement algorithms and forwarding

What is load balancing?

When an application server performance to its limits, we can use this clustering techniques to improve the overall performance of the site. Then, in the server cluster, you need to have a server act as a scheduler, and all requests are first received by the users of it, according to the load dispatcher then each server will request assigned to a single application server to handle.

The scheduler is load balancing server.

Server load balancing can be used to implement the architecture of the site high availability and scalability :

  • Availability : When a node fails, the load balancer will forward the request to another user node, so as to ensure continuous availability of all services;
  • Scalability : The overall system load conditions, can easily be added or removed nodes.

Server load balancing can be divided into two parts:

  • According to the load balancing algorithm calculated the cluster address of a Web server and Web server list
  • The data request is sent to the Web server corresponding to the address on

The following describes the load balancing algorithm and forwarding achieve this in two parts.

First, the load balancing algorithm

Polling (Round Robin)

The polling algorithm each request sent in turn to each server.

Suitable for all server hardware is the same scene.

As shown below, a total of four requests (1,2,3,4) to the load balancing scheduler sequentially, they will in turn be sent to the server A and server B.

Here Insert Picture Description
However, if the server performance difference is too large, then the poor performance of the server may be unable to bear the excessive load , as shown in the Server B, it can only afford a request.

Here Insert Picture Description
2. WRR (Weighted Round Robbin)

WRR is based on polling, according to the difference in performance of the server, the server gives a certain weight, high performance server is assigned a higher weight.

如下图所示,服务器 A 被赋予的权值为 3,服务器 B 被赋予的权值为 1,那么 (1, 2, 3) 请求会被发送到服务器 A,(4) 请求会被发送到服务器 B。

Here Insert Picture Description

3. 随机(Random)

把请求随机发送到服务器上。和轮询算法类似,该算法比较适合服务器性能差不多的场景。同样,对于服务器性能有差异的场景,也可以使用加权随机算法。

Here Insert Picture Description

4. 最少连接(least Connections)

记录每个应用服务器正在处理的连接数(请求数),将新到的请求分发到最少连接的服务器上,应该说,这是最符合负载均衡定义的算法。同样,最少连接算法也可以实现加权最少连接。

如下所示,此时服务器 B 的连接数较少,那么新到达的请求,将会被转发到服务器 B上。
Here Insert Picture Description

5. 源地址散列(Source Hashing)

根据请求来源的IP地址进行Hash计算,再对应用服务器数量进行取模,得到应用服务器,这样来自同一个IP地址的请求总在同一个服务器上处理,该请求的上下文信息可以存储在这台服务器上,在一个会话周期内重复使用,从而实现会话粘滞(Session 复制)

Here Insert Picture Description

二、转发实现

1. HTTP 重定向负载均衡

HTTP 重定向负载均衡服务器使用某种负载均衡算法计算得到服务器的 IP 地址之后,将该地址写入 HTTP 重定向报文中,状态码为 302。客户端收到重定向报文之后,需要重新向服务器发起请求。

Here Insert Picture Description
优点:实现简单。

缺点:

  • 两次请求,访问延迟较高
  • HTTP 负载均衡器处理能力有限,会限制集群的规模。

实践中使用这种方案进行负载均衡的案例并不多见。

2. DNS 域名解析负载均衡

在 DNS 服务器中配置多个 A 记录,如 : www.mysite.com IN A 114.100.80.1、www.mysite.com IN A 114.100.80.2、www.mysite.com IN A 114.100.80.3。

每次域名解析请求都会根据负载均衡算法计算一个不同的IP地址返回,这样 A 记录中配置的多个服务器就构成一个集群,并可以实现负载均衡。

Here Insert Picture Description
优点:DNS 能够根据地理位置进行域名解析,返回离用户最近的服务器 IP 地址。

缺点:由于 DNS 具有多级结构,每一级的域名记录都可能被缓存,当下线一台服务器需要修改 DNS 记录时,需要过很长一段时间才能生效。

大型网站总是部分使用 DNS 域名解析, 利用 DNS 做为第一级负载均衡手段,然后在内部使用其它方式做第二级负载均衡。也就是说,域名解析的结果为内部的负载均衡服务器 IP 地址,而不是实际提供Web服务的物理服务器。之后再有这些内部的负载均衡服务器进行负载均衡,将请求发到真实的Web服务器上。

3. 反向代理负载均衡

利用反向代理可以缓存资源,以改善网站性能。

在部署位置上,反向代理服务器处于Web服务器前面(所以可以缓存Web响应,加速访问),这个位置正好也是负载均衡服务器的位置,所以大多数反向代理服务器同时提供负载均衡的功能。

在这种负载均衡转发方式下,客户端不直接请求源服务器,因此源服务器不需要外部 IP 地址,而反向代理需要配置内部和外部两套 IP 地址。

Here Insert Picture Description
由于反向代理服务器转发请求在HTTP协议层面,因此也叫应用层负载均衡。

优点:与其它功能集成在一起,部署简单。

缺点:所有请求和响应都需要经过反向代理服务器,它可能会成为性能瓶颈。

4. IP负载均衡

在网络层通过修改请求目标地址进行负载均衡。

After the user request packet to the load balancing servers, load balancing server acquires network packets in the operating system kernel process, according to the algorithm of the source IP address of the server load balancing, and modify the request destination IP address of the packet, and finally forwarded.

Source response returned by the server also need to go through the load balancing server, typically by getting them to achieve load balancing server acts as the gateway server cluster.

Here Insert Picture Description
Advantages: processed in the kernel process, the performance is relatively high.

Cons: and reverse proxy, as all requests and responses have been load balancing server, will become a performance bottleneck.

The data link layer load balancing

The link layer according to the load balancing algorithm source server MAC address, and modifies the destination MAC address of the request packet, and forwarding.

By the same configuration of the source server and load balancing virtual IP address of the server's IP address, eliminating the need to change the IP address can be forwarded . It is because of the same IP address, so the response back to the source server does not need to forward the server load balancing can be forwarded directly to the client, server load balancing to avoid a bottleneck.

This is a triangle mode is called a direct route. For download and video service Web site, the direct route to avoid a lot of network traffic through load balancing server.

Here Insert Picture Description

Reference: "Large Web Site Technology Framework" - Li Zhihui the
second part, to achieve load balancing forwarding section illustrations are from the book.

Guess you like

Origin blog.csdn.net/u013568373/article/details/91403446