# shreyashpandey-dev/redis-distributed-rate-limiter architecture

> Architecture diagram of the GitHub repository shreyashpandey-dev/redis-distributed-rate-limiter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shreyashpandey-dev/redis-distributed-rate-limiter
- Repository: https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter

## Overview

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: [`src/main/java/com/projects/ratelimiter/controller/RateLimitController.java`](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/controller/RateLimitController.java) (Rate API), [`src/main/java/com/projects/ratelimiter/RatelimiterApplication.java`](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/RatelimiterApplication.java) (App entry), [`src/main/java/com/projects/ratelimiter/service/RateLimitService.java`](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/service/RateLimitService.java) (Limiter service), [`src/main/java/com/projects/ratelimiter/exception/GlobalExxception.java`](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/exception/GlobalExxception.java) (Error advice)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_app["Application"]
  node_node_app(("App entry<br/>Spring Boot main"))
  node_node_config["Config<br/>runtime yaml<br/>[application.yaml]"]
end

subgraph group_group_api["API layer"]
  node_node_controller["Rate API<br/>REST controller"]
  node_node_req["Request DTO<br/>API contract"]
  node_node_res["Response DTO<br/>API contract"]
  node_node_error["Error advice<br/>exception handler"]
  node_node_limit_exc(("Limit exceeded<br/>business exception"))
end

subgraph group_group_core["Rate limiting core"]
  node_node_service["Limiter service<br/>domain service"]
  node_node_policy{{"Fixed window<br/>algorithm"}}
  node_node_key["Redis key<br/>key schema"]
  node_node_redis_ops[("Atomic counter<br/>Redis operations")]
  node_node_redis[("Redis store<br/>distributed state")]
end

subgraph group_group_deploy["Packaging"]
  node_node_dockerfile["Image build<br/>container build"]
  node_node_compose["Compose stack<br/>local runtime"]
end

subgraph group_group_test["Tests"]
  node_node_test(("Smoke test<br/>spring test"))
end

node_node_app -->|"wires"| node_node_controller
node_node_app -->|"creates"| node_node_service
node_node_app -->|"registers"| node_node_error
node_node_app -->|"reads"| node_node_config
node_node_controller -->|"accepts"| node_node_req
node_node_controller -->|"invokes"| node_node_service
node_node_controller -->|"returns"| node_node_res
node_node_service -->|"implements"| node_node_policy
node_node_policy -->|"derives"| node_node_key
node_node_service -->|"uses"| node_node_redis_ops
node_node_redis_ops -->|"persists"| node_node_redis
node_node_controller -->|"throws"| node_node_limit_exc
node_node_error -->|"handles"| node_node_limit_exc
node_node_error -->|"maps"| node_node_res
node_node_compose -->|"builds"| node_node_dockerfile
node_node_compose -->|"runs"| node_node_redis
node_node_test -->|"starts"| node_node_app
node_node_test -.->|"covers"| node_node_controller
node_node_service -->|"configures"| node_node_config

click node_node_app "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/RatelimiterApplication.java"
click node_node_config "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/resources/application.yaml"
click node_node_controller "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/controller/RateLimitController.java"
click node_node_req "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/dto/RateLimitRequest.java"
click node_node_res "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/dto/RateLimitResponse.java"
click node_node_error "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/exception/GlobalExxception.java"
click node_node_limit_exc "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/exception/RateLimitExceededException.java"
click node_node_service "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/service/RateLimitService.java"
click node_node_dockerfile "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/tree/main/Dockerfile"
click node_node_compose "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/docker-compose.yaml"
click node_node_test "https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/test/java/com/projects/ratelimiter/RatelimiterApplicationTests.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_app,node_node_config toneBlue
class node_node_controller,node_node_req,node_node_res,node_node_error,node_node_limit_exc toneAmber
class node_node_service,node_node_policy,node_node_key,node_node_redis_ops,node_node_redis toneMint
class node_node_dockerfile,node_node_compose toneRose
class node_node_test toneIndigo
```

## Components

### Application

Spring Boot bootstrap and runtime configuration.

- [App entry](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/RatelimiterApplication.java): Spring Boot main · Bootstraps the Java 17 Spring Boot application. · `src/main/java/com/projects/ratelimiter/RatelimiterApplication.java`
- [Config](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/resources/application.yaml): runtime yaml · Externalized runtime settings such as server and Redis configuration. · `src/main/resources/application.yaml`

### API layer

HTTP ingress, DTOs, and exception translation.

- [Rate API](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/controller/RateLimitController.java): REST controller · Public HTTP ingress for rate-limit checks. · `src/main/java/com/projects/ratelimiter/controller/RateLimitController.java`
- [Request DTO](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/dto/RateLimitRequest.java): API contract · Carries the client identifier for a rate-limit check. · `src/main/java/com/projects/ratelimiter/dto/RateLimitRequest.java`
- [Response DTO](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/dto/RateLimitResponse.java): API contract · Returns allow/deny status, remaining quota, and reset timing. · `src/main/java/com/projects/ratelimiter/dto/RateLimitResponse.java`
- [Error advice](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/exception/GlobalExxception.java): exception handler · Maps business exceptions into HTTP responses such as 429. · `src/main/java/com/projects/ratelimiter/exception/GlobalExxception.java`
- [Limit exceeded](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/exception/RateLimitExceededException.java): business exception · 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](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/main/java/com/projects/ratelimiter/service/RateLimitService.java): domain service · Implements the fixed-window rate-limiting policy. · `src/main/java/com/projects/ratelimiter/service/RateLimitService.java`
- **Fixed window**: algorithm · Per-client windowed counter policy used to enforce quotas.
- **Redis key**: key schema · Maps a client to a counter key such as rate:{clientId}.
- **Atomic counter**: Redis operations · Uses Redis INCR and TTL to create and advance the request window atomically.
- **Redis store**: distributed state · Shared backend storing request counts and expiration metadata across replicas.

### Packaging

Container and local multi-service runtime.

- [Image build](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/tree/main/Dockerfile): container build · Packages the Spring Boot service into a container image. · `Dockerfile`
- [Compose stack](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/docker-compose.yaml): local runtime · Runs the application and Redis together for local development. · `docker-compose.yaml`

### Tests

Basic application test scaffold.

- [Smoke test](https://github.com/shreyashpandey-dev/redis-distributed-rate-limiter/blob/main/src/test/java/com/projects/ratelimiter/RatelimiterApplicationTests.java): spring test · Basic Spring Boot test scaffold for startup validation. · `src/test/java/com/projects/ratelimiter/RatelimiterApplicationTests.java`

## Connections

- 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.
