Home php教程 php手册 PHP超级负载均衡

PHP超级负载均衡

Jun 21, 2016 am 08:54 AM
count nbsp server

摘要

 

超级负载均衡旨在为解决服务不断扩展、机器不断增多、机器性能差异等问题,以增强系统的稳定性,自动分配请求压力。算法实现了多个模型和均衡策略,能通过配置实现随机、轮询、一致hash等。同时也能实现跨机房的相关分配。现已经在多个系统中使用。

TAG

负载均衡

内容

现有系统中存在的问题:

 

1. 慢连接、瞬时访问慢。

场景一:

如果后端新增加机器,cache命中率低,因此响应速度慢,但是能连接上且不超时。如果ui持续访问就会把ui夯住。

场景二:

如果后端模块某一台机器响应较慢。如果前端持续访问就会被夯住。

2. 死机。

场景一:

能断断续续响应请求,不过速度很慢。造成ui夯住。

3. 混合部署。

场景一:

多个模块在同一机器上,项目影响。

4. 机器权重。

场景一:

老机器,性能差;新机器,性能彪悍。因此他们应该承载不同的压力。

5. 跨机房冗余。

场景一:

后端对cache依赖很高的模块,因为采用的是一致hash算法,如果挂掉一台机器,对另外的机器cache命中率冲击很大。因此希望将对这个机器的请求均衡到另外一个机房。

6. php和c使用同样的策略。

现在php和c希望能使用的策略实际上是有很大的一致。为了避免重复开发,php和c希望采用同样的负载均衡库。

要解决的问题:

设计思路:

1. 根据均衡策略计算出的均衡值对Server进行逆序排序。

2. 负载选择。对步骤1排序后的Server按以下顺序进行选择:

a、按连接失败概率进行选择。

注:横轴代表失败次数,纵轴代表选择的概率。

Cconn:一段区间内失败次数

f(Cconn):连接概率,取值范围在(0,100]

b、按健康状态选择。

整个模型基于服务处理时间的收敛性。

分析:

1) 如果机器状态良好,则平均处理时间会保持在一个稳定水平;即使是小波动,也会较快平稳在一个状态。

2) 如果机器开始出现问题,处理时间会开始增长。如果增长持续超过一段时间,则说明有可能会影响服务;如果一段时间后稳定了,说明对请求没有太多影响。

f(healthy):机器健康状态,取值范围[0,1]

select(healthy):机器选择概率,取值范围[R,1]

c、如果所有机器都没选中,则随机选择一台机器进行服务。

3. 机器流量均分。

不同的机器处理能力是不一样的。当按照步骤2选择了某台机器,需要将其他处理时间为他的1/T(T>=2)的机器也选取出来,将部分压力分给对应的机器。

设k台机器的处理时间分别是t1, t2,…,tk, 选中的机器id=i,比该机器处理能力高的机器时间分别为p1,p2,..,pr, (其中pj × T

算法设计:

A、均衡算法

1. 一致hash算法。

将每个server的ip和port加上balance_key三者做字符串拼接后,做md5签名。

value(server) = md5(server_ip + server_port + balance_key)

2. 随机算法。

value(server) = random();

3. 轮询算法。

value(server) =((server.id – (rounds % server_count)) + server_count) % server_count

4. 多个选一算法。

rank初始化为1, 如果默认的server失败,则rank+1

value(server) =((server.id – (rank % server_count)) + server_count) % server_count

B、负载算法

1. 连接状态算法。

a、对每一个server开辟一个状态队列。bool queue[K] 用来统计失败次数。每次有坏状态进队,计数加一。如果有坏状态出队,则计数减一。

b、按照f(Cconn)公式计算出选择概率。

c、利用rand()%100是否在[0,f(Cconn)]来决定是否选择该机器。

2. 健康状态算法。

a、每台机器维持一个一秒钟内的处理时间T和次数C。

b、当一秒过去以后,将T、C计算为平均处理时间R。

c、每M秒,统计每台机器最近一段时间的平均处理时间, 按照公式select(healthy)算出选择概率。

d、利用rand()%100是否在[0, select(healthy)*100]来决定是否选择该机器。

C、流量均分

按照策略选出满足要求的机器,按照流量均分公式进行流量分配。

分配时按照balance_key+server方式和random()来分配机器, 尽量保证请求落在同一台机器。




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles