Open
Description
Description
To prevent abuse and ensure fair usage of our RESTful API, we should implement rate limiting using Bucket4j, a Java rate-limiting library based on the token bucket algorithm. This will help improve system reliability and provide a better experience for all users.
Proposed Approach
- Integrate
Bucket4j
via a filter or interceptor in the Spring Boot application. - Apply rate limiting at the controller level using IP-based or user-based keys.
- Use an in-memory store for buckets (
ConcurrentHashMap
) in the first iteration. - Define rate limit configuration (e.g., 10 requests per minute) in a central config file or class.
- Consider using annotations or AOP for cleaner integration if applicable.
Acceptance Criteria
- Add
Bucket4j
dependency to the project. - Implement a filter/interceptor that applies rate limiting logic.
- Protect at least one public-facing endpoint with rate limiting.
- Return
HTTP 429 Too Many Requests
when rate limit is exceeded. - Log events when requests are throttled for observability.
- Include basic unit and/or integration tests for the rate limiter.
Reference
- 🔗 Follow this guide as a base: Baeldung - Rate Limiting with Bucket4j