Spring Boot Redis Rate Limiter
A production-ready Spring Boot starter that provides annotation-driven rate limiting backed by Redis. Protect your service methods with simple annotations and let the starter handle throttling, metrics, and HTTP responses.Quick start
Get up and running in minutes with a working example
Installation
Add the dependency to your Spring Boot project
GitHub repository
View source code and contribute
Maven Central
Browse published releases
Why this starter
In distributed systems, not all limits belong at the edge. Internal service methods often need their own protection based on business keys, tenants, users, or operation type. This starter standardizes method-level rate limiting so teams avoid duplicating AOP, Redis keying logic, HTTP handling, and metrics wiring in every service.This starter complements API Gateway-level rate limiting by enabling method-level protection inside services. It does not replace gateway throttling.
Key features
Annotation-driven
Use
@RateLimit on methods or classes for declarative throttlingRedis-backed
Fixed-window implementation using
INCR + TTL with no Lua scriptsAuto-configured
Zero manual wiring with Spring Boot 3.x auto-configuration
HTTP 429 handling
Automatic HTTP 429 responses with RFC 7807 Problem Detail format
Pluggable strategies
Custom key resolution and policy providers for advanced use cases
Micrometer metrics
Built-in metrics for allowed, blocked, and error outcomes
Fail-open support
Configurable behavior when Redis is unavailable
Rate limit headers
Optional
Retry-After and RateLimit-* headers on 429 responsesRequirements
Java 17+
Requires Java 17 or higher
Spring Boot 3.x
Compatible with Spring Boot 3.x
Redis
Redis instance accessible to your app
How it works
The starter uses Spring AOP to intercept methods annotated with@RateLimit. When a method is called:
- The AOP interceptor detects the
@RateLimitannotation - The policy provider resolves the rate limit configuration (limit, window duration)
- The key resolver generates a Redis bucket key based on scope and method metadata
- The Redis rate limiter evaluates the request using
INCR+ TTL operations - If allowed, the method executes normally
- If blocked, a
RateLimitExceededExceptionis thrown - The exception handler converts it to an HTTP 429 response with Problem Detail body
- Micrometer metrics are recorded for monitoring
Default behavior
Out of the box, the starter:- Resolves policy from
@RateLimitannotation values - Resolves key using default strategy:
scope:ClassName#methodName - Applies fixed-window Redis rate limiting with
INCR+ TTL - Throws
RateLimitExceededExceptionwhen requests exceed the limit - Returns HTTP
429 Too Many Requestswith RFC 7807 Problem Detail body - Includes
Retry-AfterandRateLimit-*headers in 429 responses - Publishes Micrometer metrics when
MeterRegistryis present - Fails closed (rejects requests) when Redis is unavailable
Example usage
Here’s a simple example of protecting a service method:USER scope.
What’s next
Install the starter
Add the Maven or Gradle dependency to your project
Quick start guide
Follow the step-by-step guide to implement rate limiting