How shreyashpandey-dev/redis-distributed-rate-limiter works
This repository is a single Spring Boot service that exposes a REST API for distributed rate limiting, with Redis used as the shared state store. The core architectural boundary is simple: HTTP in, Redis-backed limit check in the middle, HTTP response out.
1. Application entry point
- RatelimiterApplication is the Spring Boot bootstrap class.
- The runtime is Java 17 with Spring Boot as the application framework.
2. API layer
- RateLimitController is the public ingress point.
- It accepts a request containing a clientId and returns a response indicating whether the request is allowed, how many calls remain, and when the window resets.
- The endpoint described in the README is POST /api/rate-limit.
3. Service layer
- RateLimitService holds the actual rate-limiting logic.
- This is where the fixed-window policy is enforced, making the service logic separate from transport concerns.
4. Rate limiting algorithm
- The design uses a fixed-window counter per client.
- Each client maps to a Redis key like rate:{clientId}.
- Redis performs atomic INCR, and TTL is applied when the counter is first created, which defines the window boundary.
- This makes the service stateless and safe to scale horizontally.
5. Redis as the shared distributed state
- Redis is the only coordination store and holds request counts plus expiration metadata.
- Because all app instances consult the same Redis backend, rate limits are enforced consistently across multiple service replicas.
6. DTO boundary
- RateLimitRequest and RateLimitResponse define the API contract.
- These objects isolate the controller from internal service details and make the external request/response shape explicit.
7. Error handling
- RateLimitExceededException represents the business rule violation when a client exceeds its quota.
- GlobalExxception appears to be the global exception handler/advice layer that converts exceptions into HTTP responses, including 429 Too Many Requests.
8. Configuration
- application.yaml contains runtime configuration, likely including Redis connection settings and server properties.
- This keeps environment-specific values out of the code path.
9. Deployment and runtime packaging
- Dockerfile packages the Spring Boot app into a container.
- docker-compose.yaml runs the app and Redis together for local development and demonstrates the two-tier runtime boundary: application container plus Redis container.
10. Testing
- RatelimiterApplicationTests is a basic Spring Boot test scaffold, suggesting minimal automated coverage beyond startup validation.
11. External dependencies that shape the architecture
- Spring Boot and Spring Data Redis are the main frameworks.
- Redis 7 is the state backend and the critical architectural dependency.
- The system’s behavior depends on Redis atomic operations and TTL support, not on an in-memory cache.
Overall, the architecture is a thin stateless API tier plus a centralized Redis counter store, with containerization provided for local execution and deployment parity.
Main entry points: RateLimitController.java Rate API · RatelimiterApplication.java App entry · RateLimitService.java Limiter service · GlobalExxception.java Error advice
Components 15
Application
Spring Boot bootstrap and runtime configuration.
API layer
HTTP ingress, DTOs, and exception translation.
- Rate API · Public HTTP ingress for rate-limit checks.
src/main/java/com/projects/ratelimiter/controller/RateLimitController.java - Request DTO · Carries the client identifier for a rate-limit check.
src/main/java/com/projects/ratelimiter/dto/RateLimitRequest.java - Response DTO · Returns allow/deny status, remaining quota, and reset timing.
src/main/java/com/projects/ratelimiter/dto/RateLimitResponse.java - Error advice · Maps business exceptions into HTTP responses such as 429.
src/main/java/com/projects/ratelimiter/exception/GlobalExxception.java - Limit exceeded · Signals that a client has exceeded its quota.
src/main/java/com/projects/ratelimiter/exception/RateLimitExceededException.java
Rate limiting core
Business logic and shared Redis-backed state.
- Limiter service · Implements the fixed-window rate-limiting policy.
src/main/java/com/projects/ratelimiter/service/RateLimitService.java - Fixed window · Per-client windowed counter policy used to enforce quotas.
- Redis key · Maps a client to a counter key such as rate:{clientId}.
- Atomic counter · Uses Redis INCR and TTL to create and advance the request window atomically.
- Redis store · Shared backend storing request counts and expiration metadata across replicas.
Packaging
Container and local multi-service runtime.
- Image build · Packages the Spring Boot service into a container image.
Dockerfile - Compose stack · Runs the application and Redis together for local development.
docker-compose.yaml
Tests
Basic application test scaffold.
- Smoke test · Basic Spring Boot test scaffold for startup validation.
src/test/java/com/projects/ratelimiter/RatelimiterApplicationTests.java
Connections 19
- App entry → Rate API: wires · Application context exposes the HTTP controller.
- App entry → Limiter service: creates · Bootstraps the rate-limiting service bean.
- App entry → Error advice: registers · Enables global exception translation.
- App entry → Config: reads · Loads runtime configuration from application.yaml.
- Rate API → Request DTO: accepts · Consumes the request DTO from HTTP clients.
- Rate API → Limiter service: invokes · Delegates rate-limit decisions to the service layer.
- Rate API → Response DTO: returns · Responds with allow/deny and quota metadata.
- Limiter service → Fixed window: implements · Encodes the fixed-window policy.
- Fixed window → Redis key: derives · Builds a per-client Redis counter key.
- Limiter service → Atomic counter: uses · Performs atomic counter updates with TTL semantics.
- Atomic counter → Redis store: persists · Stores counts and expiration in Redis.
- Rate API → Limit exceeded: throws · Rejects requests when the quota is exceeded.
- Error advice → Limit exceeded: handles · Converts the business exception into an HTTP error response.
- Error advice → Response DTO: maps · Shapes error responses for the API boundary.
- Compose stack → Image build: builds · Compose uses the container image for the application service.
- Compose stack → Redis store: runs · Starts Redis alongside the app for local execution.
- Smoke test → App entry: starts · Loads the application context for validation.
- Smoke test → Rate API: covers · Indirectly exercises the API wiring.
- Limiter service → Config: configures · Consumes runtime settings such as Redis connectivity and limits.