A common load balancing algorithm and its code implementation __ algorithm

Source: Internet
Author: User
Tags hash server array
What is load balancing

Load balancing, the English name is load Balance, refers to the multiple servers in a symmetric way to form a collection of servers, each server has an equivalent status, can be provided separately from external services without the assistance of other servers. With a load-sharing technique, requests sent externally are evenly distributed to one server in the symmetric structure, while the server receiving the request responds independently to the client's request. Load Balancing distributes customer requests to the server array evenly, providing fast access to important data to address a large number of concurrent access service problems, which can be achieved with minimal investment in the performance of a large host.

Load balancing is divided into software load balancing and hardware load balancing, the former Representative is the LVS developed by Dr. Ali Zhangwensong, the latter is a balanced server such as F5, of course, this is just mention, not the focus.

This article describes the various algorithms for evenly distributing requests sent externally to one server in a symmetric structure, and demonstrates the specific implementation of each algorithm in Java code. stochastic (random)

Select a server from the random number generation algorithm and send the connection to it. Polling (Round Robin) the principle of polling scheduling algorithm:

Each time a request from the user is assigned to an internal server, starting at 1, until N (the number of internal servers), and then restarting the loop. The advantage of the algorithm is its simplicity, which does not need to record the state of all current connections, so it is a stateless dispatch. Disadvantages:

does not consider the processing capacity of each server weighted polling (Weight Round Robin)

Because of the configuration of each server, the business application installed, and so on, its processing power will be different. Therefore, we assign different weights to each server according to the different processing power of the server, so that it can accept the service request of the corresponding weight value. source Hashing (address hash)

The source address hash scheduling algorithm is a static mapping algorithm that maps a source IP address to a server through a hash function, and if the server is available and not overloaded, sends the request to the server, otherwise it returns NULL. It uses the same hash function as the target address hash scheduling algorithm. Its algorithm flow and the target address hash scheduling algorithm basically similar, in addition to the request of the destination IP address for the requested source IP address, so here is not a narrative. Destination hashing (target address hash)

The target address hash scheduling algorithm is also a load balancer for the destination IP address, which is a static mapping algorithm that maps a destination IP address to a server through a hash (hash) function. The target address hash scheduling algorithm first finds the corresponding server from the statically allocated hash list according to the destination IP address of the request, as hash key (hash key), if the server is available and not overloaded, sends the request to the server, otherwise returns NULL. consistent hash (Consistent-hash)

Consistent hash (Consistent-hash) least-connection (minimum connection)

The least connection scheduling algorithm is to assign the new connection request to the server with the least number of connections, and the minimum connection scheduling is a dynamic scheduling short algorithm, which estimates the server load balancer by the number of connections currently active by the server, and the scheduler needs to record the number of connections that each server has established when a request is dispatched to a server , its number of connections plus 1, when the connection is aborted or timed out, its number of connections minus one, in the implementation of the system, we also introduced when the server's weight is 0 o'clock, indicating that the server is not available without being dispatched.

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.