MCPcopy Index your code
hub / github.com/bucket4j/bucket4j

github.com/bucket4j/bucket4j @8.19.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 8.19.0 ↗ · + Follow
3,145 symbols 9,462 edges 351 files 421 documented · 13%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

Licence

Get dependency

The Bucket4j is distributed through Maven Central:

Java 17 dependency

<dependency>
  <groupId>com.bucket4j</groupId>
  <artifactId>bucket4j_jdk17-core</artifactId>
  <version>8.19.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

  • Absolutely non-compromise precision - Bucket4j does not operate with floats or doubles, all calculation are performed in the integer arithmetic, this feature protects end users from calculation errors involved by rounding.
  • Effective implementation in terms of concurrency:
  • Bucket4j is good scalable for multi-threading case it by defaults uses lock-free implementation.
  • In same time, library provides different concurrency strategies that can be chosen when default lock-free strategy is not desired.
  • Effective API in terms of garbage collector footprint: Bucket4j API tries to use primitive types as much as it is possible in order to avoid boxing and other types of floating garbage.
  • Pluggable listener API that allows to implement monitoring and logging.
  • Rich diagnostic API that allows to investigate internal state.
  • Rich configuration management - configuration of the bucket can be changed on fly

Bucket4j distributed features

In additional to basic features described above, Bucket4j provides ability to implement rate-limiting in cluster of JVMs: - Bucket4j out of the box supports any GRID solution which compatible with JCache API (JSR 107) specification. - Bucket4j provides the framework that allows to quickly build integration with your own persistent technology like RDMS or a key-value storage. - For clustered usage scenarios Bucket4j supports asynchronous API that extremely matters when going to distribute world, because asynchronous API allows avoiding blocking your application threads each time when you need to execute Network request.

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
Inifinispan Yes Yes Yes No bucket4j-infinispan
Oracle Coherence Yes Yes Yes No bucket4j-coherence
Couchbase Yes Yes Yes No bucket4j-couchbase

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

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

Have a question?

Feel free to ask via: * Bucket4j github issue tracker to report a bug. * Bucket4j github discussions for questions, feature proposals, sharing of experience.

License

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

Extension points exported contracts — how you extend this code

ExpiredEntriesCleaner (Interface)
Interface used by particular ProxyManager implementations to indicate that they support the manual(triggered by [18 implementers]
bucket4j-core/src/main/java/io/github/bucket4j/distributed/proxy/ExpiredEntriesCleaner.java
RedisApi (Interface)
(no doc) [5 implementers]
bucket4j-redis/bucket4j-jedis/src/main/java/io/github/bucket4j/redis/jedis/RedisApi.java
RedisApi (Interface)
(no doc) [5 implementers]
bucket4j-redis/bucket4j-vertx/src/main/java/io/github/bucket4j/redis/vertx/RedisApi.java
RedisApi (Interface)
(no doc) [5 implementers]
bucket4j-redis/bucket4j-lettuce/src/main/java/io/github/bucket4j/redis/lettuce/RedisApi.java
Optimization (Interface)
Specifies request optimization strategy for BucketProxy and AsyncBucketProxy. By default, an interaction [10 implementers]
bucket4j-core/src/main/java/io/github/bucket4j/distributed/proxy/optimization/Optimization.java
AsyncCompareAndSwapOperation (Interface)
Describes the set of operations that AbstractCompareAndSwapBasedProxyManager typically performs in reaction to u [14 implementers]
bucket4j-core/src/main/java/io/github/bucket4j/distributed/proxy/generic/compare_and_swap/AsyncCompareAndSwapOperation.java
SelectForUpdateBasedTransaction (Interface)
Describes the set of operations that AbstractSelectForUpdateBasedProxyManager typically performs in reaction to [7 implementers]
bucket4j-core/src/main/java/io/github/bucket4j/distributed/proxy/generic/select_for_update/SelectForUpdateBasedTransaction.java
MockCommand (Interface)
(no doc) [13 implementers]
bucket4j-core/src/test/java/io/github/bucket4j/distributed/proxy/optimization/batch/mock/MockCommand.java

Core symbols most depended-on inside this repo

get
called by 368
bucket4j-redis/bucket4j-lettuce/src/main/java/io/github/bucket4j/redis/lettuce/RedisApi.java
build
called by 223
bucket4j-core/src/main/java/io/github/bucket4j/BandwidthBuilder.java
builder
called by 115
bucket4j-core/src/main/java/io/github/bucket4j/Bucket.java
getNumber
called by 110
bucket4j-core/src/main/java/io/github/bucket4j/distributed/versioning/Version.java
currentTimeNanos
called by 104
bucket4j-core/src/main/java/io/github/bucket4j/TimeMeter.java
check
called by 90
bucket4j-core/src/main/java/io/github/bucket4j/distributed/versioning/Versions.java
getTableName
called by 76
bucket4j-core/src/main/java/io/github/bucket4j/distributed/jdbc/BucketTableSettings.java
copy
called by 74
bucket4j-core/src/main/java/io/github/bucket4j/BucketState.java

Shape

Method 2,716
Class 369
Interface 53
Enum 7

Languages

Java100%
Kotlin1%

Modules by API surface

bucket4j-core/src/main/java/io/github/bucket4j/BucketExceptions.java48 symbols
bucket4j-core/src/main/java/io/github/bucket4j/BucketState64BitsInteger.java42 symbols
bucket4j-core/src/main/java/io/github/bucket4j/AbstractBucket.java41 symbols
bucket4j-core/src/main/java/io/github/bucket4j/local/ThreadUnsafeBucket.java38 symbols
bucket4j-core/src/main/java/io/github/bucket4j/local/SynchronizedBucket.java38 symbols
bucket4j-core/src/main/java/io/github/bucket4j/local/LockFreeBucket.java38 symbols
bucket4j-core/src/test/java/io/github/bucket4j/tck/AbstractDistributedBucketTest.java32 symbols
bucket4j-core/src/test/java/io/github/bucket4j/distributed/proxy/MaxRetriesTest.java30 symbols
bucket4j-core/src/main/java/io/github/bucket4j/distributed/proxy/DefaultBucketProxy.java29 symbols
bucket4j-core/src/main/java/io/github/bucket4j/Bandwidth.java29 symbols
bucket4j-core/src/main/java/io/github/bucket4j/distributed/remote/RemoteBucketState.java28 symbols
bucket4j-core/src/main/java/io/github/bucket4j/distributed/proxy/AbstractProxyManager.java27 symbols

For agents

$ claude mcp add bucket4j \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page