# vaibhav7676-vrh/rate-limiter-service architecture

> Architecture diagram of the GitHub repository vaibhav7676-vrh/rate-limiter-service, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vaibhav7676-vrh/rate-limiter-service
- Repository: https://github.com/vaibhav7676-vrh/rate-limiter-service

## Overview

This is a multi-tenant rate-limiting API. A client submits a tenant, API key, and resource to `/check`; the service loads that tenant/resource policy, checks the corresponding token bucket in Redis, and returns an allow/deny result. The Redis Lua script makes the check atomic. The API records outcome and latency metrics for Prometheus and Grafana. The README also documents policy initialization and an in-memory token-bucket reference implementation. Edges reflect shown calls or documented integrations; the Lua implementation itself was not excerpted.

Main entry points: [`app/api/routes.py`](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/api/routes.py) (Check Endpoint), [`app/services/rate_limiter_service.py`](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/services/rate_limiter_service.py) (Limit Service), [`app/storage/bucket_repository.py`](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/bucket_repository.py) (Bucket Repository), [`app/main.py`](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/main.py) (FastAPI App)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API"]
  node_routes["Check Endpoint<br/>[routes.py]"]
  node_schemas["Request Schemas<br/>[schemas.py]"]
  node_main["FastAPI App<br/>[main.py]"]
end

subgraph group_limiting["Rate Limiting"]
  node_service["Limit Service"]
  node_algorithm["Token Bucket Reference<br/>[token_bucket.py]"]
end

subgraph group_storage["Redis Storage"]
  node_bucket_repo["Bucket Repository"]
  node_lua["Atomic Lua Bucket<br/>[token_bucket.lua]"]
  node_policy_repo["Policy Repository"]
  node_redis_client["Redis Client<br/>[redis_client.py]"]
  node_policy_init["Policy Initializer<br/>[init_policies.py]"]
end

subgraph group_observability["Observability"]
  node_metrics["Metrics<br/>[metrics.py]"]
end

node_client(("API Client"))
node_redis[("Redis")]
node_prometheus["Prometheus"]
node_grafana["Grafana Dashboard"]

node_client -->|"checks limits"| node_routes
node_main -->|"registers router"| node_routes
node_routes -->|"accepts request"| node_schemas
node_routes -->|"checks limit"| node_service
node_service -->|"loads policy"| node_policy_repo
node_service -->|"checks bucket"| node_bucket_repo
node_bucket_repo -->|"loads script"| node_lua
node_bucket_repo -->|"evaluates script"| node_redis_client
node_policy_repo -->|"reads and writes"| node_redis_client
node_redis_client -->|"connects to"| node_redis
node_policy_init -->|"saves policies"| node_policy_repo
node_routes -->|"records outcomes"| node_metrics
node_main -->|"mounts endpoint"| node_metrics
node_prometheus -.->|"scrapes metrics"| node_main
node_prometheus -.->|"feeds dashboard"| node_grafana

click node_routes "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/api/routes.py"
click node_schemas "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/api/schemas.py"
click node_main "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/main.py"
click node_service "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/services/rate_limiter_service.py"
click node_algorithm "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/algorithms/token_bucket.py"
click node_bucket_repo "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/bucket_repository.py"
click node_lua "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/lua/token_bucket.lua"
click node_policy_repo "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/policy_repository.py"
click node_redis_client "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/redis_client.py"
click node_policy_init "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/scripts/init_policies.py"
click node_metrics "https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/monitoring/metrics.py"

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_routes,node_schemas,node_main,node_client toneBlue
class node_service,node_algorithm,node_redis toneAmber
class node_bucket_repo,node_lua,node_policy_repo,node_redis_client,node_policy_init toneMint
class node_metrics toneRose
class node_prometheus,node_grafana toneIndigo
```

## Components

### API

- [Check Endpoint](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/api/routes.py): `app/api/routes.py`
- [Request Schemas](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/api/schemas.py): `app/api/schemas.py`
- [FastAPI App](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/main.py): `app/main.py`

### Rate Limiting

- [Limit Service](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/services/rate_limiter_service.py): `app/services/rate_limiter_service.py`
- [Token Bucket Reference](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/algorithms/token_bucket.py): `app/algorithms/token_bucket.py`

### Redis Storage

- [Bucket Repository](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/bucket_repository.py): `app/storage/bucket_repository.py`
- [Atomic Lua Bucket](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/lua/token_bucket.lua): `app/storage/lua/token_bucket.lua`
- [Policy Repository](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/policy_repository.py): `app/storage/policy_repository.py`
- [Redis Client](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/storage/redis_client.py): `app/storage/redis_client.py`
- [Policy Initializer](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/scripts/init_policies.py): `scripts/init_policies.py`

### Observability

- [Metrics](https://github.com/vaibhav7676-vrh/rate-limiter-service/blob/main/app/monitoring/metrics.py): `app/monitoring/metrics.py`

### Other components

- **API Client**
- **Redis**
- **Prometheus**
- **Grafana Dashboard**

## Connections

- API Client → Check Endpoint: checks limits (evidence: `README.md`)
- FastAPI App → Check Endpoint: registers router (evidence: `app/main.py`)
- Check Endpoint → Request Schemas: accepts request (evidence: `app/api/routes.py`)
- Check Endpoint → Limit Service: checks limit (evidence: `app/api/routes.py`)
- Limit Service → Policy Repository: loads policy (evidence: `app/services/rate_limiter_service.py`)
- Limit Service → Bucket Repository: checks bucket (evidence: `app/services/rate_limiter_service.py`)
- Bucket Repository → Atomic Lua Bucket: loads script (evidence: `app/storage/bucket_repository.py`)
- Bucket Repository → Redis Client: evaluates script (evidence: `app/storage/bucket_repository.py`)
- Policy Repository → Redis Client: reads and writes (evidence: `app/storage/policy_repository.py`)
- Redis Client → Redis: connects to (evidence: `app/storage/redis_client.py`)
- Policy Initializer → Policy Repository: saves policies (evidence: `scripts/init_policies.py`)
- Check Endpoint → Metrics: records outcomes (evidence: `app/api/routes.py`)
- FastAPI App → Metrics: mounts endpoint (evidence: `app/main.py`)
- Prometheus → FastAPI App: scrapes metrics (evidence: `README.md`)
- Prometheus → Grafana Dashboard: feeds dashboard (evidence: `README.md`)
