Skip to content

Anshikakalpana/rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

40 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Rate Limiter API (Microservice)

Node.js TypeScript Express.js Redis Docker Lua

License: MIT PRs Welcome Maintenance

A high-performance, Dockerized rate limiting microservice built with Node.js, TypeScript, Redis, Lua scripting, and Redis Cluster, implementing multiple real-world algorithms used in API Gateways.


๐Ÿ“‘ Table of Contents


โญ Features

โœ” Implements four industry-standard algorithms:

  • Fixed Window
  • Sliding Window
  • Token Bucket
  • Leaky Bucket

โœ” Redis Cluster support for high scalability
โœ” Atomic operations using Lua
โœ” /metrics endpoint aggregates: allowedRequests, blockedRequests, tokensRemaining, resetTime, totalRequests
โœ” Dockerized microservice
โœ” Basic Auth (for testing)
โœ” Modular folder structure

๐Ÿ”ฎ Future Additions

  • Sliding Window Log algorithm
  • Load balancing layer
  • API Keys & RBAC (Redis-only possible)
  • Middleware-level caching
  • Gateway features:
    • rate limiting
    • authentication
    • load balancing
    • circuit breaking
  • Prometheus/Grafana dashboards
  • Distributed tracing (OpenTelemetry)

๐Ÿ›  Tech Stack

Node.js โ€ข TypeScript โ€ข Express.js โ€ข Redis / Redis Cluster โ€ข Lua โ€ข Zod โ€ข Docker & Docker Compose


๐Ÿงฉ Architecture Overview

Client โ†’ API Gateway (future) โ†’ Rate Limiter Service โ†’ Redis / Redis Cluster
  • Each algorithm uses:
    • Dedicated Redis keys
    • Atomic Lua scripts
    • Isolated logic for dashboard comparison

๐Ÿง  Rate Limiting Algorithms (Full Details)

1๏ธโƒฃ Fixed Window Algorithm

The Fixed Window algorithm assigns a fixed number of allowed requests inside a fixed time window.

โœ” How it Works

  • Example: 10 requests per 60 seconds
  • Requests exceeding limit โ†’ blocked
  • Counter resets when next window starts

โŒ Major Issue โ€” Burst Problem

  • 10 requests at 59th sec + 10 at 1st sec of next window โ†’ 20 requests in 2 sec โ†’ possible overload
  • Reason: tracks only current window, not the last 60 seconds
2๏ธโƒฃ Token Bucket Algorithm

Stores requests as tokens in a bucket.

โœ” How it Works

  • Bucket has fixed capacity
  • Tokens refill at a fixed rate
  • Each request consumes 1 token
  • If tokens exist โ†’ request allowed, else blocked

โœ” Burst Handling

  • Supports bursts up to bucket capacity
  • Smooth traffic control

โœ” Example

  • Capacity = 10 tokens, Refill = 1 token/sec
  • 10 requests โ†’ allowed
  • 11th โ†’ blocked
  • After 1 sec โ†’ 1 token refills โ†’ allowed
3๏ธโƒฃ Leaky Bucket Algorithm

Ensures constant output rate.

โœ” How it Works

  • Requests enter a queue (bucket)
  • Processed at fixed leak rate
  • If bucket full โ†’ request rejected

โœ” Characteristics

  • Smooth & uniform traffic
  • Prevents burst attacks
  • Protects server load

โŒ Limitation

  • No bursts allowed
  • Example: Leak rate = 5 req/sec โ†’ 100 requests arrive โ†’ only 5 processed/sec, rest queued/rejected
4๏ธโƒฃ Sliding Window Algorithm

Improves Fixed Window by tracking requests in the last N seconds, not fixed blocks.

โœ” How it Works

  • Fairer distribution
  • Prevents burst issues at window edges

๐Ÿ”ฅ API Endpoints

Method Endpoint Description
POST /api/limiter/test Fixed Window
POST /api/limiter/sliding Sliding Window
POST /api/limiter/tokenbucket Token Bucket
POST /api/limiter/leakybucket Leaky Bucket
POST /api/limiter/all Run all algorithms together
GET /api/limiter/metrics Get aggregated metrics

๐Ÿ“ฆ Example Response

{
  "activeKeys": 1,
  "response": {
    "allowed": true,
    "remaining": 9,
    "resetTime": 1703174400
  },
  "blockedRequests": 27,
  "allowedRequests": 67,
  "totalRequests": 94
}

๐Ÿณ Setup & Installation

# Clone the repository
git clone https://github.com/Anshikakalpana/rate-limiter
cd rate-limiter

# Start with Docker Compose
docker compose up --build

Starts:

  • Redis Cluster
  • Node.js server
  • Lua scripts loaded automatically

๐Ÿงช Testing & Load Scenarios

Test bursts, allowed vs blocked, algorithm comparison via /metrics

Tools: Postman Runner, k6, Artillery, JMeter


๐Ÿ“ฌ Postman Collection

Postman collection to test all endpoints


๐Ÿงญ Roadmap

  • Sliding Window Log algorithm
  • API Keys & RBAC (Redis-only)
  • Middleware caching
  • Load balancing
  • Circuit breaking
  • Prometheus/Grafana dashboards
  • Distributed tracing (OpenTelemetry)

๐Ÿ“„ License

MIT License โ€” free for personal & commercial use.


๐Ÿค Contributing

Contributions, issues, and feature requests are welcome!
Feel free to check the issues page.


โญ Show Your Support

Give a โญ๏ธ if this project helped you!


Made with โค๏ธ by Anshika Kalpana

About

Dockerized rate limiting microservice built with Node.js, TypeScript, Redis, Lua scripting, and Redis Cluster, implementing multiple real-world algorithms used in API Gateways.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors