Whether in a single service or in a microservice, the API interface provided by the developer for the front end has an upper limit of access. When the frequency of access or the amount of concurrency exceeds its tolerance, we must consider current limit to ensure the interface. Availability or degraded availability. That is, the interface also needs to be installed with a fuse to prevent the system from being paralyzed due to excessive pressure on the system by unexpected requests.
This article will introduce
go-zero adopts a sliding window counting method to calculate the number of accesses to the same resource within a period of time. If it exceeds the specified
limit, access is denied. Of course, if you are accessing different resources within a period of time, the amount of access to each resource does not exceed the
limit. In this case, a large number of requests are allowed to come in.
In a distributed system, there are multiple microservices to provide services. So when instantaneous traffic accesses the same resource at the same time, how to make the counter count normally in the distributed system? At the same time, when computing resources are accessed, multiple calculations may be involved. How to ensure the atomicity of calculations?
go-zerocounts resource visits with the help of
lua scriptto do the whole window calculation to ensure the atomicity of calculation
Let's take a look at several key attributes controlled by
|key||Logo for access to resources|
|ARGV||limit => the total number of requests, if it exceeds the rate limit. Can be set to QPS|
|ARGV||window size => sliding window, use ttl to simulate the effect of sliding|
As for the above
return code, return it to the caller. The caller decides to request subsequent operations:
|return code||tag||call code||mean|
The following picture describes the process of request entry and the subsequent situation when the request triggers
If a large batch of requests comes in at a certain point in the service, the
periodlimit reaches the
limit threshold in a short period of time, and the set time range is far from reaching. The processing of subsequent requests becomes a problem.
It is not processed in
code is returned. The processing of subsequent requests is left to the developer.
- If it is not processed, it is simply to reject the request
- If these requests need to be processed, developers can use
mqto buffer the requests to ease the pressure of the requests
tokenlimitto allow temporary traffic impact
So in the next article, we will talk about
periodlimit current limiting scheme in
go-zero is based on
redis counters. By calling
redis lua script, it guarantees the atomicity of the counting process and guarantees that the counting is normal under distributed conditions. However, this scheme has disadvantages because it needs to record all behavior records within the time window. If this amount is particularly large, memory consumption will become very serious.