https://github.com/rahul07bagul/design-rate-limiter
Rate Limiter
https://github.com/rahul07bagul/design-rate-limiter
algorithms design hld java lld rate-limiter redis spring-boot system
Last synced: about 2 months ago
JSON representation
Rate Limiter
- Host: GitHub
- URL: https://github.com/rahul07bagul/design-rate-limiter
- Owner: rahul07bagul
- License: mit
- Created: 2025-03-11T16:11:53.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-26T21:27:05.000Z (over 1 year ago)
- Last Synced: 2025-03-26T22:28:27.022Z (over 1 year ago)
- Topics: algorithms, design, hld, java, lld, rate-limiter, redis, spring-boot, system
- Language: Java
- Homepage:
- Size: 785 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Design Rate Limiter
A standalone rate limiter service for Java Spring Boot applications that supports multiple rate limiting algorithms and can be used as a separate service to control API request rates.
## Demo

## High Level Design

## Class Diagram

## Features
- Multiple rate limiting algorithms supported:
- Token Bucket
- Leaking Bucket
- Fixed Window Counter
- Redis-based storage.
- Dynamic rule loading from DOC files.
- Scheduled rule reloading (every 1 hour).
- REST API for rate limit checking and rule management.
- Standalone service architecture.
## Integration with Application
```sh
@Service
public class ApiGatewayService {
@Autowired
private RestTemplate restTemplate;
@Value("${rate.limiter.url}")
private String rateLimiterUrl;
public boolean isRequestAllowed(String apiPath, String method, String clientId) {
String resourceId = apiPath + ":" + method;
RateLimitRequest request = new RateLimitRequest(resourceId, clientId);
try {
ResponseEntity response = restTemplate.postForEntity(
rateLimiterUrl + "/check", request, RateLimitResponse.class);
return response.getBody().isAllowed();
} catch (HttpClientErrorException.TooManyRequests ex) {
return false;
}
}
}
```
## Rate Limit Rules Format
```sh
API: /api/users
METHOD: GET
ALGORITHM: TOKEN_BUCKET
LIMIT: 100
PERIOD: 60
TIME_UNIT: SECONDS
API: /api/orders
METHOD: POST
ALGORITHM: LEAKING_BUCKET
LIMIT: 30
PERIOD: 60
TIME_UNIT: SECONDS
```
## API Usage
- Check Rate Limit
```sh
POST /rate-limiter/api/v1/rate-limit/check
//Request Body:
{
"resourceId": "/api/users:GET",
"clientId": "user123"
}
//Response:
{
"allowed": true,
"limit": 100,
"remaining": 99,
"resetTime": 1646324568
}
```
- Get All Rules
```sh
GET /rate-limiter/api/v1/rate-limit/rules
```
- Reload Rules
```sh
POST /rate-limiter/api/v1/rate-limit/rules/reload
```