Zero dependencies
Only the JDK. No framework, no logging binding, no JSON library, no Redis client to exclude from your build.
One 18 KB jar, six algorithms, zero runtime dependencies, no background threads.
<dependency>
<groupId>io.github.onblog</groupId>
<artifactId>java-limiter</artifactId>
<version>1.0.0</version>
</dependency>implementation 'io.github.onblog:java-limiter:1.0.0'import io.github.onblog.limiter.RateLimiter;
import io.github.onblog.limiter.RateLimiters;
import java.time.Duration;
// 100 requests per second on average, bursts of up to 200.
RateLimiter limiter = RateLimiters.tokenBucket("payments", 200, 100, Duration.ofSeconds(1));
if (limiter.tryAcquire()) {
handleRequest();
} else {
response.setStatus(429);
}A rejected request is not an error: tryAcquire returns false, nothing is logged and nothing is thrown.
| Limiter | Behaviour | Use it for |
|---|---|---|
TokenBucketLimiter | Average rate plus a burst budget that refills while idle | Public APIs, protecting a dependency that tolerates bursts |
LeakyBucketLimiter | Strictly even pacing, never accumulates a burst | Outbound calls, MQ consumption, anything that must be smooth |
SlidingWindowLimiter | At most N permits in any rolling window | Login attempts, per-minute or per-hour quota with no exploitable boundary |
FixedWindowLimiter | At most N permits per aligned window, one AtomicLong | Very high traffic with loose accuracy needs |
WarmUpTokenBucketLimiter | Starts at a third of the rate, ramps up over a warm-up period | Services that are cold right after start-up |
ConcurrencyLimiter | Caps permits in flight, not permits per second | Protecting thread pools, connection pools, slow downstreams |
Read Algorithms for how each one works and what it guarantees under contention.
The full documentation is also available in Chinese: 简体中文.