periodlimit
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.
go-zero
counts resource visits with the help ofincrby
ofredis
- Use
lua script
to do the whole window calculation to ensure the atomicity of calculation
Let’s take a look at several key attributes controlled by lua script
:
-- to be compatible with aliyun redis,
-- we cannot use `local key = KEYS[1]` to reuse thekey
local window = tonumber(ARGV[2])
local current = redis.call("INCRBY", KEYS[1], 1)
-- If it is the first visit, set the expiration time => TTL = window size
-- Because it only limits the number of visits for a period
if current == 1 then
redis.call("expire", KEYS[1], window)
return 1
elseif current < limit then
elseif current == limit then
return 2
else
return 0
As for the above return code
, return it to the caller. The caller decides to request subsequent operations:
return code | tag | call code | mean |
---|---|---|---|
0 | OverQuota | 3 | over limit |
1 | Allowed | 1 | in limit |
2 | HitQuota | 2 | hit limit |
The following picture describes the process of request entry and the subsequent situation when the request triggers limit
:
It is not processed in periodlimit
, but 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
mq
to buffer the requests to ease the pressure of the requests - Use
tokenlimit
to allow temporary traffic impact
So in the next article, we will talk about tokenlimit
The periodlimit
current limiting scheme in go-zero
is based on redis
counters. By calling , 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.