Skip to content

A robust and scalable API Rate Limiter implementation in .NET Core, supporting 4 algorithms (Fixed Window, Sliding Window, Token Bucket, Concurrency) with IP-based rate limiting. Built using Clean Architecture and integrated with Redis for distributed environments.

Notifications You must be signed in to change notification settings

MrEshboboyev/api-rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🚀 API Rate Limiter - .NET 9

.NET 9 Redis Clean Architecture Rate Limiting

A powerful and scalable API Rate Limiter built with .NET 9, supporting multiple rate-limiting algorithms (Fixed Window, Sliding Window, Token Bucket, Concurrency) with IP-based rate limiting. Designed using Clean Architecture and integrated with Redis for distributed environments.


🌟 Features

Multiple Rate-Limiting Algorithms:

  • Fixed Window 🕒 – Limits requests per fixed time window.
  • Sliding Window 🔄 – Smooths request spikes with a rolling window.
  • Token Bucket 🪙 – Allows burstable traffic via token-based management.
  • Concurrency 🔗 – Restricts simultaneous client connections.

IP-Based Rate Limiting 📡 – Ensures fair request distribution across users. ✅ Redis Integration 🛢️ – Enables scalable, distributed rate limiting. ✅ Clean Architecture 🏗️ – Layered design following best practices. ✅ Fully Configurable 🔧 – Manage rate limits in appsettings.json.


🛠️ Technologies Used

Technology Purpose
.NET 9 Core framework
Redis Distributed caching & rate limiting
StackExchange.Redis Redis client for .NET
MediatR CQRS pattern implementation
Serilog Logging
xUnit Unit testing framework
Moq Mocking for unit tests

🏗️ Clean Architecture Overview

📌 This project follows Clean Architecture principles:

📂 src/
 ├── 📁 App             # Runnable and installers located web api
 ├── 📁 Domain          # Business logic & entities
 ├── 📁 Application     # Use cases, handlers, services
 ├── 📁 Infrastructure  # Redis integration & rate-limiting logic
 ├── 📁 Persistence     # EF Core integration & implementations
 ├── 📁 Presentation    # API controllers & contracts

✨ Benefits of Clean Architecture

  • Separation of Concerns – Organized and maintainable code.
  • Scalability – Easily extendable for new features.
  • Testability – Isolated business logic for robust unit tests.

⚡ How It Works

1️⃣ Fixed Window Algorithm

  • Restricts requests within a fixed time period.
  • Example: 100 requests per minute.

2️⃣ Sliding Window Algorithm

  • Maintains a rolling window to smooth traffic bursts.
  • Example: Consider last 60 seconds instead of resetting counters.

3️⃣ Token Bucket Algorithm

  • Allows controlled bursts of traffic.
  • Example: Users get 100 tokens, refilling at 10/sec.

4️⃣ Concurrency Algorithm

  • Limits the number of simultaneous API requests.
  • Example: Max 10 concurrent users.

5️⃣ IP-Based Rate Limiting

  • Identifies and restricts request flow per IP address.
  • Ensures fair resource allocation across users.

🚀 Getting Started

📝 Prerequisites

.NET 9 SDK
Redis
Visual Studio Code
Postman (for API testing)

🔧 Installation & Setup

1️⃣ Clone the Repository

 git clone https://github.com/MrEshboboyev/api-rate-limiter.git
 cd src

2️⃣ Install Dependencies

 dotnet restore

3️⃣ Run Redis in Docker

 docker run -d -p 6379:6379 redis

4️⃣ Run the Application

 dotnet run --project src/App

🔧 Configuration

Modify appsettings.json to configure rate limits:

{
  "RateLimitSettings": {
    "FixedWindow": {
      "PermitLimit": 100,
      "WindowInSeconds": 60
    },
    "SlidingWindow": {
      "PermitLimit": 100,
      "WindowInSeconds": 60
    },
    "TokenBucket": {
      "BucketSize": 100,
      "RefillRate": 10
    },
    "Concurrency": {
      "MaxConcurrentRequests": 10
    }
  }
}

📡 API Endpoints

Method Endpoint Description
GET /api/rate-limit/get-random Enabled endpoint for rate limiting
POST /api/rate-limit/get-random-two Disabled endpoint for rate limiting

🧪 Testing

🔍 Unit Tests

Run tests to validate the functionality:

 dotnet test

🌐 API Testing (Postman/cURL)

Send multiple requests to trigger rate limiting:

 curl -X GET http://localhost:5000/api/rate-limit/test

✅ If rate limit is exceeded, you’ll get 429 Too Many Requests.


🤝 Contributing

Contributions are welcome! 🎉 If you find any issues or have suggestions, feel free to open an issue or submit a pull request.


📜 License

This project is MIT Licensed. See the LICENSE file for details.


🎉 Acknowledgments

Special thanks to:

  • The .NET and Redis communities for excellent tools.
  • Clean Architecture & Domain-Driven Design inspirations.

✉️ Contact

📬 GitHub: MrEshboboyev
📬 Email: [email protected]


Star this repository if you found it helpful! 🚀

About

A robust and scalable API Rate Limiter implementation in .NET Core, supporting 4 algorithms (Fixed Window, Sliding Window, Token Bucket, Concurrency) with IP-based rate limiting. Built using Clean Architecture and integrated with Redis for distributed environments.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages