imperugo / imperugo/StackExchange.Redis.Extensions

Add Redis-backed Rate Limiter (.NET 8+)

Open
#645 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
628
Forks
177
PR merge metrics
No merged PRs in 30d

Description

Summary

Implement System.Threading.RateLimiting.RateLimiter backed by Redis for distributed rate limiting across multiple app instances.

Problem

.NET 8's RateLimiting middleware is in-memory by default. In a multi-instance deployment, each instance has its own counter — so a limit of 100 req/min becomes 100×N. Redis-backed rate limiting solves this.

Proposed Algorithms

Fixed Window
services.AddRedisFixedWindowRateLimiter("api", options =>
{
    options.Window = TimeSpan.FromMinutes(1);
    options.PermitLimit = 100;
});
Sliding Window
services.AddRedisSlidingWindowRateLimiter("api", options =>
{
    options.Window = TimeSpan.FromMinutes(1);
    options.PermitLimit = 100;
    options.SegmentsPerWindow = 6;
});

Implementation

  • Use `StringIncrementAsync` with TTL for fixed window
  • Use Lua script for sliding window (atomic check-and-increment)
  • Consider a separate package: `StackExchange.Redis.Extensions.RateLimiting`

Considerations

  • SE.Redis 2.12 has `StringGcraRateLimitAsync` — a built-in GCRA algorithm
  • Depends on #634 (StringIncrementAsync)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with issue #634 and the existing Redis APIs, especially StringIncrementAsync and the SE.Redis 2.12 GCRA option. Compare the proposed fixed-window and sliding-window entry points, including their package boundary and atomicity requirements. Done means distributed rate limits work across multiple app instances for both proposed algorithms.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, redis
Domain
backend-api-design, distributed-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.