This article introduces PHP based on redis and uses the token bucket algorithm to control access traffic. It provides a complete algorithm description and demonstration examples for everyone to learn and use.
Whenever there are long domestic holidays or important festivals, domestic scenic spots or subways will be crowded with people, resulting in excessive load. Some will adopt flow restriction measures to limit the number of people entering. When the number of people in the area decreases, After reaching a certain value, entry is allowed.
For example:
The maximum number of people allowed in the area is M
The current number of people in the area is N
Every time a person enters, N 1, when N = M, entry is not allowed
Every time a person leaves, N-1, when When N < Overloaded with work.
Of course we can add servers to share the pressure. First of all, adding servers also takes a certain amount of time to configure, and if servers are added because of a certain activity, these server resources will be wasted after the activity is over.
Therefore, we can first use
current limiting to reduce server pressure according to the business type. Different from the traffic limit in scenic spots,
the time from visit to end of the system is very short, so we only need to know the average duration of each visit and set the maximum number of simultaneous visitors. . Token Bucket Algorithm
1. First, there is a token bucket, and tokens are stored in the bucket. At the beginning, the tokens in the token bucket are full (the number of tokens in the bucket is The quantity can be set according to the server conditions).
2. Each visit takes a token from the bucket. When the token in the bucket is 0, no further visits are allowed.
3. Every once in a while, add tokens until the bucket is full of tokens. (You can put in several tokens at regular intervals according to the actual situation, or directly fill up the token bucket)
We can use the queue of
redis as the token bucket container, Use lPush (enqueue), rPop (dequeue) to implement token addition and consumption operations.
TrafficShaper.class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
demo:
1 2 3 4 5 6 7 8 9 10 |
|
1 |
|
Regularly joining token algorithm
The minimum execution interval of crontab is 1 minute. If the tokens in the token bucket have been consumed in the first few seconds , then the token cannot be obtained in the remaining tens of seconds, causing the user to wait for a long time.
We can optimize the algorithm for adding tokens and add several tokens every few seconds within a minute. This can ensure that there is a chance to obtain tokens every time within a minute.
The add token program called by crontab is as follows, automatically adding 3 tokens per second.
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 |
|
Set a scheduled task and execute it once every minute
1 |
|
Execution simulation consumption
1 |
|
Execution results :
1 |
|
Because the token bucket is full at the beginning (the maximum number of tokens is 10), tokens can be obtained in the first 10 times. After 10 times, the tokens consumed will be greater than the joining token. When the number of cards is exceeded, access is restricted.
This article explains the relevant content of PHP using the token bucket algorithm to implement traffic control based on redis. For more related content, please pay attention to the PHP Chinese website.
Related recommendations:
How to create a QR code class with logo through PHP
The above is the detailed content of Explain the relevant content of PHP using the token bucket algorithm to implement traffic control based on redis. For more information, please follow other related articles on the PHP Chinese website!