Skip to content

prateek-code-22/rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Spring Boot Redis Rate Limiter (Token Bucket)

A distributed rate-limiting solution built with Spring Boot and Redis, implementing the Token Bucket Algorithm. This project functions as an API Gateway component that regulates traffic based on client identity (IP Address).

πŸš€ Overview

This project demonstrates a robust, scalable rate limiter that:

  • Uses Redis Lua Scripts to ensure atomicity in distributed environments.
  • Implements the Token Bucket algorithm for smooth traffic shaping.
  • Built on top of Spring Cloud Gateway for high-performance non-blocking I/O.
  • Provides fallback mechanisms for identifying clients through X-Forwarded-For headers.

πŸ— Project Structure

rate-limiter/
β”œβ”€β”€ src/main/java/com/app/ratelimiter/
β”‚   β”œβ”€β”€ config/               # Configuration (Redis, Gateway, Properties)
β”‚   β”œβ”€β”€ filter/               # Gateway Filter (Rate limiting logic)
β”‚   β”œβ”€β”€ service/              # Redis interaction & Lua script execution
β”‚   β”œβ”€β”€ controller/           # Health and status monitoring
β”‚   └── RateLimiterApplication.java
β”œβ”€β”€ src/main/resources/
β”‚   └── application.properties # System configuration
β”œβ”€β”€ mockServer.py              # Mock backend for testing
β”œβ”€β”€ quick_test.sh              # Performance / Functional test script
└── build.gradle               # Dependency management

πŸ›  Tech Stack

  • Java 21
  • Spring Boot 3.4.0
  • Spring Cloud Gateway
  • Redis (Jedis)
  • Lua (for atomic operations)
  • Gradle

βš™οΈ Configuration

Configure the rate limiter in src/main/resources/application.properties:

# Redis Configuration
spring.redis.host=localhost
spring.redis.port=6379

# Rate Limiter Tunables
rate-limiter.capacity=10       # Max tokens in the bucket
rate-limiter.refill-rate=1     # Tokens added per second
rate-limiter.timeout=5000      # Request timeout
rate-limiter.api-server-url=http://localhost:8081 # Target Backend

🧠 How it Works (Token Bucket Algorithm)

  1. Request Interception: The TokenBucketRateLimiterFilter intercepts every incoming request.
  2. Client Identification: It extracts the client's IP from the X-Forwarded-For header or direct remote address.
  3. Atomic Check: It executes a Lua script on Redis:
    • Calculates the tokens to refill based on the time elapsed since the last request.
    • Checks if at least 1 token is available.
    • If available, decrements the token and returns 1 (Allowed).
    • If empty, returns 0 (Rate Limited).
  4. Response:
    • Success (200 OK): Forwards the request to the backend and adds X-RateLimit headers.
    • Failure (429 Too Many Requests): Returns a JSON error response.

🚦 Getting Started

1. Prerequisites

  • Redis server running on localhost:6379.
  • JDK 17 or higher.

2. Run the Mock Backend

Since the rate limiter acts as a gateway, you need a backend server to forward requests to:

python mockServer.py

3. Run the Rate Limiter

./gradlew bootRun

4. Test the API

Use the provided script to simulate traffic:

bash quick_test.sh

Or use curl:

curl -v http://localhost:8080/api/resource

πŸ“Š Monitoring & Health

The gateway provides internal endpoints for monitoring:

  • GET /gateway/health: Check if the gateway is up.
  • GET /gateway/rate-limit/status: View current token status for your IP.

πŸ“‘ Monitoring Headers

The application injects the following headers into every redirected response:

  • X-RateLimit-Limit: Maximum bucket capacity.
  • X-RateLimit-Remaining: Tokens currently available for the client.

About

A distributed rate-limiting solution built with Spring Boot and Redis, implementing the Token Bucket Algorithm.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors