go-redis / redis_rate

Rate limiting for go-redis
https://redis.uptrace.dev/guide/go-redis-rate-limiting.html
BSD 2-Clause "Simplified" License
824 stars 102 forks source link

Rate and Burst configurations are not clear #92

Open Nomango opened 9 months ago

Nomango commented 9 months ago

Hi, there. Thank you for providing such a simple and useful library.

There are some configuration options confuse me. What do 'Rate' and 'Burst' mean? How can I modify them to achieve the effect of 'maximum 2 times per 10 minutes'?

Hope to add some explanatory comments.

tikhomirovv commented 3 months ago

@Nomango

It seems the "Leaky Bucket" algorithm is being used. Burst is the bucket size, Rate is the bucket's throughput (leak rate = Rate/Period).

The "Leaky Bucket" algorithm represents a model where incoming requests are added to a bucket from which they leak out at a constant rate, ensuring a steady and predictable flow of request processing. The bucket size determines the maximum number of requests that can be temporarily accumulated, allowing the system to handle short-term load spikes. This algorithm is used to prevent system overload and ensure even load distribution by discarding requests if the bucket is full.

To answer your question: if you don't need to account for load spikes, you can do it like this:

limit := redis_rate.Limit{
    Rate:   2,
    Burst:  1,
    Period: time.Minute * 10,
}

Thus, no accumulation of requests will occur, the first request will be allowed immediately, and the next one approximately 5 minutes later (10 minutes / 2).