An open API service indexing awesome lists of open source software.

https://github.com/mreshboboyev/api-rate-limiter

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.
https://github.com/mreshboboyev/api-rate-limiter

api-security clean-architecture concurrency fixed-window ip-based-rate-limiting middleware rate-limiting redis security sliding-window throttling token-bucket

Last synced: 27 days ago
JSON representation

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.

Awesome Lists containing this project

README

          

# ๐Ÿš€ API Rate Limiter - .NET 9

![.NET 9](https://img.shields.io/badge/.NET%209-%F0%9F%92%AA-blue)
![Redis](https://img.shields.io/badge/Redis-%F0%9F%93%9A-red)
![Clean Architecture](https://img.shields.io/badge/Clean%20Architecture-%F0%9F%9A%80-green)
![Rate Limiting](https://img.shields.io/badge/Rate%20Limiting-%F0%9F%94%92-yellow)

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](https://dotnet.microsoft.com/download/dotnet/9.0)
โœ… [Redis](https://redis.io/download)
โœ… [Visual Studio Code](https://code.visualstudio.com/)
โœ… [Postman](https://www.postman.com/) (for API testing)

### ๐Ÿ”ง Installation & Setup

#### 1๏ธโƒฃ Clone the Repository
```sh
git clone https://github.com/MrEshboboyev/api-rate-limiter.git
cd src
```

#### 2๏ธโƒฃ Install Dependencies
```sh
dotnet restore
```

#### 3๏ธโƒฃ Run Redis in Docker
```sh
docker run -d -p 6379:6379 redis
```

#### 4๏ธโƒฃ Run the Application
```sh
dotnet run --project src/App
```

---

## ๐Ÿ”ง Configuration

Modify `appsettings.json` to configure rate limits:

```json
{
"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:
```sh
dotnet test
```

### ๐ŸŒ API Testing (Postman/cURL)
Send multiple requests to trigger rate limiting:
```sh
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](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](https://github.com/MrEshboboyev)
๐Ÿ“ฌ **Email**: mreshboboyev@gmail.com

---

โญ **Star this repository** if you found it helpful! ๐Ÿš€