dev.club — where best developers and top companies connect.

dev.club — where best developers and top companies connect.Invite only

Request invite

Java rate-limiting library based on token-bucket algorithm.

Licence

Get dependency

The Bucket4j is distributed through Maven Central:

Java 17 dependency
<!-- For java 17+ -->
<dependency>
  <groupId>com.bucket4j</groupId>
  <artifactId>bucket4j_jdk17-core</artifactId>
  <version>8.20.0</version>
</dependency>

Quick start

import io.github.bucket4j.Bucket;

...
// bucket with capacity 20 tokens and with refilling speed 1 token per each 6 second
private static Bucket bucket = Bucket.builder()
      .addLimit(limit -> limit.capacity(20).refillGreedy(10, Duration.ofMinutes(1)))
      .build();

private void doSomethingProtected() {
   if (bucket.tryConsume(1)) {
      doSomething();    
   } else {
      throw new SomeRateLimitingException();
   }
}

More examples can be found there

Documentation

Bucket4j basic features

Bucket4j distributed features

In additional to basic features described above, Bucket4j provides ability to implement rate-limiting in cluster of JVMs:

Spring boot starter

Bucket4j is not a framework, it is a library, with Bucket4j you need to write a code to achive your goals. For generic use-cases, try to look at powerfull Spring Boot Starter for Bucket4j, that allows you to set access limits on your API effortlessly. Its key advantage lies in the configuration via properties or yaml files, eliminating the need for manual code authoring.

Supported JCache compatible(or similar) back-ends

In addition to local in-memory buckets, the Bucket4j supports clustered usage scenario on top of following back-ends:

Back-end Async supported Flexible per-entry expiration Optimized serialization Thin-client support Documentation link
JCache API (JSR 107) No No No No bucket4j-jcache
Hazelcast Yes Yes Yes No bucket4j-hazelcast
Apache Ignite Yes No n/a Yes bucket4j-ignite
Apache Ignite 3 Yes No n/a Yes bucket4j-ignite3
Inifinispan Yes Yes Yes No bucket4j-infinispan
Oracle Coherence Yes Yes Yes No bucket4j-coherence
Couchbase Yes Yes Yes No bucket4j-couchbase
Apache Geode (GemFire) No No n/a No bucket4j-geode

Redis back-ends

Back-end Async supported Redis cluster supported Documentation link
Redis/Vert.x Redis Client Yes Yes bucket4j-redis/Vert.x
Redis/Redisson Yes Yes bucket4j-redis/Redisson
Redis/Jedis No Yes bucket4j-redis/Jedis
Redis/Lettuce Yes Yes bucket4j-redis/Lettuce

Valkey back-ends

Back-end Async supported Redis cluster supported Documentation link
Valkey/Glide Yes Yes bucket4j-valkey/Glide

Mongo back-ends

Back-end Async supported Documentation link
Mongodb/mongodb-driver-sync No bucket4j-mongodb/sync
Mongodb/mongodb-driver-reactivestreams Yes bucket4j-mongodb/async

Memcached back-ends

Back-end Async supported Documentation link
Memcached/compare-and-swap based Yes bucket4j-memcached

Cassandra back-ends

Back-end Async supported Documentation link
Apache Cassandra/Lightweight Transactions Yes bucket4j-cassandra

JDBC back-ends

Back-end Documentation link
MySQL bucket4j-mysql
PostgreSQL bucket4j-postgresql
Oracle bucket4j-oracle
Microsoft SQL Server bucket4j-mssql
MariaDB bucket4j-mariadb
DB2 bucket4j-db2

Local caches support

Sometimes you are having deal with bucket per key scenarios but distributed synchronization is unnecessary, for example where request stickiness is provided by a load balancer, or other use-cases where stickiness can be achieved by the application itself, for example, Kafka consumer. For such scenarios Bucket4j provides support for following list of local caching libraries:

Back-end Documentation link
Caffeine bucket4j-caffeine

Third-party integrations

Back-end Project page
Datomic Database clj-bucket4j-datomic

Bucket4j Backward compatibility policy

Snapshot builds

Every commit/merge to the master branch is automatically built and published as a -SNAPSHOT artifact to GitHub Packages (see .github/workflows/snapshot-release.yml).

To consume a snapshot, add the GitHub Packages repository to your pom.xml:

<repositories>
  <repository>
    <id>github</id>
    <name>Bucket4j GitHub Packages</name>
    <url>https://maven.pkg.github.com/bucket4j/bucket4j</url>
  </repository>
</repositories>

GitHub Packages requires authentication even for reading public packages, so add a server entry with your GitHub username and a personal access token that has the read:packages scope to your ~/.m2/settings.xml:

<servers>
  <server>
    <id>github</id>
    <username>YOUR_GITHUB_USERNAME</username>
    <password>YOUR_GITHUB_TOKEN</password>
  </server>
</servers>

Then reference the snapshot version (current pom.xml version with the -SNAPSHOT suffix), for example:

<dependency>
  <groupId>com.bucket4j</groupId>
  <artifactId>bucket4j_jdk17-core</artifactId>
  <version>8.21.0-SNAPSHOT</version>
</dependency>

Have a question?

Feel free to ask via:

License

Copyright 2015-2026 Vladimir Bukhtoyarov Licensed under the Apache Software License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0.

Join libs.tech

...and unlock some superpowers

GitHub

We won't share your data with anyone else.