SoFunction
Updated on 2025-05-23

Common current limiting schemes and implementation methods in Java

In high concurrency scenarios,Rate LimitingIt is an important protection mechanism used to control the system's request traffic and avoid system overload. The following are common current limiting solutions and their Java implementations.

1. Common algorithms for current limiting

1.1 Counter Algorithm

  • principle: count the number of requests within a fixed time window, and reject the request if the threshold exceeds the threshold.

  • advantage: Simple implementation.

  • shortcoming: Unable to deal with burst traffic.

1.2 Sliding window algorithm

  • principle: Divide the time window into multiple small windows to count the number of requests in the last period of time.

  • advantage: Smoother than counter algorithm.

  • shortcoming: Implementation is complex.

1.3 Leaked bucket algorithm

  • principle: Requests flow out at a fixed rate, requests exceeding the bucket capacity are discarded or waited.

  • advantage: Smooth flow.

  • shortcoming: Unable to deal with burst traffic.

1.4 Token bucket algorithm

  • principle: Generate tokens at a fixed rate, and the request needs to obtain the token before it can be processed.

  • advantage: Support burst traffic.

  • shortcoming: Implementation is complex.

2. Java implementation of current limiting solution

The following is based onCounter AlgorithmandToken bucket algorithmJava implementation example.

2.1 Counter algorithm implementation

import ;

public class CounterRateLimiter {
    private final int limit; // Current limit threshold    private final long interval; // Time window (milliseconds)    private final AtomicInteger counter; // Counter    private long lastResetTime; // Last time reset
    public CounterRateLimiter(int limit, long interval) {
         = limit;
         = interval;
         = new AtomicInteger(0);
         = ();
    }

    public boolean tryAcquire() {
        long now = ();
        if (now - lastResetTime > interval) {
            // Reset the counter            (0);
            lastResetTime = now;
        }
        // Determine whether the threshold value is exceeded        return () <= limit;
    }

    public static void main(String[] args) throws InterruptedException {
        CounterRateLimiter limiter = new CounterRateLimiter(10, 1000); // 10 times per second        for (int i = 0; i < 20; i++) {
            ("ask " + i + ": " + (() ? "pass" : "Price restricted"));
            (100); // Simulate request interval        }
    }
}

2.2 Token bucket algorithm implementation

import ;

public class TokenBucketRateLimiter {
    private final long capacity; // Barrel capacity    private final long rate; // Token generation rate (token/ms)    private final AtomicLong tokens; // Current number of tokens    private long lastRefillTime; // Last time to replenish token
    public TokenBucketRateLimiter(long capacity, long rate) {
         = capacity;
         = rate;
         = new AtomicLong(capacity);
         = ();
    }

    public boolean tryAcquire() {
        refillTokens(); // Supplemental token        long currentTokens = ();
        if (currentTokens > 0) {
            return () >= 0;
        }
        return false;
    }

    private void refillTokens() {
        long now = ();
        long elapsedTime = now - lastRefillTime;
        long newTokens = elapsedTime * rate; // Calculate the number of new tokens        if (newTokens > 0) {
            lastRefillTime = now;
            (old -> (capacity, old + newTokens)); // Update the number of tokens        }
    }

    public static void main(String[] args) throws InterruptedException {
        TokenBucketRateLimiter limiter = new TokenBucketRateLimiter(10, 1); // Bucket capacity is 10, 1 token is generated per second        for (int i = 0; i < 20; i++) {
            ("ask " + i + ": " + (() ? "pass" : "Price restricted"));
            (100); // Simulate request interval        }
    }
}

3. Use Guava's RateLimiter

Google Guava providesRateLimiterClass, implements current limiting based on token bucket algorithm.

3.1 Add dependencies

<dependency>
    <groupId></groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

Run HTML

3.2 Use examples

import ;

public class GuavaRateLimiterExample {
    public static void main(String[] args) throws InterruptedException {
        RateLimiter limiter = (1.0); // 1 current limit per second        for (int i = 0; i &lt; 10; i++) {
            ("ask " + i + ": " + (() ? "pass" : "Price restricted"));
            (300); // Simulate request interval        }
    }
}

4. Choice of current limiting solution

algorithm advantage shortcoming Applicable scenarios
counter Simple implementation Unable to deal with burst traffic Simple flow restriction scenario
Sliding window Smoother than counter Implementation complex Scenarios that require smooth current limit
Leaked bucket Smooth flow Unable to deal with burst traffic Scenarios where traffic is strictly controlled
Token bucket Support burst traffic Implementation complex Scenarios that need to support burst traffic
Guava Simple and easy to use, powerful Relying on third-party libraries Scenarios that require rapid implementation of current limit

5. Summary

  • Current limiting is an important means to protect the system. Common current limiting algorithms include counters, sliding windows, leaky buckets and token buckets.

  • In Java, you can implement it through customization or use Guava.RateLimiterAchieving current limit.

  • Select the appropriate current limiting solution according to business needs to ensure the stability and high availability of the system.

Through the above content, you can easily master the implementation method of current limiting!

This is the end of this article about the common Java flow restriction schemes and implementations. For more related Java flow restriction schemes, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!