When the interviewer asks you, "What is the token bucket current limiting algorithm"!
Do you know how to answer to get the interviewer's favor?
Hi, my name is Mic, a Java programmer who has been working for 14 years.
Regarding this question, what dimensions does the interviewer want to examine? How should we answer?
Problem Analysis
The current limiting strategy is a strategy to protect system stability under high concurrent traffic.
Therefore, this issue is mainly due to the fact that Internet companies will investigate.
Of course, in actual business development, current limiting is everywhere, such as
- Thread pools and connection pools avoid resource overuse by limiting the total number of concurrency.
- Limit the number of instantaneous concurrent connections through the limit_conn module on the Nginx reverse proxy server
- At the method level, the number of concurrent requests of the interface is limited by tools such as Sentinel, RateLimiter, etc.
Their core goal is to limit the number of concurrent requests to avoid the problem of system being overwhelmed and unusable.
In the entire system of current limiting, I think there are three more important latitudes
- Resources, that is, for which resources are limited, such as interfaces, or connections, etc.
- Threshold, limit the access of subsequent traffic after the traffic peak reaches
- Behaviors after the current limit is triggered, such as fusing, downgrading, etc.
The current limiting algorithm is the core of the entire current limiting implementation. Different current limiting algorithms can precisely control the granularity of traffic and whether it can support burst traffic. Common current limiting algorithms, sliding window, token bucket, leaky bucket Wait.
The token bucket is a current limiting algorithm that can handle burst traffic. The system adds tokens to the token bucket at a constant rate, and then each request needs to obtain a token from the token bucket to access it. is reached, the current limit will be triggered.
So, I think this question examines two aspects
- Overall perception of current throttling
- Understand the importance of the current limiting algorithm to the current limiting itself
Expert:
The token bucket is an algorithm that controls the access rate of requests.
Its specific working principle is: the system generates tokens at a certain rate and puts them in the token bucket.
Then after all client requests enter the system, the token is obtained from the token bucket first. Successfully obtaining the token means that the access can be done normally.
If the token cannot be obtained, it means that the request traffic is greater than the token generation rate, that is, the concurrent number exceeds the threshold of the system, and the current limiting action will be triggered.
In the case of low traffic, the token bucket can cache a certain number of tokens, so the token bucket can handle instantaneous burst traffic.
Summarize
Well, today's sharing ends here.
If you like my work, remember to like, favorite, and follow!
Scan the full set of interview documents below! ! !
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。