Skip to content

Feat/redis rate limiter#343

Open
AugistineCreates wants to merge 4 commits into
ChainForgee:mainfrom
AugistineCreates:feat/redis-rate-limiter
Open

Feat/redis rate limiter#343
AugistineCreates wants to merge 4 commits into
ChainForgee:mainfrom
AugistineCreates:feat/redis-rate-limiter

Conversation

@AugistineCreates

@AugistineCreates AugistineCreates commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Closes #212

This PR migrates the process-local Map rate limiter inside createRateLimiter middleware to a Redis Sorted Set (ZSET) sliding-window rate limiter, allowing the backend to scale across multiple replicas, share state, and maintain limits through process restarts.

Changes

  1. Redis Sliding Window Middleware: Implemented sliding-window logic using a Redis MULTI transaction containing ZREMRANGEBYSCORE, ZADD, ZRANGE (with scores to retrieve the oldest request timestamp for header reset computation), ZCARD, and EXPIRE per unique IP client key.
  2. NestJS integration: Passed the NestJS container's configured RedisService to the middleware factory in both main.ts and security.e2e-spec.ts.
  3. Robust Mocking:
    • Mapped @prisma/client to a lightweight proxy mock during test compilation to support local offline runs without requiring connections to external Prisma engine binaries.
    • Refactored the test suite to bypass bootstrapping the heavy AppModule context (with BullMQ workers/queues, database pools, and scheduler jobs), solving local open handles and thread hangs.
  4. E2E Testing:
    • Added E2E tests validating that 100 hits in 1 second result in 80+ requests returning 429 Too Many Requests along with the correct rate-limiting headers.
    • Added a fail-open verification test ensuring that when Redis goes down, the middleware issues a warning log and permits requests to flow through instead of returning a 500 Internal Server Error.

@AugistineCreates
AugistineCreates force-pushed the feat/redis-rate-limiter branch from 5635106 to ef1122c Compare July 16, 2026 07:46

Copy link
Copy Markdown
Contributor

Hi 👋 Appreciate this PR! The CI is flagging a failing check — could you take a peek at the failed job and push a fix? Happy to help if you need a hand 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace in-process Map-based rate limiter with a Redis ZSET sliding-window

2 participants