Skip to content

rahul07bagul/Design-Rate-Limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

High Level Design

HLD

Class Diagram

LLD

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

@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<RateLimitResponse> response = restTemplate.postForEntity(
                rateLimiterUrl + "/check", request, RateLimitResponse.class);
                
            return response.getBody().isAllowed();
        } catch (HttpClientErrorException.TooManyRequests ex) {
            return false;
        }
    }
}

Rate Limit Rules Format

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
    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
    GET /rate-limiter/api/v1/rate-limit/rules
  • Reload Rules
    POST /rate-limiter/api/v1/rate-limit/rules/reload