Prerequisites
Before you begin, make sure you have:- Java 17 or higher
- Spring Boot 3.x application
- Access to a Redis instance (or Docker to run one locally)
Step-by-step guide
Add the dependency
Add the Spring Boot Redis Rate Limiter starter to your project:
The starter uses Spring Boot’s auto-configuration, so no manual bean wiring is required.
Start Redis
You need a running Redis instance. If you don’t have one, start Redis using Docker:This command starts Redis 7 on port 6379 in the background.
Configure Redis connection
Add Redis connection properties to your
application.yml or application.properties:If you’re using a cloud Redis service, update the host and port accordingly. You can also configure authentication using
spring.data.redis.password.Create a service with @RateLimit
Add the This configuration allows 10 requests per minute globally across all users.
@RateLimit annotation to any service method you want to protect. Here’s a billing service example:BillingService.java
Create a REST controller
Expose your rate-limited service through a REST endpoint:
BillingController.java
Start your application
Run your Spring Boot application:Or if you’re using Gradle:Watch for the auto-configuration log messages confirming the rate limiter is active.
Test the rate limit
Make repeated requests to your endpoint to trigger the rate limit. You can use curl in a loop:The first 10 requests should succeed with a 200 response. Starting from the 11th request, you’ll see rate limit errors.
Observe the HTTP 429 response
When the rate limit is exceeded, you’ll receive an HTTP 429 Too Many Requests response with detailed information:The response includes helpful headers:
Retry-After: Seconds until you can retryRateLimit-Limit: Maximum requests allowedRateLimit-Remaining: Requests remaining (0 when blocked)RateLimit-Reset: Seconds until the window resets
The response follows RFC 7807 Problem Details format, making it easy to parse and handle in client applications.
Verify Redis keys
Check that rate limit data is being stored in Redis:Then in the Redis CLI:You’ll see the current request count and the key’s TTL (time to live).
Configure optional settings
Customize the rate limiter behavior by adding properties to your configuration:Configuration options:
application.yml
enabled: Enable or disable rate limiting globallyredis-key-prefix: Prefix for Redis keys (default:ratelimiter)fail-open: Iftrue, allows requests when Redis is unavailableinclude-http-headers: Include rate limit headers in 429 responsesmetrics-enabled: Enable Micrometer metrics collection
What happens under the hood
When a request hits a rate-limited method:- The AOP interceptor detects the
@RateLimitannotation - The policy provider resolves the limit and window duration
- The key resolver generates a Redis key based on scope and method
- Redis executes
INCRon the key and sets a TTL if needed - If the count exceeds the limit, a
RateLimitExceededExceptionis thrown - The exception handler converts it to an HTTP 429 response
- Metrics are recorded for monitoring
The implementation uses a fixed-window algorithm with Redis
INCR and TTL commands. No Lua scripts are required, making it simple and Redis cluster-compatible.Next steps
Now that you have basic rate limiting working, you can:- Learn about per-user rate limiting with custom key resolvers
- Configure class-level rate limits for shared policies
- Monitor rate limiting with Micrometer metrics
- Understand the fail-open vs fail-closed strategies