A distributed rate limiter service built with Go, featuring Redis-backed storage and load balancing capabilities. The service also includes monitoring with Prometheus and Grafana.
- Redis-backed sliding window rate limiting algorithm
- Distributed architecture with load balancing
- Prometheus metrics and Grafana dashboards
- Docker containerization
- Comprehensive test suite (unit, integration, and load tests)
- Configurable rate limits and time windows
- Health checks and automatic backend failover
The system consists of several components:
- Rate Limiter Service: Implements the core rate limiting logic using a sliding window algorithm
- Load Balancer: Distributes traffic across multiple rate limiter instances
- Redis: Stores rate limiting data and enables distributed coordination
- Prometheus: Collects and stores metrics
- Grafana: Visualizes metrics and provides monitoring dashboards
- Docker and Docker Compose
- Go 1.22 or later (for local development)
- Redis (automatically handled by Docker Compose)
-
Clone the repository:
git clone https://github.com/stemitom/rate-limiter.git cd rate-limiter
-
Start the services:
./run.sh
This will start:
- Two rate limiter instances (
::8081
,::8082
) - Load balancer (
::8080
) - Redis (
::6379
) - Prometheus (
::9090
) - Grafana (
::3000
)
The following environment variables can be configured:
PORT
: Service port (default: 8081)REDIS_ADDR
: Redis address (default: localhost:6379)RATE_LIMIT
: Requests per window (default: 10)WINDOW_SIZE
: Time window duration (default: 1s)
BACKEND_1_URL
: First backend URLBACKEND_2_URL
: Second backend URLBACKEND_1_WEIGHT
: Traffic weight for first backendBACKEND_2_WEIGHT
: Traffic weight for second backend
Run the test suite:
./scripts/test.sh
This will execute:
- Unit tests
- Integration tests
- Load tests
The project includes a load testing tool that can be used to benchmark the rate limiter:
go run cmd/loadtest/main.go -rps 100 -duration 10s -url http://localhost:8080
Parameters:
-rps
: Requests per second-duration
: Test duration-url
: Target URL
- Grafana Dashboard: http://localhost:3000 (default credentials: admin/admin)
- Prometheus: http://localhost:9090
- Metrics endpoints:
- Rate Limiter: http://localhost:8081/metrics
- Load Balancer: http://localhost:8080/metrics
GET /
: Main endpoint for rate-limited requestsGET /health
: Health check endpointGET /metrics
: Prometheus metrics endpoint
This project is licensed under the MIT License - see the LICENSE file for details.