Load Balancing Algorithm for Large Distributed Websites

In my spare time, I will deeply study the e-book on the design and practice of large-scale distributed website architecture. You can download it online. I will upload it to my blog later. Oh, I hope it helps everyone.

There are many types of load balancing algorithms. Common load balancing algorithms include round-robin method, random method, source address hash method, weighted round-robin method, weighted random method, minimum link sending, etc. The corresponding algorithm should be selected according to specific usage scenarios. .

 1. Round Robin method

Allocate requests to back-end servers in turn, and it treats each back-end server in a balanced manner, regardless of the actual number of connections on the server and the current system load.

Here, a map variable of serverWeightMap is initialized to represent the mapping of server addresses and weights, so as to simulate the implementation of the polling algorithm. The set weight values ​​will be used in the later weighting algorithm, which will not be described here.

The variable is initialized as follows:

 serverWeightMap = new HashMap<String,Intager>();

 serverWeightMap.put('192.168.1.100',1);

 serverWeightMap.put('192.168.1.101',1);

// weight is 4

 serverWeightMap.put('192.168.1.102',4);

 serverWeightMap.put('192.168.1.103',1);

 serverWeightMap.put('192.168.1.104',1);

// weight is 3

 serverWeightMap.put('192.168.1.105',3);

 serverWeightMap.put('192.168.1.106',1);

// weight is 2

 serverWeightMap.put('192.168.1.107',2);

 serverWeightMap.put('192.168.1.108',1);

 serverWeightMap.put('192.168.1.109',1);

 serverWeightMap.put('192.168.1.110',1);

The weight of IP address 192.168.1.102 is 4, the weight of 192.168.1.105 is 3, and the weight of 192.168.1.107 is 2

Through this address list, some key codes of the implemented polling algorithm are as follows:

public static String testRoundRobin(){

 //Recreate a map to avoid concurrency caused by server online and offline

 Map<String,Integer> serverMap = new HashMap<String,Integer> ();

 serverMap.putAll (serverWeightMap);

 // Get a list of IP addresses

 Set<String> keySet = serverMap.keySet(); //IP address string

 ArrayList<String>keyList = new ArrayList<String>(); // Convert IP collection string to array

 keyList.addAll(keySet);

 String server = null;

 synchronized(pos){

  if(pos >= keySet.size()){ // It seems that the number of servers is automatically incremented by pos >= 11

    pos = 0;

  }

  server = keyList.get(pos); // Get the server

  pos++;

 }

return server;

}

Since the server address in serverWeightMap is dynamic in daily life, that is to say, it can be put on and off the shelf at any time, such as downtime, etc., in order to avoid possible concurrency problems such as array out of bounds, through the local variable serverMap of the new method, first copy the domain variable to the present Local, to avoid being modified by multiple threads, the location variable pos of the polling algorithm is to ensure the order of server selection, and a synchronized lock needs to be added during the operation, so that only one thread can modify the pos value at the same time, otherwise the pos variable If it is modified concurrently, the order of server selection cannot be guaranteed.

Using the polling strategy to achieve absolute balance of request transfer, the cost is quite high: in order to ensure the mutual exclusion of variable modification by pos, it is necessary to introduce a heavyweight lock synchronized, which will cause the concurrent throughput of this section of polling code to increase. Significant decline, resulting in reduced performance.

 

2. Random

Through the system random function, one of the back-end servers is randomly selected for access according to the size of the back-end server list. According to the probability and statistics theory, as the call volume increases, the actual effect is getting closer and closer to evenly distributing traffic to each server. A backend server can also achieve the effect of polling

Code for random algorithm:

 public static String testRandom(){

     //Recreate a map to avoid concurrency caused by server online and offline

     Map<String,Integer> serverMap = new HashMap<String,Integer> ();

     serverMap.putAll (serverWeightMap);

      // Get a list of IP addresses

      Set<String> keySet = serverMap.keySet(); //IP address string

      ArrayList<String>keyList = new ArrayList<String>(); // Convert IP collection string to array

      keyList.addAll(keySet);

      Random random = new Random();

      int randomPos = random.nextInt(keyLIst.size()); // here count the number of servers 11

      String server = keyList.get(randomPos );

      return server;

 }

 Through the nextInt method of Random, a random value in the range of 0~keyList.size() is obtained, and a server address is randomly obtained from the server list and returned. Based on the statistics of probability theory, the greater the throughput, the closer the effect of the random algorithm is to that of the polling algorithm. This approach doesn't come at a huge performance cost.

 

3. Source address hashing (Hash)

The source address hash means to obtain the IP address value accessed by the client, calculate a value through the hash function, and use this value to perform a modulo operation on the size of the server list, and the result is the serial number of the accessed server. Hash method is used for load balancing. When the client with the same IP address does not change the list of backend servers, it will be mapped to the same backend server for access every time.

Source address hash algorithm implementation code:

public static String testConsumerHash(String remoteip){

     //Recreate a map to avoid concurrency caused by server online and offline

     Map<String,Integer> serverMap = new HashMap<String,Integer> ();

     serverMap.putAll (serverWeightMap);

      // Get a list of IP addresses

      Set<String> keySet = serverMap.keySet(); //IP address string

      ArrayList<String>keyList = new ArrayList<String>(); // Convert IP collection string to array

      keyList.addAll(keySet);

      int hashCode = remoteip.hashCode();

      int serverListSize = keyList.size();

      int serverPos = hashCode % serverListSize;

      return keyList.get(serverPos);

}

The client remoteip parameter passed in through the parameter, obtain its hash value, and modulo the size of the server list. The result is the index value of the selected server in the server list. This algorithm guarantees that the same client IP address will be hashed to the same backend server until the list of backend servers changes. According to this feature, the session session between the server consumer and the service provider is possible.

 

 4. Weight Round Robin

Different back-end servers may have different machine configurations and current system loads, so their pressure resistance capabilities are also different. A machine with a high load and a low load is assigned a higher weight to handle more requests, while a machine with a low load and a high load is assigned a lower weight to reduce its load. Weighted round robin can handle this well. issues and assigns the order of requests to backends by weight

Weighted polling code:

public static String testWeightRoundRobin(){

     //Recreate a map to avoid concurrency caused by server online and offline

     Map<String,Integer> serverMap = new HashMap<String,Integer> ();

     serverMap.putAll (serverWeightMap);

      // Get a list of IP addresses

      Set<String> keySet = serverMap.keySet(); //IP address string

      Iterator<Strign>it = keySet.iterator();

      List<String>keyList = new ArrayList<String>(); // Convert IP collection string to array

      while(it.hasNext()){

          String server = it.next();

           Integer weight = serverMap.get(server);

           for(int i=0;i<weight;i++){

              serverList.add(server);

           }

       }

      String server = null;

      synchronized(pos){

          if(pos >= keySet.size()){ // It seems that the number of servers is automatically incremented by pos >= 11

              pos = 0;

         }

          server = keyList.get(pos); // Get the server

          pos++;

     }

      return server;

}

Similar to the polling algorithm, just add a piece of weight calculation code before obtaining the server address, and repeatedly add the address to the server list according to the size of the weight. The larger the weight, the more requests the server gets each round.

 

5. Weight Random

Similar to the weighted round-robin method, the weighted random method also configures different weights according to the different configurations and load conditions of the back-end servers. The difference is that it installs weights to randomly select servers, not in order.

Code for weighted random implementation:

public static String testWeightRandom(){

 //Recreate a map to avoid concurrency caused by server online and offline

     Map<String,Integer> serverMap = new HashMap<String,Integer> ();

     serverMap.putAll (serverWeightMap);

      // Get a list of IP addresses

      Set<String> keySet = serverMap.keySet(); //IP address string

      Iterator<String> it = keySet.iterator();

      List<String>serverList = new ArrayList<String>(); // Convert IP collection string to array

      while(it.hasNext()){

          String server = it.next();

           Integer weight = serverMap.get(server);

           for(int i=0;i<weight;i++){

              serverList.add(server);

           }

       }

      Random random = new Random();

      int randomPos = random.nextInt(keyLIst.size()); // here count the number of servers 11

      String server = keyList.get(randomPos );

      return server;

}

 

6. Least Connections

The minimum number of connections method is more flexible and intelligent. Since the configuration of the back-end servers is not the same, the processing of requests is fast or slow. It dynamically selects the one with the least current backlog connections according to the current connection situation of the back-end server. One server is used to process the current request, improve the utilization efficiency of the back-end server as much as possible, and distribute the load to each server reasonably. Since the minimum number of connections involves the summary and perception of the number of server connections, the process and implementation are more complicated and tedious, so please choose carefully.

If you have any questions, please give more pointers, thank you!

---------------over------------

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326961906&siteId=291194637