Search code examples
javaguavarate-limiting

Why does RateLimiter allow only 1 transaction for a rate limit of 2 TPS?


RateLimiter configured for a rate of 2 TPS only allows 1st request and throttles 2nd Request.

import com.google.common.util.concurrent.*;

class Main {

    public static void main(String... args) {
        RateLimiter rl = RateLimiter.create(2);
        System.out.println(rl.getRate());
        System.out.println(rl.tryAcquire());
        System.out.println(rl.tryAcquire());
        System.out.println(rl.tryAcquire());
   }

}

Output:

2.0
true
false
false

I expect that since rate is configured to 2 TPS, it would allow first two requests and not just the first one. Why does this happen?

Using guava-27.0-jre.jar.

Any help is appreciated.


Solution

  • Smooth distribution

    A RateLimiter is defined primarily by the rate at which permits are issued. Absent additional configuration, permits will be distributed at a fixed rate, defined in terms of permits per second. Permits will be distributed smoothly, with the delay between individual permits being adjusted to ensure that the configured rate is maintained.
    It is possible to configure a RateLimiter to have a warmup period during which time the permits issued each second steadily increases until it hits the stable rate.

    Ref: Rate Limiter

    The configured rate is distributed evenly across the interval. (more like a sliding window)