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 of incrby of redis
    • 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:

    1. -- to be compatible with aliyun redis,
    2. -- we cannot use `local key = KEYS[1]` to reuse thekey
    3. local window = tonumber(ARGV[2])
    4. local current = redis.call("INCRBY", KEYS[1], 1)
    5. -- If it is the first visit, set the expiration time => TTL = window size
    6. -- Because it only limits the number of visits for a period
    7. if current == 1 then
    8. redis.call("expire", KEYS[1], window)
    9. return 1
    10. elseif current < limit then
    11. elseif current == limit then
    12. return 2
    13. else
    14. 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: image.png

    It is not processed in periodlimit, but code is returned. The processing of subsequent requests is left to the developer.

    1. If it is not processed, it is simply to reject the request
    2. If these requests need to be processed, developers can use mq to buffer the requests to ease the pressure of the requests
    3. 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.