This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

preface

Good attitude, not so tired. In a good mood, all you see is beautiful scenery.

"If you can't solve a problem for a while, use this opportunity to see your limitations and put yourself out of order." As the old saying goes, it's easy to let go. If you are distracted by something, learn to disconnect. Cut out the paranoia, the trash in the community, and get rid of the negative energy. Good attitude, not so tired. In a good mood, all you see is beautiful scenery.

With regard to load balancing, it is important to choose a good load balancer to achieve a high performance cluster, and it is also important to choose an appropriate load balancing algorithm for different business scenarios.


Source address hashing algorithm

The only algorithm that doesn’t lose policy, but load balancing has a lot to do with the source data information and the hash algorithm.

The idea of source address hash method is that according to the IP address of the client requested by the service consumer, a hash value is calculated through the hash function, and the hash value and the size of the server list are modular operation, and the result is the serial number of the server address to be accessed. The source address hash method is used for load balancing. If the server list remains unchanged, the same IP client will be mapped to the same background server for access.

Pseudo code:

private static Map<String, Integer> serviceWeightMap = new HashMap<String, Integer>();

static {
    serviceWeightMap.put("192.168.1.100".1);
    serviceWeightMap.put("192.168.1.101".1);
    serviceWeightMap.put("192.168.1.102".4);
    serviceWeightMap.put("192.168.1.103".1);
   }

   public static String testConsumerHash(String remoteIp) {

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

       // Get the list of IP addresses
       Set<String> keySet = serverMap.keySet();
       ArrayList<String> keyList = new ArrayList<String>();
       keyList.addAll(keySet);

       int hashCode = remoteIp.hashCode();
       int pos = hashCode % keyList.size();

       return keyList.get(pos);
   }
Copy the code

In fact, this code and its explanation is very similar, through the hash function calculation to get a hash value, the hash value and the size of the server list modular operation, the result is to access the serial number of the server address.


Weighted polling algorithm

To look at the weighted polling algorithm, let’s first look at the weight configuration in Nginx:

http {
upstream cluster {
server a weight=1;
server b weight=2;
server c weight=3;
}
Copy the code

If Nginx receives six requests from clients, it forwards one of them to back-end A, two of them to back-end B, and three of them to back-end C.

The result of the weighted polling algorithm is to generate a sequence of servers. Each time a request comes in, the next server is taken out of the sequence to process the request.

Weighted polling algorithm pseudocode:

private static Map<String, Integer> serviceWeightMap = new HashMap<String, Integer>();

static {
    serviceWeightMap.put("192.168.1.100".1);
    serviceWeightMap.put("192.168.1.101".1);
    serviceWeightMap.put("192.168.1.102".4);
    serviceWeightMap.put("192.168.1.103".1);
   }


    public static String testWeightRoundRobin(a) {

        // Create a new map to avoid concurrency problems due to server online and offline
        Map<String, Integer> serverMap = new HashMap<String, Integer>();
        serverMap.putAll(serviceWeightMap);

        // Get the list of IP addresses
        Set<String> keySet = serverMap.keySet();
        Iterator<String> it = keySet.iterator();

        List<String> serverList = new ArrayList<String>();

        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 > serverList.size()) {
                pos = 0;
            }

            server = serverList.get(pos);
            pos++;
        }

        return server;
}
Copy the code

In fact, there are defects in the weighted polling algorithm. Under some special weights, the weighted polling scheduling will generate uneven instance sequences. Such uneven load may cause some instances to have instantaneous high load, leading to the risk of system downtime. In order to solve the defects of this scheduling, there is a smooth weighted polling scheduling.


Third, weighted random algorithm

The weighted random method is similar to the weighted polling method, which configures different weights according to the different configuration and load of the background server. The difference is that it randomly selects servers by weight, not order.

private static Map<String, Integer> serviceWeightMap = new HashMap<String, Integer>();

    static {
        serviceWeightMap.put("192.168.1.100".1);
        serviceWeightMap.put("192.168.1.101".1);
        serviceWeightMap.put("192.168.1.102".4);
        serviceWeightMap.put("192.168.1.103".1);
       }

    public static String testWeightRandom(a) {
        // Create a new map to avoid concurrency problems due to server online and offline
        Map<String, Integer> serverMap = new HashMap<String, Integer>();
        serverMap.putAll(serviceWeightMap);

        // Get the list of IP addresses
        Set<String> keySet = serverMap.keySet();
        List<String> serverList = new ArrayList<String>();
        Iterator<String> it = keySet.iterator();

        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(serverList.size());

        String server = serverList.get(randomPos);

        return server;
    }
Copy the code

The difference here is that the server is acquired by a random algorithm.

In fact, we can think of an example: for example, in the following scenario: there is A set S, which has for example A,B,C, and D. So we want to pick one of these at random, but with different probabilities, so let’s say we want A 50% chance of picking A, A 20% chance of picking B and C, and A 10% chance of picking D. In general, we can attach a weight to each term, and the probability of extraction is proportional to that weight.


HTTP internationalization

HTTP messages can carry content in any language, just as they can carry images, movies, or any type of media. For HTTP, entity bodies are simply containers for binary information.

To support international content, the server needs to tell the client the alphabet and language of each document so that the client can properly unpack the information in the document into characters and present the content to the user.

The server uses the charset parameter in the CONTent-Type header and the Content-language header to inform the client of the document’s alphabet and Language. These “information boxes” begin with a description of what is inside the physical body, how to translate the content into the appropriate characters for display on the screen and what language the words inside are in.

At the same time, the client needs to tell the server what language the user understands and what alphabet encoding algorithm is installed on the browser. The client sends the accept-charset and accept-language headers to tell the server which character set encoding algorithms and languages it understands and the order in which they are prioritised.

If you’re interested, see the HTTP Diagram and the Definitive HTTP Guide.


🎉 summary:

  • For more references, see here:The Blog of Chan Wing Kai

  • Like the small partner of the blogger can add a concern, a thumbs-up oh, continue to update hey hey!