Method and device for processing business request
Through the combination of token bucket algorithm and redis cluster, the processing performance of each business request is precisely controlled, which solves the problem of rough handling of live broadcast accidents in the existing technology, avoids the avalanche effect, and ensures the stability and functional availability of the system. .
Patent Information
- Application Number
- CN202210365512.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-07
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2042-04-07
AI Technical Summary
The existing live broadcast accident handling methods are rough, causing the system to easily experience an avalanche effect under high concurrency conditions, affecting system functions.
The token bucket algorithm is used to calculate the query rate per second QPS of each business request separately, and the number of token buckets is set according to the QPS, the request processing is controlled, the token bucket processing capacity is obtained in real time for supplementation, and statistics and priority processing are performed in conjunction with the redis cluster. , to avoid the avalanche effect.
Finely control the processing performance of each business request, effectively protect the lower-layer services of the live broadcast system, avoid avalanche effects, and ensure the stability and availability of system functions.
Smart Images

Figure CN114745562B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of live broadcast accident processing, and in particular to a method, device, electronic device and computer-readable storage medium for processing a business request. Background Art
[0002] During the evening peak of live broadcast, the system often suffers from excessive pressure and concurrency, and the underlying system or some bypass dependent systems cannot withstand the avalanche effect. Therefore, redis is used at the entry gateway of the system to increase the concurrency statistics. The concurrency count accumulates by +1 for each request received. When the request is processed, the concurrency count is reduced by -1 in the post-middleware. If a request is not processed within 5S, it will be invalid by default to prevent the request from being stuck. Then, the fuse rules are set for the request routing to determine the concurrency that each route can withstand. When the statistical concurrency count reaches the threshold set for the request, the request will no longer be received and forwarded, and will be intercepted and returned directly at the entry gateway to reduce the concurrency pressure of the system and ensure the processing of core functions.
[0003] It can be seen that the relevant technology has the following deficiencies: 1: The statistical concurrency is global, which will fuse some business functions that originally had relatively high performance; 2: The statistical pressure of a single redis machine is too high.
[0004] Regarding the problem that the prevention and handling of live broadcast accidents in related technologies are relatively rough, no effective solution has been proposed yet. Summary of the invention
[0005] The embodiments of the present invention provide a method, device, electronic device and computer-readable storage medium for processing a business request, so as to at least solve the technical problem that the prevention and processing methods of live broadcast accidents in related technologies are relatively rough.
[0006] According to one aspect of an embodiment of the present invention, a method for processing business requests is provided, including: respectively calculating the query rate per second (QPS) corresponding to each business request; according to the QPS corresponding to each business request, using a token bucket algorithm to set the number of token buckets for each business request, wherein the token bucket is used to control the business request; after receiving the business request, setting the business request to obtain a token from the token bucket; and processing the business request if the business request obtains the token.
[0007] Optionally, the method also includes: after processing the business request, obtaining the business request processing capacity of the token bucket in real time, wherein the business request processing capacity includes that the number of business requests processed by the token bucket per second is greater than a first preset threshold; according to the business request processing capacity of the token bucket, using preset rules to supplement the number of token buckets, wherein the preset rules include millisecond-level supplementation rules.
[0008] Optionally, a request threshold of each service request is obtained; when the number of the service requests is greater than the request threshold, the service requests are individually controlled on a control panel.
[0009] Optionally, the method further includes: at the inlet gateway of the system, using a redis cluster to count the business requests.
[0010] Optionally, the method further includes: determining whether the number of remaining tokens in the token bucket is lower than a second preset threshold; if the determination result is yes, obtaining the priority of the service request; and processing the service request according to the priority of the service request.
[0011] According to another aspect of an embodiment of the present invention, a device for processing business requests is also provided, including: a calculation module, used to calculate the query rate per second (QPS) corresponding to each business request; a first setting module, used to set the number of token buckets for each business request according to the QPS corresponding to each business request using a token bucket algorithm, wherein the token bucket is used to control the business request; a second setting module, used to set the business request to obtain a token from the token bucket after receiving the business request; and a processing module, used to process the business request when the business request obtains the token.
[0012] Optionally, the device also includes: an acquisition module, used to obtain the business request processing capability of the token bucket in real time after processing the business request, wherein the business request processing capability includes the number of business requests processed by the token bucket per second being greater than a first preset threshold; a supplement module, used to supplement the number of token buckets using preset rules according to the business request processing capability of the token bucket, wherein the preset rules include millisecond-level supplementation rules.
[0013] Optionally, the device further includes: the device further includes: a statistical module, used to use a redis cluster to count the business requests at the inlet gateway of the system.
[0014] Optionally, the device is further used to obtain a request threshold of each service request; when the number of requests for the service requests is greater than the request threshold, adjust the request threshold on the control panel.
[0015] The device is also used to determine whether the number of remaining tokens in the token bucket is lower than a second preset threshold; if the determination result is yes, obtain the priority of the service request; and process the service request according to the priority of the service request.
[0016] An embodiment of the present invention provides an electronic device, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the steps of any one of the above methods.
[0017] An embodiment of the present invention provides a computer-readable storage medium, on which instructions are stored, and when the instructions are executed by a processor, the steps of any of the above methods are implemented.
[0018] In the embodiment of the present invention, the query rate per second (QPS) corresponding to each business request is calculated respectively; according to the QPS corresponding to each business request, the number of token buckets is set for each business request using a token bucket algorithm, wherein the token bucket is used to control the business request; after receiving the business request, the business request is set to obtain a token from the token bucket; when the business request obtains the token, the business request is processed. In other words, the embodiment of the present invention abandons the original counting method and uses a token bucket to implement it, finely controlling the processing performance of each business request, thereby solving the problem of relatively rough prevention and processing methods of live broadcast accidents in related technologies, and achieving the technical effect of effectively protecting the lower-level services and public resources of the live broadcast system, thereby avoiding an avalanche effect that affects the function of the entire system. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0020] Figure 1 A flowchart of a method for processing a service request provided by an embodiment of the present invention;
[0021] Figure 2 A schematic diagram of a service request processing device provided by an embodiment of the present invention (I);
[0022] Figure 3 A schematic diagram of a service request processing device provided by an embodiment of the present invention (II);
[0023] Figure 4 A schematic diagram of a service request processing device provided by an embodiment of the present invention (III). DETAILED DESCRIPTION
[0024] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0025] It should be noted that the terms "first", "second" and the like in the specification, claims and drawings of the present invention are used to distinguish different objects rather than to limit a specific order.
[0026] The embodiment of the present invention provides a method for processing a service request. Figure 1 A flowchart of a method for processing a service request provided by an embodiment of the present invention.
[0027] Optionally, application scenarios of the embodiments of the present invention include but are not limited to:
[0028] Scenario 1: Users query product details and make purchases through the live broadcast platform. Due to the large number of purchasers, the system often gets stuck due to excessive pressure and concurrency, and the underlying system or some bypass dependent systems cannot bear it. In this case, the method provided by the embodiment of the present invention can be used to calculate the query rate per second (QPS) corresponding to each business request; according to the QPS corresponding to each business request, the number of token buckets is set for each business request using the token bucket algorithm, wherein the token bucket is used to control the business request; after receiving the business request, the business request is set to obtain a token from the token bucket; when the business request obtains the token, the business request is processed. In other words, the embodiment of the present invention abandons the original counting method and uses a token bucket to implement fine-grained control of the processing performance of each business request.
[0029] Scenario 2: A large number of users access a small program at the same time. Due to the large number of visitors, the system often gets stuck due to excessive pressure and concurrency, and the underlying system or some bypass dependent systems cannot bear it. In this case, the method provided by the embodiment of the present invention can be used to calculate the query rate per second (QPS) corresponding to each business request; according to the QPS corresponding to each business request, the number of token buckets is set for each business request using the token bucket algorithm, wherein the token bucket is used to control the business request; after receiving the business request, the business request is set to obtain a token from the token bucket; when the business request obtains the token, the business request is processed. In other words, the embodiment of the present invention abandons the original counting method and uses a token bucket to implement fine-grained control of the processing performance of each business request.
[0030] like Figure 1 As shown, the method for processing a service request provided in an embodiment of the present application includes the following steps:
[0031] S102, respectively calculating the query rate per second (QPS) corresponding to each service request;
[0032] QPS is a measure of the amount of traffic a specific query server handles within a specified time. On the Internet, the query rate per second is often used to measure the performance of the domain name system server machine, corresponding to fetches / sec, that is, the number of response requests per second, that is, the maximum throughput capacity, and the calculation relationship is QPS = concurrency / average response time.
[0033] S104, according to the QPS corresponding to each service request, using a token bucket algorithm to set the number of token buckets for each service request, wherein the token bucket is used to control the service request;
[0034] It should be noted that when transmitting data in the network, in order to prevent network congestion, it is necessary to limit the flow of traffic out of the network so that the traffic is sent out at a relatively uniform speed. The token bucket algorithm implements this function, which can control the number of data sent to the network and allow burst data to be sent. The token bucket algorithm is the most commonly used algorithm in network traffic shaping (Traffic Shaping) and rate limiting (Rate Limiting). Typically, the token bucket algorithm is used to control the number of data sent to the network and allow burst data to be sent. A fixed-size token bucket can generate tokens at a constant rate. If the token is not consumed, or the rate of consumption is lower than the rate of generation, the token will continue to increase until the bucket is full, and the tokens generated later will overflow from the bucket. Finally, the maximum number of tokens that can be stored in the bucket will never exceed the size of the bucket. Data packets transmitted to the token bucket need to consume tokens. Data packets of different sizes consume different numbers of tokens. The token bucket control mechanism indicates when traffic can be sent based on whether there are tokens in the token bucket. Each token in the token bucket represents a byte. If there are tokens in the token bucket, traffic is allowed to be sent; if there are no tokens in the token bucket, traffic is not allowed to be sent. Therefore, if the burst threshold is configured properly and there are enough tokens in the token bucket, traffic can be sent at the peak rate. The basic process of the token bucket algorithm is as follows: If the average sending rate configured by the user is r, a token is added to the bucket every 1 / r seconds; assume that the bucket can store and send a maximum of b tokens. If the token bucket is full when the token arrives, the token will be discarded; when a data packet of n bytes arrives, n tokens are deleted from the token bucket and the data packet is sent to the network; if there are less than n tokens in the token bucket, the token is not deleted and the data packet is considered to be outside the flow limit; the algorithm allows bursts of up to b bytes, but from the long-term operation results, the rate of data packets is limited to a constant r. Data packets outside the flow limit can be handled in different ways: they can be discarded; they can be placed in a queue to be transmitted when enough tokens are accumulated in the token bucket; they can continue to be sent, but they need to be specially marked, and these specially marked packets will be discarded when the network is overloaded.
[0035] S106, after receiving the service request, setting the service request to obtain a token from the token bucket;
[0036] S108: When the service request obtains the token, the service request is processed.
[0037] Optionally, in this embodiment, the processing of the service request includes but is not limited to: forwarding the service request and rejecting the service request.
[0038] Through the above steps S102~S108, the query rate per second QPS corresponding to each business request is calculated respectively; according to the QPS corresponding to each business request, the number of token buckets is set for each business request using the token bucket algorithm, wherein the token bucket is used to control the business request; after receiving the business request, the business request is set to obtain the token from the token bucket; when the business request obtains the token, the business request is processed. In other words, the embodiment of the present invention abandons the original counting method and uses the token bucket to implement it, and finely controls the processing performance of each business request, thereby solving the problem of relatively rough prevention and processing methods of live broadcast accidents in related technologies, and achieving the technical effect of effectively protecting the lower-level services and public resources of the live broadcast system and avoiding the avalanche effect that affects the function of the entire system.
[0039] In an optional embodiment, the above method further includes:
[0040] S11, after processing the service request, obtaining the service request processing capacity of the token bucket in real time, wherein the service request processing capacity includes that the number of service requests processed by the token bucket per second is greater than a first preset threshold;
[0041] S12, replenishing the number of the token bucket using preset rules according to the service request processing capability of the token bucket, wherein the preset rules include millisecond-level replenishment rules.
[0042] For example, the token bucket will be replenished on a millisecond basis based on the number of requests that can be processed per second. This approach can also ensure that a certain function will not remain unprocessed and may be processed in the next second.
[0043] Optionally, the method further comprises:
[0044] S21, obtaining a request threshold for each service request;
[0045] S22: When the number of requests for the service request is greater than the request threshold, the service request is controlled separately on the control panel.
[0046] For example, assuming that the request threshold of business request 1 is 5000 and the request threshold of business request 2 is 10000, when the number of requests for business request 1 is 6000 and the number of requests for business request 2 is 7000, since the number of requests for business request 1 (6000) is greater than the request threshold of business request 1 (5000) and the number of requests for business request 2 (7000) is less than the request threshold of business request 2 (10000), it is only necessary to control business request 1 separately on the control panel, while business request 2 maintains the original control method.
[0047] For another example, assuming that the request threshold of business request 1 is 5,000 and the request threshold of business request 2 is 10,000, when the number of requests for business request 1 is 6,000 and the number of requests for business request 2 is 12,000, since the number of requests for business request 1 (6,000) is greater than the request threshold of 5,000 for business request 1 and the number of requests for business request 2 (12,000) is greater than the request threshold of 10,000 for business request 2, it is necessary to control business request 1 and business request 2 on the control panel.
[0048] For another example, assuming that the request threshold of business request 1 is 5000 and the request threshold of business request 2 is 10000, when the number of requests for business request 1 is 4000 and the number of requests for business request 2 is 7000, since the number of requests for business request 1 (4000) is less than the request threshold of 5000 for business request 1 and the number of requests for business request 2 (7000) is less than the request threshold of 10000 for business request 2, business request 1 and business request 2 maintain the original control mode.
[0049] The above S21-22 can be used as an emergency treatment measure. When a service pressure changes suddenly and unexpectedly, a business request can be controlled separately on the control panel to prevent an avalanche phenomenon.
[0050] Optionally, the above method further includes:
[0051] S31, at the inlet gateway of the system, using a redis cluster to count the business requests.
[0052] Among them, the main principle of single-machine redis statistics is that the concurrent number of each request received is accumulated by +1. When the request is processed, the concurrent number is reduced by 1 in the post-middleware. If a request is not processed within 5S, it will be invalid by default to prevent the request from being stuck. Then, the fuse rules are set for the request routing to determine the concurrency that each route can withstand. When the statistical concurrency reaches the threshold set for the request, the request will no longer be received and forwarded, and it will be intercepted and returned directly at the entry gateway to reduce the concurrency pressure of the system and ensure the processing of core functions.
[0053] Redis cluster adopts the concept of decentralization, and there is no central node. For the client, the entire cluster can be regarded as a whole, and any node can be connected to operate, just like operating a single Redis instance, without any proxy middleware. When the key operated by the client is not assigned to the node, Redis will return a redirect instruction to point to the correct node. Redis also has a built-in high availability mechanism, supporting N master nodes. Each master node can mount multiple slave nodes. When the master node hangs up, the cluster will promote one of its slave nodes as the new master node.
[0054] In the redis cluster, all redis nodes are interconnected, and the nodes use binary protocols to optimize transmission speed and bandwidth. When a node fails, it is considered to be failed only when more than half of the nodes in the cluster detect failure. Unlike the tomcat cluster, which requires the use of a reverse proxy server, any node in the redis cluster can be directly connected to the Java client. The data distribution on the redis cluster uses hash slots (HASH SLOT). The redis cluster has 16384 hash slots built in. When data needs to be stored, redis will first use the CRC16 algorithm to calculate the key, and take the remainder of the calculated result to 16384, so that each key will correspond to a hash slot with a value between 0 and 16383. Redis will store the data on the corresponding redis node based on this remainder. Developers can adjust the distribution range of hash slots on each redis instance according to the performance of each redis instance.
[0055] It is mainly divided into master-slave mode, Sentinel mode, and Cluster mode. Among them, regarding the master-slave mode: when the slave is started, it actively sends a SYNC command to the master. After receiving the SYNC command, the master saves the snapshot (RDB persistence) and caches the commands for saving the snapshot in the background, and then sends the saved snapshot file and cached commands to the slave. After receiving the snapshot file and command, the slave loads the snapshot file and cached execution command. After the replication is initialized, each command received by the master will be sent synchronously to the slave to ensure the consistency of the master-slave data.
[0056] About Sentinel mode: Each sentinel sends a ping command to the master, slave and other sentinel instances it knows about once per second. If the time from the last valid reply of an instance to the ping command exceeds the value specified by the own-after-milliseconds option, the instance will be marked as subjectively offline by sentinel. If a master is marked as subjectively offline, all sentinels that are monitoring this master must confirm once per second that the master has indeed entered the subjective offline state. When a sufficient number of sentinels (greater than or equal to the value specified in the configuration file) confirm that the master has indeed entered the subjective offline state within the specified range, the master will be marked as objectively offline. In general, each sentinel sends an INFO command to all masters and slaves it knows about once every 10 seconds. When a master is marked as objectively offline by sentinel, the frequency of sending INFO commands to all slaves of the offline master is changed from once every 10 seconds to once every second. If there are not enough sentinels to confirm that the master is offline, the objective offline status of the master will be removed. If the master returns a valid recovery to the sentinel's PING command, the subjective offline status of the master will be removed. When using sentinel mode, the client should not connect to redis directly, but connect to sentinel's ip and port, and sentinel will provide a specific redis implementation that can provide services.
[0057] About Cluster mode: Cluster can be said to be a combination of sentinel and master-slave mode. Cluster can realize master-slave and master re-election functions, so if two replicas and three shards are configured, six redis instances are required. Because redis data is distributed to different machines in cluster according to certain rules, when the amount of data is too large, new machines can be added for expansion.
[0058] Optionally, in this embodiment, the number of redis included in the redis cluster can be determined according to the business pressure. When the redis cluster performs statistics, the business requests can be evenly distributed or distributed according to a certain ratio.
[0059] For example, 10 redis are used for statistics at the entrance gateway of the system. When performing statistics, the 10 redis can divide the business requests into 10 parts, and the corresponding redis performs statistics respectively.
[0060] For another example, two redis are used for statistics at the entrance gateway of the system. When performing statistics, the two redis allocate 80% of the business requests to redis1 for statistics, and allocate 20% of the business requests to redis2 for statistics.
[0061] Through the above step S31, the single-machine redis is changed to a clustered redis, which disperses the redis pressure and improves the overall processing capacity of the system.
[0062] In an optional embodiment, the above method further includes:
[0063] S41, determining whether the number of remaining tokens in the token bucket is lower than a second preset threshold;
[0064] S42, if the judgment result is yes, obtaining the priority of the service request;
[0065] S43: Process the service request according to the priority of the service request.
[0066] Optionally, the value of the second preset threshold can be determined based on the speed at which the system processes business requests. When the speed at which business requests are processed is required to be high, the value of the second preset threshold is set to be relatively large; when the speed at which business requests are processed is required to be relatively low, the value of the second preset threshold is set to be relatively small.
[0067] For example, assuming that the number of remaining tokens in the token bucket is 1000, and the above-mentioned second preset threshold is 1500, in this case, it is necessary to obtain the priority of the business request. Assuming that the priority of the business request is high priority, then the business request will be processed first to ensure that the high-priority business request is processed in time. Assuming that the priority of the business request is low priority, then the business request will not be processed to ensure that the high-priority business request is processed in time.
[0068] Through the above S41~S43, all functions can be available to the greatest extent, and the core functions can be guaranteed to be available in the worst case.
[0069] The following takes live broadcast as an example to illustrate this embodiment.
[0070] During the evening peak of live broadcast, the number of queries that can be processed per second (QPS) is stress-tested for each business request, and the token bucket algorithm is used to set the initial number of requests that can be processed for each business request, so that request control can be performed based on the performance of a certain business processing. According to the stress test performance results of each business request, the initial number of token buckets is set for each business request when the service is started. Each business request needs to obtain a token from the token bucket. Only after the token is obtained can the business request be received and forwarded. At the same time, the token bucket will be replenished at the millisecond level according to the number of business requests that can be processed per second. This method can also ensure that a certain function will not be unprocessed all the time, and may be processed in the next second. At the same time, the threshold rule setting for each business request can be dynamically adjusted on the control panel, which can be used for emergency processing. When the pressure on a certain service changes suddenly and unpredictably, a certain request can be controlled individually on the control panel to prevent an avalanche.
[0071] Through this example, in order to protect the lower-level services and public resources from the avalanche effect that affects the functions of this system, all functions are available to the greatest extent possible based on this principle. In the worst case, the core functions can also be guaranteed to be available. Compared with the existing technology, the more refined setting rules and the individual control of the number of request processing are also implemented using redis technology, and the cluster version is used to distribute the pressure. That is, the original counting method is abandoned and the token bucket is used to implement it, and the processing performance of each request is finely controlled. At the same time, the single machine is changed to a clustered redis, which disperses the redis pressure and improves the overall processing capacity of the system.
[0072] The embodiment of the present invention also provides a device for processing a service request. Figure 2 A schematic diagram of a device for processing a service request provided by an embodiment of the present invention.
[0073] Scenario 1: Users query product details and make purchases through the live broadcast platform. Due to the large number of purchasers, the system often gets stuck due to excessive pressure and concurrency, and the underlying system or some bypass dependent systems cannot bear it. In this case, the method provided by the embodiment of the present invention can be used to calculate the query rate per second (QPS) corresponding to each business request; according to the QPS corresponding to each business request, the number of token buckets is set for each business request using the token bucket algorithm, wherein the token bucket is used to control the business request; after receiving the business request, the business request is set to obtain a token from the token bucket; when the business request obtains the token, the business request is processed. In other words, the embodiment of the present invention abandons the original counting method and uses a token bucket to implement fine-grained control of the processing performance of each business request.
[0074] Scenario 2: A large number of users access a small program at the same time. Due to the large number of visitors, the system often gets stuck due to excessive pressure and concurrency, and the underlying system or some bypass dependent systems cannot bear it. In this case, the method provided by the embodiment of the present invention can be used to calculate the query rate per second (QPS) corresponding to each business request; according to the QPS corresponding to each business request, the number of token buckets is set for each business request using the token bucket algorithm, wherein the token bucket is used to control the business request; after receiving the business request, the business request is set to obtain a token from the token bucket; when the business request obtains the token, the business request is processed. In other words, the embodiment of the present invention abandons the original counting method and uses a token bucket to implement fine-grained control of the processing performance of each business request.
[0075] like Figure 2 As shown, the service request processing device provided in the embodiment of the present application includes:
[0076] The calculation module 22 is used to calculate the query rate per second (QPS) corresponding to each service request;
[0077] QPS is a measure of the amount of traffic a specific query server handles within a specified time. On the Internet, the query rate per second is often used to measure the performance of the domain name system server machine, corresponding to fetches / sec, that is, the number of response requests per second, that is, the maximum throughput capacity, and the calculation relationship is QPS = concurrency / average response time.
[0078] A first setting module 24 is used to set the number of token buckets for each service request using a token bucket algorithm according to the QPS corresponding to each service request, wherein the token bucket is used to control the service request;
[0079] It should be noted that when transmitting data in the network, in order to prevent network congestion, it is necessary to limit the flow of traffic out of the network so that the traffic is sent out at a relatively uniform speed. The token bucket algorithm implements this function, which can control the number of data sent to the network and allow burst data to be sent. The token bucket algorithm is the most commonly used algorithm in network traffic shaping (Traffic Shaping) and rate limiting (Rate Limiting). Typically, the token bucket algorithm is used to control the number of data sent to the network and allow burst data to be sent. A fixed-size token bucket can generate tokens at a constant rate. If the token is not consumed, or the rate of consumption is lower than the rate of generation, the token will continue to increase until the bucket is full, and the tokens generated later will overflow from the bucket. Finally, the maximum number of tokens that can be stored in the bucket will never exceed the size of the bucket. Data packets transmitted to the token bucket need to consume tokens. Data packets of different sizes consume different numbers of tokens. The token bucket control mechanism indicates when traffic can be sent based on whether there are tokens in the token bucket. Each token in the token bucket represents a byte. If there are tokens in the token bucket, traffic is allowed to be sent; if there are no tokens in the token bucket, traffic is not allowed to be sent. Therefore, if the burst threshold is configured properly and there are enough tokens in the token bucket, traffic can be sent at the peak rate. The basic process of the token bucket algorithm is as follows: If the average sending rate configured by the user is r, a token is added to the bucket every 1 / r seconds; assume that the bucket can store and send a maximum of b tokens. If the token bucket is full when the token arrives, the token will be discarded; when a data packet of n bytes arrives, n tokens are deleted from the token bucket and the data packet is sent to the network; if there are less than n tokens in the token bucket, the token is not deleted and the data packet is considered to be outside the flow limit; the algorithm allows bursts of up to b bytes, but from the long-term operation results, the rate of data packets is limited to a constant r. Data packets outside the flow limit can be handled in different ways: they can be discarded; they can be placed in a queue to be transmitted when enough tokens are accumulated in the token bucket; they can continue to be sent, but they need to be specially marked, and these specially marked packets will be discarded when the network is overloaded.
[0080] A second setting module 26 is used to set the service request to obtain a token from the token bucket after receiving the service request;
[0081] The processing module 28 is used to process the service request when the service request obtains the token.
[0082] Optionally, in this embodiment, the processing of the service request includes but is not limited to: forwarding the service request and rejecting the service request.
[0083] pass Figure 2 The device shown calculates the query rate per second (QPS) corresponding to each service request; according to the QPS corresponding to each service request, the number of token buckets is set for each service request using a token bucket algorithm, wherein the token bucket is used to control the service request; after receiving the service request, the service request is set to obtain a token from the token bucket; when the service request obtains the token, the service request is processed. In other words, the embodiment of the present invention abandons the original counting method and uses a token bucket to implement it, and finely controls the processing performance of each service request, thereby solving the problem of relatively rough live broadcast accident prevention and processing methods in related technologies, and achieving the technical effect of effectively protecting the lower-level services and public resources of the live broadcast system, thereby avoiding an avalanche effect that affects the function of the entire system.
[0084] Alternatively, if Figure 3 As shown, the above device also includes:
[0085] An acquisition module 32, configured to acquire the service request processing capability of the token bucket in real time after processing the service request, wherein the service request processing capability includes that the number of service requests processed by the token bucket per second is greater than a first preset threshold;
[0086] The replenishment module 34 is used to replenish the number of the token bucket using preset rules according to the service request processing capacity of the token bucket, wherein the preset rules include millisecond-level replenishment rules.
[0087] For example, the token bucket will be replenished on a millisecond basis based on the number of requests that can be processed per second. This approach can also ensure that a certain function will not remain unprocessed and may be processed in the next second.
[0088] Alternatively, if Figure 4 As shown, the above device also includes:
[0089] The statistics module 42 is used to use the redis cluster to collect statistics on the business request at the inlet gateway of the system.
[0090] Among them, the main principle of single-machine redis statistics is that the concurrent number of each request received is accumulated by +1. When the request is processed, the concurrent number is reduced by 1 in the post-middleware. If a request is not processed within 5S, it will be invalid by default to prevent the request from being stuck. Then, the fuse rules are set for the request routing to determine the concurrency that each route can withstand. When the statistical concurrency reaches the threshold set for the request, the request will no longer be received and forwarded, and it will be intercepted and returned directly at the entry gateway to reduce the concurrency pressure of the system and ensure the processing of core functions.
[0091] Redis cluster adopts the concept of decentralization, and there is no central node. For the client, the entire cluster can be regarded as a whole, and any node can be connected to operate, just like operating a single Redis instance, without any proxy middleware. When the key operated by the client is not assigned to the node, Redis will return a redirect instruction to point to the correct node. Redis also has a built-in high availability mechanism, supporting N master nodes. Each master node can mount multiple slave nodes. When the master node hangs up, the cluster will promote one of its slave nodes as the new master node.
[0092] In the redis cluster, all redis nodes are interconnected, and the nodes use binary protocols to optimize transmission speed and bandwidth. When a node fails, it is considered to be failed only when more than half of the nodes in the cluster detect failure. Unlike the tomcat cluster, which requires the use of a reverse proxy server, any node in the redis cluster can be directly connected to the Java client. The data distribution on the redis cluster uses hash slots (HASH SLOT). The redis cluster has 16384 hash slots built in. When data needs to be stored, redis will first use the CRC16 algorithm to calculate the key, and take the remainder of the calculated result to 16384, so that each key will correspond to a hash slot with a value between 0 and 16383. Redis will store the data on the corresponding redis node based on this remainder. Developers can adjust the distribution range of hash slots on each redis instance according to the performance of each redis instance.
[0093] It is mainly divided into master-slave mode, Sentinel mode, and Cluster mode. Among them, regarding the master-slave mode: when the slave is started, it actively sends a SYNC command to the master. After receiving the SYNC command, the master saves the snapshot (RDB persistence) and caches the commands for saving the snapshot in the background, and then sends the saved snapshot file and cached commands to the slave. After receiving the snapshot file and command, the slave loads the snapshot file and cached execution command. After the replication is initialized, each command received by the master will be sent synchronously to the slave to ensure the consistency of the master-slave data.
[0094] About Sentinel mode: Each sentinel sends a ping command to the master, slave and other sentinel instances it knows about once per second. If the time from the last valid reply of an instance to the ping command exceeds the value specified by the own-after-milliseconds option, the instance will be marked as subjectively offline by sentinel. If a master is marked as subjectively offline, all sentinels that are monitoring this master must confirm once per second that the master has indeed entered the subjective offline state. When a sufficient number of sentinels (greater than or equal to the value specified in the configuration file) confirm that the master has indeed entered the subjective offline state within the specified range, the master will be marked as objectively offline. In general, each sentinel sends an INFO command to all masters and slaves it knows about once every 10 seconds. When a master is marked as objectively offline by sentinel, the frequency of sending INFO commands to all slaves of the offline master is changed from once every 10 seconds to once every second. If there are not enough sentinels to confirm that the master is offline, the objective offline status of the master will be removed. If the master returns a valid recovery to the sentinel's PING command, the subjective offline status of the master will be removed. When using sentinel mode, the client should not connect to redis directly, but connect to sentinel's ip and port, and sentinel will provide a specific redis implementation that can provide services.
[0095] About Cluster mode: Cluster can be said to be a combination of sentinel and master-slave mode. Cluster can realize master-slave and master re-election functions, so if two replicas and three shards are configured, six redis instances are required. Because redis data is distributed to different machines in cluster according to certain rules, when the amount of data is too large, new machines can be added for expansion.
[0096] Optionally, in this embodiment, the number of redis included in the redis cluster can be determined according to the business pressure. When the redis cluster performs statistics, the business requests can be evenly distributed or distributed according to a certain ratio.
[0097] For example, 10 redis are used for statistics at the entrance gateway of the system. When performing statistics, the 10 redis can divide the business requests into 10 parts, and the corresponding redis performs statistics respectively.
[0098] For another example, two redis are used for statistics at the entrance gateway of the system. When performing statistics, the two redis allocate 80% of the business requests to redis1 for statistics, and allocate 20% of the business requests to redis2 for statistics.
[0099] Through the above device, the single-machine redis is changed into a clustered redis, which disperses the redis pressure and improves the overall processing capacity of the system.
[0100] Optionally, the device is further configured to obtain a request threshold of each service request; when the number of requests for the service request is greater than the request threshold, the request threshold is adjusted on the control panel.
[0101] For example, assuming that the request threshold of business request 1 is 5000 and the request threshold of business request 2 is 10000, when the number of requests for business request 1 is 6000 and the number of requests for business request 2 is 7000, since the number of requests for business request 1 (6000) is greater than the request threshold of business request 1 (5000) and the number of requests for business request 2 (7000) is less than the request threshold of business request 2 (10000), it is only necessary to control business request 1 separately on the control panel, while business request 2 maintains the original control method.
[0102] For another example, assuming that the request threshold of business request 1 is 5,000 and the request threshold of business request 2 is 10,000, when the number of requests for business request 1 is 6,000 and the number of requests for business request 2 is 12,000, since the number of requests for business request 1 (6,000) is greater than the request threshold of 5,000 for business request 1 and the number of requests for business request 2 (12,000) is greater than the request threshold of 10,000 for business request 2, it is necessary to control business request 1 and business request 2 on the control panel.
[0103] For another example, assuming that the request threshold of business request 1 is 5000 and the request threshold of business request 2 is 10000, when the number of requests for business request 1 is 4000 and the number of requests for business request 2 is 7000, since the number of requests for business request 1 (4000) is less than the request threshold of 5000 for business request 1 and the number of requests for business request 2 (7000) is less than the request threshold of 10000 for business request 2, business request 1 and business request 2 maintain the original control mode.
[0104] The above device can be used for emergency processing. When a service pressure changes suddenly and unexpectedly, a business request can be controlled separately on the control panel to prevent an avalanche.
[0105] In an optional implementation, the above-mentioned device is also used to determine whether the number of remaining tokens in the token bucket is lower than a second preset threshold; if the judgment result is yes, obtain the priority of the service request; and process the service request according to the priority of the service request.
[0106] Optionally, the value of the second preset threshold can be determined based on the speed at which the system processes business requests. When the speed at which business requests are processed is required to be high, the value of the second preset threshold is set to be relatively large; when the speed at which business requests are processed is required to be relatively low, the value of the second preset threshold is set to be relatively small.
[0107] For example, assuming that the number of remaining tokens in the token bucket is 1000, and the above-mentioned second preset threshold is 1500, in this case, it is necessary to obtain the priority of the business request. Assuming that the priority of the business request is high priority, then the business request will be processed first to ensure that the high-priority business request is processed in time. Assuming that the priority of the business request is low priority, then the business request will not be processed to ensure that the high-priority business request is processed in time.
[0108] Through the above devices, all functions can be available to the greatest extent possible, and the core functions can be guaranteed to be available in the worst case.
[0109] An embodiment of the present invention further provides an electronic device, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the steps of any one of the above methods.
[0110] An embodiment of the present invention further provides a computer-readable storage medium, on which instructions are stored, and when the instructions are executed by a processor, the steps of any of the above methods are implemented.
[0111] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention.
Claims
1. A method for processing a service request, characterized in that: include: Calculate the query rate per second (QPS) corresponding to each business request respectively; According to the QPS corresponding to each service request, the number of token buckets is set for each service request using a token bucket algorithm, wherein the token bucket is used to control the service request; After receiving the service request, setting the service request to obtain a token from the token bucket; When the service request obtains the token, processing the service request; After processing the service request, the method further includes: acquiring the service request processing capability of the token bucket in real time, wherein the service request processing capability includes that the number of service requests processed by the token bucket per second is greater than a first preset threshold; and replenishing the number of the token bucket using preset rules according to the service request processing capability of the token bucket, wherein the preset rules include millisecond-level replenishment rules; At the entrance gateway of the system, a redis cluster is used to count the business requests; the number of redis included in the redis cluster is determined according to the business pressure; when the redis cluster is counting, the business requests are evenly distributed or distributed according to a certain ratio; It also includes: obtaining a request threshold for each business request; when the number of requests for the business request is greater than the request threshold, individually controlling the business request on a control panel; determining whether the number of remaining tokens in the token bucket is lower than a second preset threshold; if the determination result is yes, obtaining the priority of the business request; processing the business request according to the priority of the business request; wherein the second preset threshold is determined according to the speed at which the business request is processed by the system.
2. A service request processing device, characterized in that: include: The calculation module is used to calculate the query rate per second (QPS) corresponding to each business request; A first setting module, configured to set the number of token buckets for each service request using a token bucket algorithm according to the QPS corresponding to each service request, wherein the token bucket is used to control the service request; A second setting module is used to set the service request to obtain a token from the token bucket after receiving the service request; A processing module, used for processing the service request when the service request obtains the token; An acquisition module, used for acquiring the service request processing capacity of the token bucket in real time after processing the service request, wherein the service request processing capacity includes that the number of service requests processed by the token bucket per second is greater than a first preset threshold; A replenishment module, used to replenish the number of the token bucket using preset rules according to the service request processing capacity of the token bucket, wherein the preset rules include millisecond-level replenishment rules; A statistics module is used to count the business requests using a redis cluster at the entry gateway of the system; the number of redis included in the redis cluster is determined according to the business pressure; when the redis cluster performs statistics, the business requests are evenly distributed or distributed according to a certain ratio; It also includes: obtaining a request threshold for each business request; when the number of requests for the business request is greater than the request threshold, individually controlling the business request on a control panel; determining whether the number of remaining tokens in the token bucket is lower than a second preset threshold; if the determination result is yes, obtaining the priority of the business request; processing the business request according to the priority of the business request; wherein the second preset threshold is determined according to the speed at which the business request is processed by the system.
3. An electronic device, characterized in that: include: processor; a memory for storing processor-executable instructions; Wherein, the processor is configured to execute the steps of the method of claim 1.
4. A computer-readable storage medium having instructions stored thereon, characterized in that: When the instructions are executed by a processor, the steps of the method according to claim 1 are implemented.
