PHP Super Load Balancing

Source: Internet
Author: User
Tags count hash md5 php and rand range access

Summary

Super Load balancing is designed to improve the stability of the system and distribute the request pressure automatically to solve the problems such as the continuous expansion of service, the increasing of machines and the difference of machine performance. The algorithm realizes a number of models and equalization strategies, which can be configured to achieve random, polling, consistent hash and so on. At the same time can also achieve the relevant distribution across the machine room. is now in use in multiple systems.

TAG

Load Balancing

Content

Problems with existing systems:

1. Slow connection, instantaneous access slow.

Scene One:

If a new machine is added to the back end, the cache hit rate is low, so the response is slow but can be connected and not timed out. If the UI continues to be accessed, the UI will be rammed.

Scene Two:

If the back-end module has a slow response from one machine. If the front end is continuously accessed, it will be rammed.

2. Crash.

Scene One:

Can respond to requests intermittently, but slowly. Causing the UI to ram.

3. Mixed deployment.

Scene One:

Multiple modules on the same machine, project impact.

4. Machine weight.

Scene One:

Old machine, poor performance, new machine, performance. So they should be carrying different pressures.

5. Cross Room redundancy.

Scene One:

The back-end cache relies on a very high module, because the use of a consistent hash algorithm, if hung off a machine, on the other machine cache hit rate hit a lot. Therefore, I hope to balance the request of this machine to another room.

6. PHP and C use the same strategy.

Now PHP and C want to be able to use the strategy is actually a lot of agreement. To avoid duplication of development, PHP and C want to use the same load-balancing library.

Issues to resolve:

Design ideas:

1. The server is sorted in reverse order according to the equilibrium value calculated by the equilibrium strategy.

2. Load Selection. The server sorted by step 1 is selected in the following order:

A, according to the connection failure probability to choose.

Note: The axis represents the number of failures, and the longitudinal axis represents the probability of selection.

Cconn: Number of failures in an interval

F (cconn): Connection probability, Value range in (0,100)

b, according to the health status of choice.

The whole model is based on the convergence of service processing time.

Analysis:

1 if the machine is in good condition, the average processing time will be maintained at a stable level, even small fluctuations will be more stable in a state.

2 If the machine starts to have problems, the processing time will begin to grow. If the growth lasts longer than a period of time, it may affect the service, and if stabilized after a period of time, it does not have much impact on the request.

F (Healthy): Machine health status, value range [0,1]

Select (healthy): Machine selection probability, value range [r,1]

C, if all machines are not selected, then randomly select a machine for service.

3. Machine flow sharing.

Different machine processing ability is not the same. When a machine is selected according to step 2, it is necessary to select the other processing time for his 1/t (t>=2) machine and to divide some of the pressure into the corresponding machine.

Set K machine processing time is T1, T2,..., tk, selected machine id=i, compared to the machine processing capacity of the machine time is P1,P2,.., PR, (pjxt <= ti). For a period of time the total number of visits to Y, each machine should be a theoretical access to vg=y/k. and the actual vr=y/(TI * (1/T1+1/T2+...+1/TK)). You should give PJ the flow of VG-VR. PJ's flow rate is 1/P1:1/P2:...:1/PR.

Algorithm design:

A, Equalization algorithm

1. Consistent hash algorithm.

Each server's IP and port plus balance_key Three do string concatenation, do MD5 signature.

Value (server) = MD5 (server_ip + server_port + balance_key)

2. Random algorithm.

Value (server) = random ();

3. Polling algorithm.

Value (server) = ((server.id– (rounds% Server_count)) + server_count)% Server_count

4. Multiple selection algorithm.

Rank is initialized to 1, if the default server fails, rank+1

Value (server) = ((server.id– (rank% server_count)) + server_count)% Server_count

B, Load algorithm

1. Connection state algorithm.

A, open a status queue for each server. BOOL Queue[k] is used to count the number of failures. Every time there is a bad state into the team, Count plus one. If there is a bad state out of the team, then count minus one.

b, according to the F (cconn) formula to calculate the selection probability.

C, use rand ()%100 to decide whether to select the machine in [0,f (Cconn)].

2. Health status algorithm.

A, each machine to maintain a one-second processing time t and the number of C.

B, when a second past, the T, c calculation for the average processing time R.

C, every m seconds, the average processing time of each machine in the last period of time, according to the formula Select (healthy) to calculate the selection probability.

D, use rand ()%100 to decide whether to select the machine in [0, select (healthy) *100].

C, traffic sharing

According to the strategy to select the machine to meet the requirements, according to the flow sharing formula for flow distribution.

Assign the machine according to the Balance_key+server method and random () to ensure that the request falls on the same machine as possible.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.