Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the algorithms of load balancing

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about the content of load balancing algorithm. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Source address hash algorithm

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

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

Or the previous operation, pseudo code:

Private static Map serviceWeightMap = new HashMap (); 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 serverMap = new HashMap (); serverMap.putAll (serviceWeightMap) / / get the IP address list Set keySet = serverMap.keySet (); ArrayList keyList = new ArrayList (); keyList.addAll (keySet); int hashCode = remoteIp.hashCode (); int pos = hashCode% keyList.size (); return keyList.get (pos);}

This code comes from Will.Shun, and I didn't quite understand what it meant when I saw it at that time. Later, I looked at it and found that it was very similar to its interpretation. A hash value was calculated by the hash function, and the size of the server list was modularized. The result is the serial number of the server address to be accessed.

two。 Weighted polling algorithm

Let's take a look at the weighted rotation training algorithm. Let's first take a look at the weight configuration in Nginx:

Http {upstream cluster {server a weight=1; server b weight=2; server c weight=3;}

If Nginx receives every 6 client requests, it forwards 1 of them to backend a, 2 of them to backend b, and 3 of them to backend c.

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

Pseudo code of weighted rotation training algorithm:

Private static Map serviceWeightMap = new HashMap (); 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 () {/ / re-create a map to avoid concurrency problems caused by server online and offline Map serverMap = new HashMap (); serverMap.putAll (serviceWeightMap); / / get IP address list Set keySet = serverMap.keySet (); Iterator it = keySet.iterator (); List serverList = new ArrayList () While (it.hasNext ()) {String server = it.next (); Integer weight = serverMap.get (server); for (int iTuno; I serverList.size ()) {pos = 0;} server = serverList.get (pos); pos++;} return server;}

In fact, it is flawed in the weighted rotation training algorithm. under some special weights, weighted round-robin scheduling will generate an uneven sequence of instances, and this unsmooth load may lead to instantaneous high load in some instances. resulting in the risk of system downtime. In order to solve the defect of this scheduling, there is smooth weighted rotation scheduling. Interested students must take a look at this smooth weighted rotation training.

3. Weighted random algorithm

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

Private static Map serviceWeightMap = new HashMap (); 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 () {/ / re-create a map to avoid concurrency problems caused by server online and offline Map serverMap = new HashMap (); serverMap.putAll (serviceWeightMap); / / get IP address list Set keySet = serverMap.keySet (); List serverList = new ArrayList (); Iterator it = keySet.iterator () While (it.hasNext ()) {String server = it.next (); Integer weight = serverMap.get (server); for (int iTuno; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report