BucketGuard is a high-performance, dependency-free Java library for token bucket rate limiting.
Most rate limiting libraries are either too heavy (bringing in large transitive dependencies) or lack advanced features like keyed limits and burst control. BucketGuard is designed for efficiency: it provides a robust, lock-free implementation of the Generic Cell Rate Algorithm (GCRA) with zero allocations on the hot path. It is suitable for protecting high-throughput APIs and backend services.
- Zero Dependencies: The library depends only on the Java 21 standard library. You don't need to worry about classpath conflicts.
- High Performance: Uses
AtomicLongandVarHandlefor lock-free state management. The hot path involves zero object allocations. - Keyed Limiting: Efficiently manages millions of independent buckets. Supports eviction policies like LRU and Expire-After-Access to keep memory usage bounded.
- Burst Control: Configurable burst allowance. You can allow requests to stack up to capacity, or strictly space them out by disabling bursts.
- Contention Strategies: Choose between
ATOMIC(CAS-based) for general use orSTRIPED(partitioned) for extremely high concurrency scenarios to minimize contention. - Strict Math: Optional overflow safety ensures correct behavior even with large token counts or long-running up-times.
This library is available via JitPack.
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.frosxt:BucketGuard:v1.0.0'
}repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
dependencies {
implementation("com.github.frosxt:BucketGuard:v1.0.0")
}<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.frosxt</groupId>
<artifactId>BucketGuard</artifactId>
<version>v1.0.0</version>
</dependency>- Java 21 or newer
- Gradle or Maven
Documentation is available on the Wiki
MIT License. See LICENSE.