Home

Awesome

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.14.0</version>
</dependency>
Java 11 dependency
<!-- For java 11 -->
<dependency>
  <groupId>com.bucket4j</groupId>
  <artifactId>bucket4j_jdk11-core</artifactId>
  <version>8.14.0</version>
</dependency>
Java 8 dependencies

Builds for Java 8 are not distributed through Maven Central. For direct downloading of Bucket4j builds for Java 8 visit this page.

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-endAsync supportedFlexible per-entry expirationOptimized serializationThin-client supportDocumentation link
JCache API (JSR 107)NoNoNoNobucket4j-jcache
HazelcastYesYesYesNobucket4j-hazelcast
Apache IgniteYesNon/aYesbucket4j-ignite
InifinispanYesYesYesNobucket4j-infinispan
Oracle CoherenceYesYesYesNobucket4j-coherence

Redis back-ends

Back-endAsync supportedRedis cluster supportedDocumentation link
Redis/RedissonYesYesbucket4j-redis/Redisson
Redis/JedisNoYesbucket4j-redis/Jedis
Redis/LettuceYesYesbucket4j-redis/Lettuce

JDBC back-ends

Back-endDocumentation link
MySQLbucket4j-mysql
PostgreSQLbucket4j-postgresql
Oraclebucket4j-oracle
Microsoft SQL Serverbucket4j-mssql
MariaDBbucket4j-mariadb
DB2bucket4j-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-endDocumentation link
Caffeinebucket4j-caffeine

Third-party integrations

Back-endProject page
Datomic Databaseclj-bucket4j-datomic

Bucket4j Backward compatibility policy

Have a question?

Feel free to ask via:

License

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