Traffic Warning and throttling solutions are commonly used. The first sliding window mode controls the number of visits within a period of time, and throttling is imposed when the number of visits reaches a certain peak. The second mode is the number of concurrent users. By controlling the maximum number of concurrent users, you can control the traffic. The following describes the advantages and disadvantages of the two methods.
1. Sliding Window Mode
Mode Analysis:
When an access request comes in, we determine whether the total access volume in the previous N Units has exceeded the set threshold and the number of requests on the current time slice is + 1.
Each format represents a fixed time (for example, 1 s), and each grid has a counter. We need to obtain the first five seconds of request, that is, for the current time slice I ~ The counter on the time slice for the I-4 is accumulated.
The implementation of this model is more in line with the essential significance of traffic control. Easy to understand. However, due to the unpredictable traffic volume, a large number of requests flood in the first half of the unit time, while the last half decline all requests. (Usually, it is necessary to reduce the unit time to a small enough value.) Secondly, it is difficult to determine how many appropriate the threshold value is. We can only use experience or simulation (such as stress testing) even the stress test is difficult to accurately estimate. In cluster deployment, the hardware parameters of each machine are different, which may lead to different threshold values for each machine. The system pressure on the same machine at different time points is also different (for example, there are some tasks at night, or other business operations), and the maximum thresholds that can be borne are also different, we cannot fully consider it.
Therefore, the sliding window mode is usually applicable to the need to protect a certain resource (or the commitment is more appropriate: I have promised a certain interface provider, and the maximum number of calls cannot exceed XX ), for example, database protection controls the calling of a service.
Code Implementation ideas:
Each time slice (unit time) is an independent counter used for Array Storage. Map the current time to an array in some way (such as modulo. Each access operation first adds one counter to the current time slice, and then calculates the total access traffic of the First n time slice. If the traffic exceeds the threshold, the traffic is limited.
@ Https://www.cnblogs.com/yuhan-TB/p/3765457.html
Two throttling Modes