An open API service indexing awesome lists of open source software.

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

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.


Java
Spring Boot
MySQL
Redis

## Demo
![Demo](https://github.com/rahul07bagul/Rate-limiter/blob/main/assets/Rate_Limiter.gif)

## High Level Design
![HLD](https://github.com/rahul07bagul/Rate-limiter/blob/main/assets/design.png)

## Class Diagram
![LLD](https://github.com/rahul07bagul/Rate-limiter/blob/main/assets/uml_diagram.png)

## 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
```