Hello! I am thinking of the following topic to contribute to Exercism’s Java Practices.
Challenge Description & Concepts
Imagine you’re running a public API. To prevent abuse, you need to rate limit requests — allowing only a certain number of requests per user within a given time window. If a user exceeds their quota, requests should be rejected.
Implement a thread-safe rate limiter in Java that supports multiple clients identified by keys (e.g., user IDs). The exercise should model a fixed-window or token-bucket rate limiter.
Concepts covered can include:
Concurrency and synchronization - How to use synchronized, Lock or AtomicInteger safely
Time handling - Using Duration, Instant, System.nanoTime() correctly for time windows
Data structures - Map for per user counters, eviction strategies for stale entries
Real world system design - Insight on how APIs, reverse proxies and distributed system control load
There are multiple solution paths, which can invite discussion.
Signature can look something like the following:
public final class RateLimiter {
public RateLimiter(int limit, Duration window);
public boolean allow(String key);
}
RateLimiter limiter = new RateLimiter(3, Duration.ofSeconds(1));
Example test scenario:
limiter.allow("alice"); // true
limiter.allow("alice"); // true
limiter.allow("alice"); // true
limiter.allow("alice"); // false (limit reached within 1s window)
// After >1s passes
limiter.allow("alice"); // true again
limiter.allow("bob"); // true (independent key)
limiter.allow("alice"); // still subject to Alice’s quota