Java Leaky bucket rate limiting Algorithm

The Leaky Bucket Algorithm is a popular algorithm used for rate limiting. It can be used to limit the rate of requests being processed by a server or to limit the rate of messages sent across a network.

The algorithm works by maintaining a fixed size bucket that is filled with tokens at a fixed rate. Whenever a request or message arrives, a token is removed from the bucket. If the bucket is empty, the request or message is dropped or delayed. This ensures that the rate of requests or messages being processed does not exceed the rate at which the bucket is being filled.

Leaky Bucket Algorithm in Java:

public class LeakyBucket {
    private final int capacity;
    private final int rate;
    private int water;
    private long timestamp;

    public LeakyBucket(int capacity, int rate) {
        this.capacity = capacity;
        this.rate = rate;
        this.water = 0;
        this.timestamp = System.currentTimeMillis();

    public synchronized boolean allowRequest() {
        long now = System.currentTimeMillis();
        water = Math.max(0, water - (int) ((now - timestamp) / 1000) * rate);
        timestamp = now;
        if (water < capacity) {
            return true;
        } else {
            return false;

In this implementation, the LeakyBucket class takes two parameters: capacity and rate. The capacity parameter specifies the maximum number of tokens that the bucket can hold at any given time, while the rate parameter specifies the rate at which the bucket is being filled with tokens (measured in tokens per second).

The water variable represents the current number of tokens in the bucket, while the timestamp variable represents the last time the bucket was checked for tokens.

The allowRequest() method is the main method of the class. It returns a boolean value indicating whether or not a request can be processed. The method first calculates the time elapsed since the last time the bucket was checked and removes tokens accordingly. If the bucket is not yet full, the method adds a token to the bucket and returns true. Otherwise, the method returns false.

This implementation can be used in a variety of contexts, such as limiting the rate of requests to a web server or the rate of messages sent across a network. By adjusting the capacity and rate parameters, the algorithm can be fine-tuned to suit specific needs.