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

How to realize the load balancing algorithm of the server

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to realize the load balancing algorithm of the server". In the daily operation, I believe that many people have doubts about how to realize the load balancing algorithm of the server. The editor consulted all kinds of data and sorted out the simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to realize the load balancing algorithm of the server". Next, please follow the editor to study!

Load balancing (Load balancing) is a technology that distributes resources evenly among multiple computers (network, CPU, disk) to improve resource utilization. Using load balancing can maximize service throughput and minimize response time. At the same time, multiple server nodes are used to replace single point service when load balancing is used, which also improves the availability of services.

Load balancing can be realized by software or hardware, such as the famous F5 load balancing equipment, software such as load balancing in NGINX, and load balancing in Springcloud Ribbon components.

If you don't know what load balancing is for when you see it here, you can only put a picture. After all, there is nothing to say.

Load balancer needs to make sure that each server is requested roughly the same number of times under multiple requests. But in actual production, the performance of each machine may be different, we will want the machine with good performance to bear more requests, which is also a normal demand.

If you don't understand in this way, I'll give you another example. A row of cute bears (servers) stand up.

At this time, someone (user) is coming to hit him in the face (requesting access).

So how can we get each cute bear beaten roughly the same number of times?

Or maybe Bear 4 is fat and has twice as much anti-hit ability as others, so how can we increase the number of times Bear 4 is beaten twice as much as others?

Or the strength of each shot is different, heavy and light, as it happens that Bear 4 always bears this kind of force and slaps in the face. Bear 4 is about to save bear trouble, do you want to continue to hit it?

These are all questions of value thinking.

After talking so much and my mouth is dry, my hands are so hungry that I can't wait to roll up the code.

1. Random access

As mentioned above, in order to load balance, we must ensure that after multiple shots, the number of hits from bear 1 to bear 4 is balanced. For example, using random access, according to the mathematical theory of probability, the more random shots, the more times each bear will be beaten. Code implementation is also relatively simple, using a random number, random access on it.

/ * * Server list * /

Private static List serverList = new ArrayList ()

Static {

ServerList.add ("192.168.1.2")

ServerList.add ("192.168.1.3")

ServerList.add ("192.168.1.4")

ServerList.add ("192.168.1.5")

}

/ * *

* Random routing algorithm

, /

Public static String random () {

/ / copy the collection used for traversal to prevent changes in the collection during the operation

List tempList = new ArrayList (serverList.size ())

TempList.addAll (serverList)

/ / Random number random access

Int randomInt = new Random () .nextInt (tempList.size ()

Return tempList.get (randomInt)

}

Because non-thread-safe collections are used, copies of collections are manipulated when accessing operations, as is the case in the following rotational training methods.

Write a simulation request method, request 10w times, record the request result.

Public static void main (String [] args) {

HashMap serverMap = new HashMap ()

For (int I = 0; I < 20000; iTunes +) {

String server = random ()

Integer count = serverMap.get (server)

If (count = = null) {

Count = 1

} else {

Count++

}

/ / record

ServerMap.put (server, count)

}

/ / overall routing result

For (Map.Entry entry: serverMap.entrySet ()) {

System.out.println ("IP:" + entry.getKey () + ", times:" + entry.getValue ())

}

}

Run to get the requested result.

IP:192.168.1.3, times: 24979

IP:192.168.1.2, times: 24896

IP:192.168.1.5, times: 25043

IP:192.168.1.4, times: 25082

The number of visits to each server is close to 2.5w, which means load balancing. However, random is random after all, and the number of visits can not be guaranteed to be absolutely uniform.

two。 Rotational training visit

The interview is much easier. Take Bear 1 to Bear 4 above. We hit Bear 1 in the face one after another, Bear 2 in Bear 1, Bear 3 in Bear 2, Bear 4 in Bear 1, and Bear 4 in Bear 1. But there is always a price to be paid to ensure uniformity. Random interviews are needed at random. What is needed to ensure rotational training in rotation visits?

/ * * Server list * /

Private static List serverList = new ArrayList ()

Static {

ServerList.add ("192.168.1.2")

ServerList.add ("192.168.1.3")

ServerList.add ("192.168.1.4")

ServerList.add ("192.168.1.5")

}

Private static Integer index = 0

/ * *

* Random routing algorithm

, /

Public static String randomOneByOne () {

/ / copy the collection used for traversal to prevent changes in the collection during the operation

List tempList = new ArrayList (serverList.size ())

TempList.addAll (serverList)

String server = ""

Synchronized (index) {

Index++

If (index = = tempList.size ()) {

Index = 0

}

Server = tempList.get (index)

}

Return server

}

As can be seen from the code, in order to ensure rotation training, the location of the last visit must be recorded, and in order to avoid problems in the case of concurrency, locks must be added when using location records, which obviously increases the performance overhead.

Still use the above test code to test the 10w request load.

IP:192.168.1.3, times: 25000

IP:192.168.1.2, times: 25000

IP:192.168.1.5, times: 25000

IP:192.168.1.4, times: 25000

3. Rotation training weighting

The above demonstrates the rotation training method, but also remember that the bear 4 proposed at the beginning is fat and has strong anti-hitting ability, and can withstand twice as many beatings as others? The above two ways do not reflect the characteristics of bear 4, bear 4 secretly happy, painless. But Bear 1 to Bear 3 is on the verge of collapse, no, we have to let the fat fight more, those who can do more work, and improve the overall performance.

/ * * Server list * /

Private static HashMap serverMap = new HashMap ()

Static {

ServerMap.put ("192.168.1.2", 2)

ServerMap.put (192.168.1.3 ", 2)

ServerMap.put (192.168.1.4 ", 2)

ServerMap.put (192.168.1.5 ", 4)

}

Private static Integer index = 0

/ * *

* weighted routing algorithm

, /

Public static String oneByOneWithWeight () {

List tempList = new ArrayList ()

HashMap tempMap = new HashMap ()

TempMap.putAll (serverMap)

For (String key: serverMap.keySet ()) {

For (int I = 0; I < serverMap.get (key); iTunes +) {

TempList.add (key)

}

}

Synchronized (index) {

Index++

If (index = = tempList.size ()) {

Index = 0

}

Return tempList.get (index)

}

}

This time, the overall performance of each server is recorded, and a value is given. The higher the value, the better the performance. The more requests you can take, you can see that server 192.168.1.5 has a performance of 4, twice as much as other servers, and still requests for testing for 10 weeks.

IP:192.168.1.3, times: 20000

IP:192.168.1.2, times: 20000

IP:192.168.1.5, times: 40000

IP:192.168.1.4, times: 20000

192.168.1.5 took on twice as many requests.

4. Random weighting

The way of random weighting is roughly the same as that of rotation training, except that the use of mutex lock rotation training is replaced by random access. According to the probability theory, when the number of visits increases, service access will also achieve load balance.

/ * * Server list * /

Private static HashMap serverMap = new HashMap ()

Static {

ServerMap.put ("192.168.1.2", 2)

ServerMap.put (192.168.1.3 ", 2)

ServerMap.put (192.168.1.4 ", 2)

ServerMap.put (192.168.1.5 ", 4)

}

/ * *

* weighted routing algorithm

, /

Public static String randomWithWeight () {

List tempList = new ArrayList ()

HashMap tempMap = new HashMap ()

TempMap.putAll (serverMap)

For (String key: serverMap.keySet ()) {

For (int I = 0; I < serverMap.get (key); iTunes +) {

TempList.add (key)

}

}

Int randomInt = new Random () .nextInt (tempList.size ()

Return tempList.get (randomInt)

}

Still requesting testing for 10 weeks, the weight of 192.168.1.5 is nearly twice that of other servers.

IP:192.168.1.3, times: 19934

IP:192.168.1.2, times: 20033

IP:192.168.1.5, times: 39900

IP:192.168.1.4, times: 20133

5. IP-Hash

The above methods either use random numbers or use rotational training, and finally achieve the requested load balancing. However, there is also an obvious disadvantage, that is, multiple requests from the same user may not be handled by the same service. Then the problem arises. If your service depends on session, then session will also be lost because of different services, which is not what we want. So there is a way to decide which server to request according to the ip of the requester. This approach ensures that the requests of the same user fall on the same service.

Private static List serverList = new ArrayList ()

Static {

ServerList.add ("192.168.1.2")

ServerList.add ("192.168.1.3")

ServerList.add ("192.168.1.4")

ServerList.add ("192.168.1.5")

}

/ * *

* ip hash routing algorithm

, /

Public static String ipHash (String ip) {

/ / copy the collection used for traversal to prevent changes in the collection during the operation

List tempList = new ArrayList (serverList.size ())

TempList.addAll (serverList)

/ / Hash the requested server

Int index = ip.hashCode ()% serverList.size ()

Return tempList.get (Math.abs (index))

}

At this point, the study of "how to implement the load balancing algorithm of the server" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report