Load balancing method and device, computer equipment and storage medium
By determining the relationship between the number of requests for the same resource and the server's processing capacity, a first identifier is set and appended to the hash key value. The consistent hashing algorithm is then used to distribute requests to the same server, solving the problem of low processing efficiency in existing technologies and achieving load balancing and efficient processing.
Patent Information
- Application Number
- CN202411375720.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-29
- Publication Date
- 2025-01-07
AI Technical Summary
Existing load balancing methods struggle to simultaneously meet the requirements of processing efficiency and server load balancing when handling a large number of concurrent requests for the same resource, resulting in low processing efficiency and an inability to meet current timeliness requirements.
By determining the relationship between the number of requests for the same resource and the server's processing capacity, a corresponding first identifier is set and appended to the hash key value. The consistent hashing algorithm is then used to distribute requests with the same first identifier to the same server for processing, ensuring that requests are concentrated on a small number of servers.
This ensures that requests for the same resource are processed on the same server as much as possible, meeting the requirements of server load balancing and improving overall processing efficiency and user experience.
Smart Images

Figure CN119276869A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a load balancing method, device, computer equipment and storage medium. Background Art
[0002] With the development of the Internet, service providers need to handle a large number of concurrent requests, which a single server can no longer handle. In this context, load balancing servers came into being.
[0003] In the current environment, load balancing servers distribute requests based on polling or hashing. However, for a large number of concurrent requests for the same resource, the current load balancing method will either distribute the requests to too many servers for processing, or allocate them to a single server for centralized processing. The above processing methods will have a significant impact on processing efficiency and it is difficult to meet the current requirements for timeliness. Summary of the invention
[0004] In view of this, the present disclosure proposes a load balancing method, apparatus, computer device and storage medium to solve or partially solve the above problems.
[0005] Based on the above objectives, the present disclosure provides a load balancing method in a first aspect, including:
[0006] In response to obtaining at least one task request for the same resource, determining the request quantity of the at least one task request, and determining a first identifier corresponding to the at least one task request according to a quantitative relationship between the request quantity and a set threshold; wherein the set threshold corresponds to a processing capacity of a server that processes the task request;
[0007] Determine a second identifier corresponding to the at least one task request, and generate a hash key value corresponding to the at least one task request according to the first identifier and the second identifier; wherein the second identifier corresponds to the identity information of the at least one task request;
[0008] The server allocation of the at least one task request is performed according to the hash key value.
[0009] In some exemplary embodiments, determining the request quantity of the at least one task request includes:
[0010] Divide the time into multiple time periods according to the set rules, and determine whether there is a historical task request for the same resource in the current time period;
[0011] In response to the existence of historical task requests, a first number of the historical task requests is determined, and the number of the at least one task request is accumulated on the first number to determine the request number.
[0012] In some exemplary embodiments, after dividing the time according to the set rule to generate multiple time periods, the method further includes:
[0013] In response to a crossing between time periods, the first number is cleared.
[0014] In some exemplary embodiments, determining the request quantity of the at least one task request includes:
[0015] A second number of requests that have been acquired for the same resource is determined, and the number of the at least one task request is accumulated based on the second number to generate the request number.
[0016] In some exemplary embodiments, determining the first identifier corresponding to the at least one task request according to the quantitative relationship between the number of requests and a set threshold value includes:
[0017] When accumulating the number of the at least one task request, assigning a number to the at least one task request in sequence, and determining a corresponding relationship between the number and a multiple of the set threshold;
[0018] The first identifier is set for the at least one task request according to the corresponding relationship.
[0019] In some exemplary embodiments, determining the first identifier corresponding to the at least one task request according to the quantitative relationship between the number of requests and a set threshold value includes:
[0020] The value range of the first identifier is determined according to the multiple relationship between the number of requests and the set threshold, and a random selection is performed within the value range to set the first identifier corresponding to any task request.
[0021] In some exemplary embodiments, determining the first identifier corresponding to the at least one task request according to the quantitative relationship between the number of requests and a set threshold value includes:
[0022] Determining a value range of the first identifier according to a quantitative relationship between the number of requests and a set threshold;
[0023] The at least one task request is grouped according to a set threshold value, so that the first identifier of each group of task requests corresponds to a value within the value range.
[0024] In some exemplary embodiments, generating a hash key value corresponding to the at least one task request according to the first identifier and the second identifier includes:
[0025] The first identifier and the second identifier are concatenated using a concatenation function to obtain the hash key value.
[0026] In some exemplary embodiments, allocating the server of the at least one task request according to the hash key value includes:
[0027] The hash key value is subjected to consistent hash routing allocation using a set hash algorithm, and at least one task request with the same first identifier is allocated to the same server.
[0028] In some exemplary embodiments, the set hash algorithm includes at least one of a message digest algorithm, a secure hash algorithm, and a cyclic redundancy check algorithm.
[0029] In a second aspect of the present disclosure, a load balancing device is provided, comprising:
[0030] A first module is used to determine the request quantity of the at least one task request in response to obtaining at least one task request for the same resource, and determine a first identifier corresponding to the at least one task request according to a quantitative relationship between the request quantity and a set threshold; wherein the set threshold corresponds to a processing capacity of a server that processes the task request;
[0031] A second module is used to determine a second identifier corresponding to the at least one task request, and generate a hash key value corresponding to the at least one task request according to the first identifier and the second identifier; wherein the second identifier corresponds to the identity information of the at least one task request;
[0032] The third module is used to allocate the server of the at least one task request according to the hash key value.
[0033] In a third aspect of the present disclosure, a computer device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method described in the first aspect is implemented.
[0034] According to a fourth aspect of the present disclosure, a non-transitory computer-readable storage medium is provided, wherein the non-transitory computer-readable storage medium stores computer instructions, wherein the computer instructions are used to enable a computer to execute the method described in the first aspect.
[0035] From the above, it can be seen that the load balancing method, device, computer equipment and storage medium provided by the present invention compare the number of requests for the same resource with the processing capacity of the server, thereby setting corresponding first identifiers for these requests, and splicing the first identifiers into the hash key value, so that when performing consistent hashing of the server, requests with the same first identifier can be allocated to the same server for processing according to the first identifier, so that multiple requests for the same resource can be allocated, and according to the relationship between the number of requests and the processing capacity, several first identifiers are set according to the number of processors required, so that requests for the same resource can be concentrated on several processors to complete, thereby satisfying the requirement that requests for the same resource should be processed on the same server as much as possible, and satisfying the problem of server load balancing, thereby improving the overall processing efficiency and user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present disclosure or related technologies, the drawings required for use in the embodiments or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0037] Figure 1 A schematic diagram of an exemplary system 100 according to an embodiment of the present disclosure is shown.
[0038] Figure 2 A flowchart of an exemplary method 200 provided in an embodiment of the present disclosure is shown.
[0039] Figure 3 A schematic diagram showing the effects of the total QPS and the QPS of a single server provided by an embodiment of the present disclosure is shown.
[0040] Figure 4 A schematic diagram of an exemplary device 400 provided by an embodiment of the present disclosure is shown.
[0041] Figure 5 A schematic diagram of an exemplary computer device 500 provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0042] In order to make the purpose, technical solutions and advantages of this specification more clear, this specification is further described in detail below in combination with specific embodiments and with reference to the accompanying drawings.
[0043] It should be noted that, unless otherwise defined, the technical terms or scientific terms used in the embodiments of the present disclosure should be understood by people with ordinary skills in the field to which the present disclosure belongs. The "first", "second" and similar words used in the examples of the present disclosure do not indicate any order, quantity or importance, but are only used to distinguish different components. "Including" or "including" and similar words mean that the elements or objects appearing before the word cover the elements or objects listed after the word and their equivalents, without excluding other elements or objects. "Connect" or "connected" and similar words are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. "Up", "down", "left", "right" and the like are only used to indicate relative positional relationships. When the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0044] It is understandable that before using the technical solutions disclosed in the embodiments of the present disclosure, the types, scope of use, usage scenarios, etc. of the personal information involved in the present disclosure should be informed to the user and the user's authorization should be obtained in an appropriate manner in accordance with relevant laws and regulations.
[0045] For example, in response to receiving an active request from a user, a prompt message is sent to the user to clearly prompt the user that the operation requested to be performed will require obtaining and using the user's personal information. Thus, the user can autonomously choose whether to provide personal information to software or hardware such as an electronic device, application, server, or storage medium that performs the operation of the technical solution of the present disclosure according to the prompt message.
[0046] As an optional but non-limiting implementation, in response to receiving an active request from the user, the prompt information may be sent to the user in the form of a pop-up window, in which the prompt information may be presented in text form. In addition, the pop-up window may also carry a selection control for the user to choose "agree" or "disagree" to provide personal information to the electronic device.
[0047] It is understandable that the above notification and the process of obtaining user authorization are merely illustrative and do not constitute a limitation on the implementation of the present disclosure. Other methods that meet the relevant laws and regulations may also be applied to the implementation of the present disclosure.
[0048] It is understandable that the data involved in this technical solution (including but not limited to the data itself, the acquisition or use of the data) shall comply with the requirements of relevant laws, regulations and relevant provisions.
[0049] In order to make the purpose, technical solutions and advantages of this specification more clear, this specification is further described in detail below in combination with specific embodiments and with reference to the accompanying drawings.
[0050] Figure 1 FIG. 1 is a schematic diagram of an exemplary system 100 according to an embodiment of the present disclosure. The system 100 may be a system for implementing load balancing.
[0051] like Figure 1 As shown, taking the case where a terminal device and a server jointly execute an object display method as an example, the system 100 may include a terminal device 102, a server 104, and a database server 106. The terminal device 102 and the server 104 are connected via a network, such as a wired or wireless network connection. Optionally, the device for implementing load balancing may be integrated in the terminal device 102. The database server 106 and the server 104 are connected via a network, such as a wired or wireless network connection. The database server 106 may store various data related to the execution of the load balancing method.
[0052] The terminal device 102 may be installed with various applications (Application, APP, Chinese abbreviation: application), such as image processing applications, video conferencing applications, life service applications, reading applications, video applications, social applications, payment applications, web browsers and instant messaging tools, etc. These applications can be used to generate tasks and generate multiple task requests based on tasks. As an optional example, the application (APP) installed on the terminal device 102 can be downloaded and installed from the server 104.
[0053] The terminal device 102 here can be hardware or software. When the terminal device 102 is hardware, it can be various electronic devices with a display screen, including but not limited to smart phones, tablet computers, e-book readers, MP3 players, laptop computers (Laptops) and desktop computers (PCs), etc. When the terminal device 102 is software, it can be installed in the electronic devices listed above. It can be implemented as multiple software or software modules (for example, to provide distributed services), or it can be implemented as a single software or software module. It is not specifically limited here.
[0054] The server 104 may be a server that provides various services. For example, it may be a background server that provides support for various applications displayed on the terminal device 102. The database server 106 may also be a database server that provides various services. It is understood that if the server 104 can implement the relevant functions of the database server 106, the database server 106 may not be provided in the system 100.
[0055] Here, the server 104 and the database server 106 can also be hardware or software. When they are hardware, they can be implemented as a distributed server cluster consisting of multiple servers, or as a single server. When they are software, they can be implemented as multiple software or software modules (for example, to provide distributed services), or as a single software or software module. No specific limitation is made here.
[0056] It should be noted that the load balancing method provided in the embodiment of the present disclosure can be executed by the system 100. Specifically, it can be executed interactively between the terminal device 102, the server 104 and the database server 106. It is understood that in the case where the terminal device 102 has the functions required by the server 104 and the database server 106 to execute the load balancing method, the terminal device 102 can also execute it alone. It should be understood that Figure 1 The number of terminal devices 102, servers 104, database servers 106 and users 108 in the figure is only for illustration. According to actual needs, there may be any number of terminal devices, users, servers and database servers.
[0057] As described in the background technology, for load balancing servers, load balancing algorithms are an important component, which plays a vital role in the balancing effect and the stability of the entire system. In some examples, the algorithms that can achieve load balancing can be divided into: Round Robin algorithm: send requests to each server in turn; Least Connections algorithm: send requests to the server with the least number of connections; Weighted Round Robin algorithm: allocate requests according to the weight of the server on the basis of polling, and the server with high weight receives more requests; Weighted Least Connections algorithm: combine the least connection and weight to allocate requests; Hash algorithm: allocate requests according to the hash value of the user-selected parameters, and ensure that requests with the same parameters are allocated to the same server. However, when adding or reducing servers, the allocated servers will change, and in extreme cases all requests will be allocated to different servers; Consistency Hash algorithm: based on hashing, minimize the impact of adding or reducing servers, and only a small part of the requests will be allocated to the new instance. These algorithms can be applied to different usage scenarios, and they play an important role in the Internet system.
[0058] The inventors of the present disclosure have discovered that, with the development of technology, the same resource may generate multiple requests, or even multiple requests concurrently at the same time, and therefore, it is hoped that the servers that process these requests have a certain "affinity". For example: requests for the same document (same document ID, regarded as the same resource) are expected to be requested to the same server as much as possible, so as to better utilize the local cache to accelerate processing efficiency, and adopt related strategies such as singleflight to reuse the response of the previous request, etc., to improve the system's ability to cope with sudden traffic. At the same time, when the number of concurrent requests exceeds the rated processing capacity of the server, the requests can be balanced to multiple servers for processing, that is, as few servers as possible are used to process requests for the same resource within the rated processing capacity of the server.
[0059] However, it can be seen that among the different load balancing methods in the above examples, polling, least connections, weighted polling, and weighted least connections algorithms cannot meet the above two requirements well. These algorithms will only select from multiple servers in sequence or weighted order. The hash and consistent hash algorithms can meet the former requirement, but cannot achieve the latter requirement. The processing capacity of a single server will become a bottleneck. Although these two algorithms will assign requests for the same task to the same server, they will not consider the server load and will assign all requests to the same server. It can be seen from the above that the processing methods in the above examples will have a significant impact on processing efficiency and it is difficult to meet the current requirements for timeliness.
[0060] In view of this, a load balancing method is provided in some embodiments of the present disclosure. By comparing the number of requests for the same resource with the processing capacity of the server, corresponding first identifiers are set for these requests, and the first identifiers are spliced into the hash key value, so that when performing consistent hashing of the server, requests with the same first identifier can be allocated to the same server for processing according to the first identifier, so that multiple requests for the same resource can be allocated, and according to the relationship between the number of requests and the processing capacity, several first identifiers are set for as many processors as needed, so that requests for the same resource are concentrated on several processors to complete, thereby satisfying the requirement that requests for the same resource are processed on the same server as much as possible, and satisfying the problem of server load balancing, thereby improving the overall processing efficiency and user experience.
[0061] Figure 2 2 shows a flow chart of an exemplary method 200 provided by an embodiment of the present disclosure. Optionally, the method 200 can be used to allocate requests for the same resource and ensure that the load of the server is relatively balanced. The method 200 can be Figure 1 The terminal device 102 can also be implemented by Figure 1 The system 100 is implemented as follows.
[0062] like Figure 2 As shown, the method 200 may further include the following steps.
[0063] Step 202, in response to obtaining at least one task request for the same resource, determines the request quantity of the at least one task request, and determines a first identifier corresponding to the at least one task request based on a quantitative relationship between the request quantity and a set threshold; wherein the set threshold corresponds to the processing capacity of the server that processes the task request.
[0064] In this step, resources can correspond to applications, and all requests generated by an application can be regarded as requests generated by the same resource; they can also correspond to a specific website or a file (document, picture, video, etc.), and all requests generated from the website or file can be regarded as requests generated for the same resource.
[0065] After obtaining at least one task request, the number of these task requests for the same resource can be counted. The statistics can be based on all the requests for the task, that is, based on the accumulation of all historical task requests; statistics can also be performed only for a specific time period, such as 1 second or 10 seconds as a time period, and the number statistics are reset to zero at the beginning of each time period; statistics can also be performed only for requests at the same time (such as concurrent requests). After determining the number of requests, the quantitative relationship can be determined with the set threshold. The set threshold here corresponds to the processing capacity of the server. For example, if the processing capacity of the server is 100, the set threshold can be set to 100, or it can be set to 70, 50, etc. according to requirements. In more specific scenarios, in order to ensure smooth processing of the server, the set threshold is generally not set to be equivalent to the processing capacity. At the same time, in order to ensure that subsequent tasks can be added, a part of the processing capacity is generally reserved for the processor, and then the set threshold can be 2 / 3, 1 / 2, etc. of the server processing capacity. After that, the quantitative relationship between the number of requests and the set threshold can be simply understood as the size relationship and the ratio relationship between the number of requests and the set threshold. For example, if the threshold is set to 50, the number of requests between 1 and 50 corresponds to a quantitative relationship, the number of requests between 51 and 100 corresponds to a quantitative relationship, and the number of requests between 101 and 150 corresponds to another quantitative relationship. Finally, the first identifier of these task requests is determined by the quantitative relationship. For example, if the threshold is set to 50 and the number of requests is 110, the quantitative relationship can be expressed as 3, and then the first identifier can be selected between 1, 2, and 3. That is, after determining a value space of the first identifier, for any task request, the first identifier can be selected and set randomly or in a set manner within this value space.
[0066] In some examples, for the determination of the number of task requests, as described above, statistics can be performed only for the same time, that is, concurrent tasks are counted. In other examples, statistics can be performed for a set time period. In a time period, later task requests are superimposed on the number of previous task requests to determine the number of task requests for the same resource in this time period. In a more specific scenario, assuming that the time period is 10 seconds and the threshold is set to 50, 40 tasks are sent in succession in the first 5 seconds. According to the above settings, the first identifiers corresponding to these tasks will be set to the same, assuming 1. Then, at the 6th second, 20 task requests are sent in parallel. At this time, the previous 40 are superimposed, and the number of tasks is 60, which exceeds 1 times the set threshold but is less than 2 times the set threshold. Then the first identifier is set to two, 1 and 2. After that, for the later 20 tasks, the first identifier can be set in sequence, where the first identifiers of the first 10 are still 1, and the first identifiers of the last 10 are selected and set between 1 and 2. That is, in some examples, determining the number of requests for the at least one task request includes: dividing the time according to the set rules to generate multiple time periods, determining whether there are historical task requests for the same resource in the current time period; in response to the existence of historical task requests, determining a first number of the historical task requests, and accumulating the number of the at least one task request on the first number to determine the number of requests. Among them, dividing the time according to the set rules to generate multiple time periods means that the time can be divided according to the settings of the user or the system to generate multiple time periods. Then, the first number is the total number of historical task requests for the same resource in this time period. Afterwards, when crossing between time periods, the first number can be cleared, that is, after reaching a new time period, the first number is counted again.
[0067] Of course, in other examples, all task requests for the same resource can also be requested, that is, the total number of task requests for this task from beginning to end is accumulated, and the number of requests is determined accordingly. That is, in some examples, the determination of the number of requests for at least one task request includes: determining the second number of requests that have been obtained for the same resource, and accumulating the number of at least one task request based on the second number to generate the number of requests. The second number is the number of tasks that have been generated for the same resource in history.
[0068] Further, according to the setting method of the first identification of the task request described in the above example, for any task request of the same resource, it can be accumulated on the basis of the number of historical tasks. If only one task request comes at the same time, add 1 to the number of historical tasks as the number of the task request; if multiple tasks come at the same time, they can be superimposed on the basis of the number of historical tasks according to certain rules (such as slight differences in receiving time, differences in order when processing allocation, etc.), so as to generate the number corresponding to each task request. After each task request gets its own number, it can be compared with the set threshold. Assuming that the set threshold is 50, the first identification that the task request can correspond to can be determined according to which multiple range of the set threshold the number falls into. For example, if the number is 40 and falls within 1 times of the set threshold, the first identification is 1; if the number is 65 and falls between 1 and 2 times of the set threshold, the first identification is 1 or 2; if the number is 103 and falls between 2 and 3 times of the set threshold, the first identification is 1, 2 or 3. That is, in some examples, determining the first identifier corresponding to the at least one task request based on the quantitative relationship between the number of requests and the set threshold includes: assigning numbers to the at least one task request in sequence when accumulating the number of the at least one task request, and determining the corresponding relationship between the numbers and multiples of the set threshold; and setting the first identifier for the at least one task request based on the corresponding relationship.
[0069] In other examples, after determining the number of requests and setting the threshold, the quantitative relationship can be determined in other ways, and the first identifiers of these task requests can be set accordingly. Determining the quantitative relationship between the number of requests and the set threshold can be understood as determining the multiple relationship between the number of requests and the set threshold, where the set threshold is generally a set value, such as 50, and the number of requests is not fixed, so it can be determined how many times the number of requests is the set threshold to determine the value range of the first identifier. For example, if the number of requests is a multiple of the set threshold between 0 and 1, the value range of the first identifier is 1; if the number of requests is a multiple of the set threshold between 1 and 2, the value range of the first identifier is 1 and 2; and so on. Afterwards, after determining the value range of the first identifier, a value within the value range can be randomly selected as the corresponding first identifier for each current task request. That is, in some examples, determining the first identifier corresponding to the at least one task request based on the quantitative relationship between the number of requests and a set threshold includes: determining a value range of the first identifier based on a multiple relationship between the number of requests and a set threshold, and performing random selection within the value range to set the first identifier corresponding to any task request.
[0070] In other examples, the method of determining the quantitative relationship or the value range of the first identifier can also be to divide the number of requests by the set threshold, and the integer part of the result can correspond to the value range, 0 corresponds to the value range of 1, 1 corresponds to the value range of 2, and so on. Afterwards, at least one task request can be grouped according to the set threshold, and the grouping method can be random grouping or sequential grouping. For example, if there are 60 task requests and the set threshold is 50, the first 50 tasks are divided into 1 group and the last 10 tasks are divided into 2 groups; or 60 task requests are randomly allocated in two groups, etc. After the grouping is completed, the task requests of each group correspond to the same value of the first identifier, and the values of the first identifiers corresponding to different groups are generally different. That is, in some examples, the first identifier corresponding to the at least one task request is determined according to the quantitative relationship between the number of requests and the set threshold, including: determining the value range of the first identifier according to the quantitative relationship between the number of requests and the set threshold; grouping the at least one task request according to the set threshold so that the first identifier of each group of task requests corresponds to a value within the value range.
[0071] Step 204, determine a second identifier corresponding to the at least one task request, and generate a hash key value corresponding to the at least one task request based on the first identifier and the second identifier; wherein the second identifier corresponds to the identity information of the at least one task request.
[0072] In this step, the second identifier generally corresponds to the identity information of the same resource, such as ID (Identity document), etc. The ID of the same task is generally the same. This is also the reason why when the same resource is requested concurrently, the hash key generated by hashing and other methods is the same, and therefore concurrent requests will only be mapped to the same processor.
[0073] In this example, since at least one task request is from the same task, its second identifier is generally the same. However, for this example, the difference is that when generating the hash key value, not only the second identifier is used for generation, but also the first identifier needs to be combined for generation. The combination can be to connect or splice the first identifier on the basis of the second identifier, and use this whole as the hash key value; or it can be a method of merging the second identifier with the first identifier through a specific algorithm (for example, by encrypting the first identifier, etc.), and using the fused whole as the hash key value. That is, in some examples, the generation of the hash key value corresponding to the at least one task request according to the first identifier and the second identifier includes: splicing the first identifier and the second identifier using a splicing function to obtain the hash key value. Among them, the splicing function is the concat function, which is used to splice strings obtained from different places together.
[0074] Step 206: Allocate the server for the at least one task request according to the hash key value.
[0075] In this step, after the hash key value is determined, the task allocation can be performed according to the corresponding hash function. For example, these task requests can be routed through consistent hashing and these task requests can be allocated according to the corresponding hash key value.
[0076] In the allocation process, since the hash key value of the same first identifier is also the same, task requests with the same first identifier will be allocated to the same server for processing, and different first identifiers will be allocated to different servers. Since the value range of the first identifier is limited and is directly related to the number of task requests and the set threshold, the servers to which these task requests are allocated are also very limited and concentrated, and finally the allocation of task requests for the same resource is completed as concentrated as possible under the premise of ensuring load balancing. That is, in some examples, the server allocation of at least one task request according to the hash key value includes: using a set hash algorithm to perform consistent hash routing allocation on the hash key value, and allocating at least one task request with the same first identifier to the same server.
[0077] Afterwards, the hash function involved in this example can be applied to load balancing solutions applicable to various current hash functions, such as the message digest algorithm (MD5, Message-Digest Algorithm 5), the secure hash algorithm (SHA, Secure Hash Algorithm), and the cyclic redundancy check algorithm (CRC, Cyclic Redundancy Check).
[0078] In a more specific scenario, such as a scenario where multi-task requests are concurrent, QPS can be used to represent the request concurrency, and Limit represents the server's rated concurrent processing capacity or the set processing capacity threshold (corresponding to the set threshold). Then, the expected number of servers to be balanced to RS_Count = (QPS + Limit - 1) / Limit (rounded up to the nearest integer). Furthermore, the specific implementation process of the method 200 in this example can be as follows: (1) When a request (for the same resource) is received, the counter value is incremented by 1, and the accumulated counter value is denoted as Count. (2) Calculate the offset of the Hash key (hash key value): Offset = rand((Count + Limit - 1) / Limit), where rand is a random function with a value range of [1, (Count + Limit - 1) / Limit]. (3) Calculate the hash key value based on the count value: HashKey’ = concat(HashKey, Offset), where concat is a string concatenation function, HashKey is the key used by the ordinary HashKey routing (i.e., the original task request as the hash key value, generally corresponding to the identity ID, etc.), and HashKey' is the adjusted hash key value of this method. It should be noted that for the concatenation function, the concatenation symbol in the middle can be specifically set according to the specific scenario. For example, it can be set to "#", "&", "@", etc. (4) Calculate the final hash value and perform routing through consistent hashing: HashValue = hash(HashKey’), where hash is a hash function, and the specific implementation is not limited.
[0079] After that, after load balancing in the above manner, the schematic diagram of the total QPS of the final task requests and the QPS of a single server is as Figure 3 shown, where the abscissa is the total QPS and the ordinate is the QPS of a single server. It can be seen that when QPS < Limit, all requests with the same HashKey will be routed to the same server. When Limit < QPS < 2 * Limit, all requests with the same HashKey will be randomly routed to two servers. When 2 * Limit < QPS < 3 * Limit, all requests with the same HashKey will be randomly routed to three servers. And so on.
[0080] It can be seen from the above examples that the load balancing method provided by the embodiments of the present disclosure compares the number of requests for the same resource with the processing capacity of the server, thereby setting corresponding first identifiers for these requests, and splicing the first identifiers into the hash key value, so that when performing consistent hashing allocation of servers, requests with the same first identifier can be allocated to the same server for processing according to the first identifier, so that multiple requests for the same resource can be allocated, and according to the relationship between the number of requests and the processing capacity, several first identifiers are set as many processors as needed, so that requests for the same resource can be concentrated on several processors to complete, thereby satisfying the requirement that requests for the same resource should be processed on the same server as much as possible, and satisfying the problem of server load balancing, thereby improving the overall processing efficiency and user experience.
[0081] It should be noted that the method of the embodiment of the present disclosure can be performed by a single device, such as a computer or a server. The method of the embodiment of the present disclosure can also be applied in a distributed scenario and completed by multiple devices cooperating with each other. In the case of such a distributed scenario, one of the multiple devices can only perform one or more steps in the method of the embodiment of the present disclosure, and the multiple devices will interact with each other to complete the described method.
[0082] It should be noted that the above is a description of a specific embodiment of the present disclosure. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the above embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0083] Based on the same technical concept, corresponding to any of the above-mentioned embodiment methods, the present disclosure also provides a load balancing device. Figure 4 FIG. 4 is a schematic diagram of an exemplary device 400 provided by an embodiment of the present disclosure. Figure 4 As shown, the device 400 can be used to implement the method 200, and can further include the following modules.
[0084] The first module 410 is used to determine the request quantity of at least one task request in response to obtaining at least one task request for the same resource, and determine the first identifier corresponding to the at least one task request based on the quantitative relationship between the request quantity and a set threshold; wherein the set threshold corresponds to the processing capacity of the server that processes the task request.
[0085] The second module 420 is used to determine a second identifier corresponding to the at least one task request, and generate a hash key value corresponding to the at least one task request based on the first identifier and the second identifier; wherein the second identifier corresponds to the identity information of the at least one task request.
[0086] The third module 430 is used to allocate the server of the at least one task request according to the hash key value.
[0087] In some exemplary embodiments, the first module 410 is further configured to:
[0088] Divide the time into multiple time periods according to the set rules, and determine whether there is a historical task request for the same resource in the current time period;
[0089] In response to the existence of historical task requests, a first number of the historical task requests is determined, and the number of the at least one task request is accumulated on the first number to determine the request number.
[0090] In some exemplary embodiments, the first module 410 is further configured to:
[0091] In response to a crossing between time periods, the first number is cleared.
[0092] In some exemplary embodiments, the first module 410 is further configured to:
[0093] A second number of requests that have been acquired for the same resource is determined, and the number of the at least one task request is accumulated based on the second number to generate the request number.
[0094] In some exemplary embodiments, the first module 410 is further configured to:
[0095] When accumulating the number of the at least one task request, assigning a number to the at least one task request in sequence, and determining a corresponding relationship between the number and a multiple of the set threshold;
[0096] The first identifier is set for the at least one task request according to the corresponding relationship.
[0097] In some exemplary embodiments, the first module 410 is further configured to:
[0098] The value range of the first identifier is determined according to the multiple relationship between the number of requests and the set threshold, and a random selection is performed within the value range to set the first identifier corresponding to any task request.
[0099] In some exemplary embodiments, the first module 410 is further configured to:
[0100] Determining a value range of the first identifier according to a quantitative relationship between the number of requests and a set threshold;
[0101] The at least one task request is grouped according to a set threshold value, so that the first identifier of each group of task requests corresponds to a value within the value range.
[0102] In some exemplary embodiments, the second module 420 is further configured to:
[0103] The first identifier and the second identifier are concatenated using a concatenation function to obtain the hash key value.
[0104] In some exemplary embodiments, the third module 430 is further configured to:
[0105] The hash key value is subjected to consistent hash routing allocation using a set hash algorithm, and at least one task request with the same first identifier is allocated to the same server.
[0106] In some exemplary embodiments, the set hash algorithm includes at least one of a message digest algorithm, a secure hash algorithm, and a cyclic redundancy check algorithm.
[0107] For the convenience of description, the above device is described by dividing it into various modules according to its functions. Of course, when implementing the present disclosure, the functions of each module can be implemented in the same or multiple software and / or hardware.
[0108] The device of the above embodiment is used to implement the corresponding method 200 in any of the above embodiments, and has the beneficial effects of the corresponding method 200 embodiment, which will not be described in detail here.
[0109] Based on the same technical concept, corresponding to any of the above-mentioned embodiments and methods, the embodiment of the present disclosure further provides a computer device for implementing the above-mentioned method 200. Figure 5 FIG. 5 is a schematic diagram showing the hardware structure of an exemplary computer device 500 provided in an embodiment of the present disclosure. The computer device 500 can be used to implement Figure 1 In some scenarios, the computer device 500 can also be used to implement Figure 1 Server 104 and database server 106.
[0110] like Figure 5 As shown, the computer device 500 may include: a processor 502, a memory 504, a network module 506, a peripheral interface 508 and a bus 510. The processor 502, the memory 504, the network module 506 and the peripheral interface 508 are connected to each other in communication within the computer device 500 through the bus 510.
[0111] Processor 502 may be a central processing unit (CPU), an image processor, a neural network processor (NPU), a microcontroller (MCU), a programmable logic device, a digital signal processor (DSP), an application specific integrated circuit (ASIC), or one or more integrated circuits. Processor 502 may be used to perform functions related to the technology described in this disclosure. In some embodiments, processor 502 may also include multiple processors integrated into a single logical component. For example, Figure 5 As shown, processor 502 may include a plurality of processors 502a, 502b, and 502c.
[0112] The memory 504 may be configured to store data (eg, instructions, computer code, etc.). Figure 5 As shown, the data stored in the memory 504 may include program instructions (e.g., program instructions for implementing the method 200 of the embodiment of the present disclosure) and data to be processed (e.g., the memory may store configuration files of other modules, etc.). The processor 502 may also access the program instructions and data stored in the memory 504, and execute the program instructions to operate on the data to be processed. The memory 504 may include a volatile storage device or a non-volatile storage device. In some embodiments, the memory 504 may include a random access memory (RAM), a read-only memory (ROM), an optical disk, a magnetic disk, a hard disk, a solid-state drive (SSD), a flash memory, a memory stick, etc.
[0113] The network interface 506 can be configured to provide the computer device 500 with communication with other external devices via a network. The network can be any wired or wireless network capable of transmitting and receiving data. For example, the network can be a wired network, a local wireless network (e.g., Bluetooth, WiFi, near field communication (NFC)), a cellular network, the Internet, or a combination thereof. It is understood that the type of network is not limited to the above specific examples.
[0114] The peripheral interface 508 can be configured to connect the computer device 500 to one or more peripheral devices to achieve information input and output. For example, the peripheral devices can include input devices such as a keyboard, a mouse, a touch pad, a touch screen, a microphone, and various sensors, and output devices such as a display, a speaker, a vibrator, and an indicator light.
[0115] The bus 510 may be configured to transmit information between various components of the computer device 500 (e.g., the processor 502, the memory 504, the network interface 506, and the peripheral interface 508), such as an internal bus (e.g., a processor-memory bus), an external bus (USB port, PCI-E bus), etc.
[0116] It should be noted that, although the architecture of the above-mentioned computer device 500 only shows the processor 502, the memory 504, the network interface 506, the peripheral interface 508 and the bus 510, in the specific implementation process, the architecture of the computer device 500 may also include other components necessary for normal operation. In addition, it can be understood by those skilled in the art that the architecture of the above-mentioned computer device 500 may also only include the components necessary for implementing the embodiments of the present disclosure, and does not necessarily include all the components shown in the figure.
[0117] The computer device 500 of the above embodiment is used to implement the method 200 described in any of the above embodiments, and has the beneficial effects of the corresponding method embodiments, which will not be described in detail here.
[0118] Based on the same technical concept, corresponding to any of the above-mentioned embodiments, the present disclosure also provides a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute method 200 described in any of the above embodiments.
[0119] The computer-readable medium of this embodiment includes permanent and non-permanent, removable and non-removable media, and information storage can be achieved by any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, read-only compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, tape disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device.
[0120] The computer instructions stored in the storage medium of the above embodiment are used to enable the computer to execute the method 200 described in any of the above embodiments, and have the beneficial effects of the corresponding method embodiments, which will not be repeated here.
[0121] Based on the same technical concept, corresponding to the method 200 described in any of the above embodiments, the present disclosure further provides a computer program product, including computer program instructions, which, when executed on a computer, enable the computer to execute the method 200 described in any of the above embodiments. In some embodiments, the computer program instructions may be executed by one or more processors of a computer so that the computer and / or the processor execute the method 200. Corresponding to the execution subject corresponding to each step in each embodiment of the method 200, the processor that executes the corresponding step may belong to the corresponding execution subject.
[0122] The computer program product of the above embodiment is used to enable the computer and / or the processor to execute the method 200 described in any of the above embodiments, and has the beneficial effects of the corresponding method embodiments, which will not be described in detail here.
[0123] Those skilled in the art should understand that the discussion of any of the above embodiments is merely illustrative and is not intended to imply that the scope of the present disclosure (including the claims) is limited to these examples. Based on the concept of the present disclosure, the technical features in the above embodiments or different embodiments may be combined, the steps may be implemented in any order, and there are many other variations of the different aspects of the embodiments of the present disclosure as described above, which are not provided in detail for the sake of simplicity.
[0124] In addition, to simplify the description and discussion, and in order not to make the embodiments of the present disclosure difficult to understand, the known power / ground connections to the integrated circuit (IC) chips and other components may or may not be shown in the provided figures. In addition, the device can be shown in the form of a block diagram to avoid making the embodiments of the present disclosure difficult to understand, and this also takes into account the fact that the details of the implementation of these block diagram devices are highly dependent on the platform on which the embodiments of the present disclosure will be implemented (that is, these details should be fully within the scope of understanding of those skilled in the art). Where specific details (e.g., circuits) are set forth to describe exemplary embodiments of the present disclosure, it is apparent to those skilled in the art that the embodiments of the present disclosure can be implemented without these specific details or with changes in these specific details. Therefore, these descriptions should be considered illustrative rather than restrictive.
[0125] Although the present disclosure has been described in conjunction with specific embodiments of the present disclosure, many replacements, modifications and variations of these embodiments will be apparent to those skilled in the art from the foregoing description. For example, other memory architectures (e.g., dynamic RAM (DRAM)) may use the embodiments discussed.
[0126] The embodiments of the present disclosure are intended to cover all such substitutions, modifications and variations that fall within the broad scope of the appended claims. Therefore, any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the embodiments of the present disclosure should be included in the scope of protection of the present disclosure.
Claims
1. A load balancing method, comprising: In response to obtaining at least one task request for the same resource, determining the request quantity of the at least one task request, and determining a first identifier corresponding to the at least one task request according to a quantitative relationship between the request quantity and a set threshold; wherein the set threshold corresponds to a processing capacity of a server that processes the task request; Determine a second identifier corresponding to the at least one task request, and generate a hash key value corresponding to the at least one task request according to the first identifier and the second identifier; wherein the second identifier corresponds to the identity information of the at least one task request; The server allocation of the at least one task request is performed according to the hash key value.
2. The method according to claim 1, wherein: The determining the request quantity of the at least one task request includes: Divide the time into multiple time periods according to the set rules, and determine whether there is a historical task request for the same resource in the current time period; In response to the existence of historical task requests, a first number of the historical task requests is determined, and the number of the at least one task request is accumulated on the first number to determine the request number.
3. The method according to claim 2, wherein: After the time is divided according to the set rule to generate multiple time periods, the method further includes: In response to a crossing between time periods, the first number is cleared.
4. The method according to claim 1, wherein: The determining the request quantity of the at least one task request includes: A second number of requests that have been acquired for the same resource is determined, and the number of the at least one task request is accumulated based on the second number to generate the request number.
5. The method according to claim 2 or 4, wherein: The determining, according to a quantitative relationship between the number of requests and a set threshold, a first identifier corresponding to the at least one task request includes: When accumulating the number of the at least one task request, assigning a number to the at least one task request in sequence, and determining a corresponding relationship between the number and a multiple of the set threshold; The first identifier is set for the at least one task request according to the corresponding relationship.
6. The method according to claim 1, wherein: The determining, according to a quantitative relationship between the number of requests and a set threshold, a first identifier corresponding to the at least one task request includes: The value range of the first identifier is determined according to the multiple relationship between the number of requests and the set threshold, and a random selection is performed within the value range to set the first identifier corresponding to any task request.
7. The method according to claim 1, wherein: The determining, according to a quantitative relationship between the number of requests and a set threshold, a first identifier corresponding to the at least one task request includes: Determining a value range of the first identifier according to a quantitative relationship between the number of requests and a set threshold; The at least one task request is grouped according to a set threshold value, so that the first identifier of each group of task requests corresponds to a value within the value range.
8. The method according to claim 1, wherein: The step of generating a hash key value corresponding to the at least one task request according to the first identifier and the second identifier includes: The first identifier and the second identifier are concatenated using a concatenation function to obtain the hash key value.
9. The method according to claim 1, wherein: The server allocation of the at least one task request according to the hash key value includes: The hash key value is subjected to consistent hash routing allocation using a set hash algorithm, and at least one task request with the same first identifier is allocated to the same server.
10. The method according to claim 1, wherein: The set hash algorithm includes at least one of an information digest algorithm, a secure hash algorithm, and a cyclic redundancy check algorithm.
11. A load balancing device, comprising: A first module is used to determine the request quantity of the at least one task request in response to obtaining at least one task request for the same resource, and determine a first identifier corresponding to the at least one task request according to a quantitative relationship between the request quantity and a set threshold; wherein the set threshold corresponds to a processing capacity of a server that processes the task request; A second module is used to determine a second identifier corresponding to the at least one task request, and generate a hash key value corresponding to the at least one task request according to the first identifier and the second identifier; wherein the second identifier corresponds to the identity information of the at least one task request; The third module is used to allocate the server of the at least one task request according to the hash key value.
12. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method according to any one of claims 1 to 10 when executing the program.
13. A non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to cause a computer to implement the method according to any one of claims 1 to 10.