Skip to content

feat: add Redis-backed shared rate limiting with in-memory fallback - #477

Merged
Nsanjayboruds merged 1 commit into
Nsanjayboruds:mainfrom
bh462007:feature/463-redis-rate-limiting
Jul 19, 2026
Merged

feat: add Redis-backed shared rate limiting with in-memory fallback#477
Nsanjayboruds merged 1 commit into
Nsanjayboruds:mainfrom
bh462007:feature/463-redis-rate-limiting

Conversation

@bh462007

Copy link
Copy Markdown
Contributor

Description

Fixes #463

Implemented Redis-backed shared rate limiting with a graceful in-memory fallback.

The application previously relied on express-rate-limit's default in-memory store, which caused rate limit counters to reset after server restarts and prevented consistent enforcement across multiple server instances.

This change adds optional Redis support while keeping local development behavior unchanged.

Changes Made

  • Added Redis-backed storage using rate-limit-redis when REDIS_URL is configured.
  • Added Redis client initialization with a single shared connection.
  • Updated all existing rate limiters to use Redis stores:
    • globalIpLimiter
    • authIpLimiter
    • otpIpLimiter
    • botIpLimiter
    • userRateLimiter
    • adminRateLimiter
  • Added unique Redis key prefixes for each limiter to prevent counter collisions.
  • Added graceful fallback to express-rate-limit's default in-memory store when:
    • REDIS_URL is not configured
    • Redis connection fails
    • Redis authentication fails
  • Added optional REDIS_URL documentation in .env.example.

Implementation Details

  • Redis connection is attempted once during startup.
  • Disabled infinite reconnect attempts to avoid unnecessary logs when Redis configuration is invalid.
  • Existing rate limits, windows, and response behavior remain unchanged.

Testing Done

Verified locally with a real Upstash Redis instance:

  • ✅ Confirmed in-memory fallback works without REDIS_URL.
  • ✅ Confirmed invalid Redis configuration does not crash the application.
  • ✅ Confirmed Redis-backed rate limiting works when Redis is available.
  • ✅ Verified Redis keys use separate prefixes for different limiters.
  • ✅ Confirmed unrelated routes continue working normally.

Backward Compatibility

No breaking changes.

Local development continues working without requiring Redis configuration.

Related Issue

Closes #463

Copilot AI review requested due to automatic review settings July 18, 2026 13:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Nsanjayboruds
Nsanjayboruds merged commit a19907d into Nsanjayboruds:main Jul 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Add Redis-backed shared rate limiting with graceful in-memory fallback

3 participants