Skip to content

fix(rate-limit): add distributed Redis-backed rate limiting - #441

Merged
AnuranjanJain merged 8 commits into
AnuranjanJain:mainfrom
Namraa310806:fix/distributed-rate-limiting
Jun 27, 2026
Merged

AnuranjanJain merged 8 commits into
AnuranjanJain:mainfrom
Namraa310806:fix/distributed-rate-limiting

Conversation

@Namraa310806

Copy link
Copy Markdown
Contributor

Summary

This PR upgrades the application's rate-limiting architecture from process-local enforcement to distributed Redis-backed enforcement, ensuring rate limits remain consistent across multiple workers and production deployments.

Previously, rate limiting relied on in-memory storage, allowing limits to be bypassed in multi-worker environments because each worker maintained its own independent state.

This PR activates and validates the existing distributed rate-limiting infrastructure, adds Redis support, improves configuration, and introduces comprehensive test coverage for concurrency and failure scenarios.


Root Cause

The project already contained Redis-based distributed rate-limiting infrastructure inside backend/utils/limiter.py, including:

  • RedisStorage
  • LimiterStorageProxy
  • Automatic Redis fallback support

However:

  • Redis was not included in requirements.txt
  • Redis configuration was undocumented
  • Authentication endpoints were not enforcing their rate limits
  • Multi-worker deployments commonly operated using process-local storage
  • Distributed enforcement was not validated through testing

As a result, rate limits could be bypassed by distributing requests across multiple application workers.


Changes Made

Distributed Rate Limiting

  • Added Redis dependency to project requirements.
  • Enabled documented Redis-backed rate limiting via REDIS_URL.
  • Preserved automatic fallback to in-memory storage when Redis is unavailable.
  • Maintained backward compatibility with existing deployments.

Authentication Security Improvements

Integrated existing authentication rate-limiting controls into:

  • /auth/login
  • /auth/signup
  • /auth/resend-verification

Added enforcement for:

  • Login rate limiting
  • Failed login tracking
  • Account lockout protection
  • Signup abuse prevention
  • Verification email abuse prevention

Configuration Improvements

Added Redis configuration examples and documentation to:

  • .env.example
  • backend/.env.example

Documented:

REDIS_URL=redis://localhost:6379/0

and related rate-limiting settings.


Testing

Added comprehensive test coverage for distributed rate limiting.

New Test Suites

Distributed Rate Limiting

  • Redis-backed storage behavior
  • Shared state enforcement
  • Storage fallback behavior
  • Configuration handling

Concurrency Testing

  • Multi-worker scenarios
  • Concurrent request handling
  • Shared limit enforcement
  • Thread safety validation

Failure Scenarios

  • Redis outages
  • Connection failures
  • Storage fallback behavior
  • Recovery scenarios

Results

  • Existing tests passing
  • Distributed rate-limiting tests passing
  • Concurrency tests passing
  • Failure scenario tests passing

Impact

Before

  • Rate limits enforced independently per worker.
  • Multi-worker deployments could bypass configured limits.
  • Authentication endpoints lacked active rate-limit enforcement.
  • Distributed enforcement was undocumented and unverified.

After

  • Rate limits can be enforced globally across all workers using Redis.
  • Authentication endpoints actively enforce abuse protection.
  • Redis-backed distributed storage is fully documented.
  • Graceful fallback preserves functionality when Redis is unavailable.
  • Production deployments can scale horizontally without weakening rate limits.

Security Benefits

This PR improves protection against:

  • Brute-force attacks
  • Credential stuffing
  • Signup abuse
  • Verification email spam
  • Multi-worker rate-limit bypasses
  • High-volume automated abuse

Fixes: #419

Files Modified

Configuration

  • requirements.txt
  • .env.example
  • backend/.env.example

Authentication

  • backend/routers/auth_routes.py

Tests

  • backend/tests/test_distributed_rate_limit.py
  • backend/tests/test_rate_limit_concurrency.py
  • backend/tests/test_rate_limit_failure_scenarios.py

Documentation

  • docs/distributed-rate-limiting-refactor.md

Backward Compatibility

  • No API changes
  • No frontend changes
  • No database migrations
  • Existing deployments continue working
  • Redis remains optional
  • Automatic fallback to in-memory storage preserved

Checklist

  • Added Redis dependency
  • Enabled distributed rate limiting
  • Documented Redis configuration
  • Added multi-worker enforcement coverage
  • Added concurrency testing
  • Added Redis failure testing
  • Enforced authentication rate limiting
  • Preserved backward compatibility
  • No breaking API changes
  • Production-ready implementation

@vercel

vercel Bot commented Jun 16, 2026

Copy link
Copy Markdown

@Namraa310806 is attempting to deploy a commit to the WizardKing's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderashhar coderashhar left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Kindly resolve the failing checks

@Namraa310806

Copy link
Copy Markdown
Contributor Author

@coderashhar Done

@vercel

vercel Bot commented Jun 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
legal-ease Ready Ready Preview, Comment Jun 27, 2026 7:36am

@AnuranjanJain AnuranjanJain added GSSoC'26 gssoc:approved Approved for GSSOC contribution level:advanced Complex task type:security Security related issue type:devops Infrastructure and CI/CD labels Jun 27, 2026
@AnuranjanJain
AnuranjanJain merged commit f5c148a into AnuranjanJain:main Jun 27, 2026
14 checks passed
@Namraa310806
Namraa310806 deleted the fix/distributed-rate-limiting branch June 30, 2026 21:01

This branch was successfully deployed

1 active deployment
Preview — 0938dd75 Deployed Jun 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Approved for GSSOC contribution GSSoC'26 level:advanced Complex task type:devops Infrastructure and CI/CD type:security Security related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rate Limiter Uses Process-Local Storage and Does Not Enforce Global Limits Across Multiple Workers

3 participants