Skip to content

feat: migrate proxy rate limiter from in-memory Map to shared store (Redis/KV) #363

@coderabbitai

Description

@coderabbitai

Summary

The current rate limiting implementation in src/proxy.ts uses a module-scoped Map (rateLimitMap) to track request counts per client identifier. Because this is process-local, rate limit counters are not shared across server instances or replicas, making it easy to bypass limits in multi-instance/horizontal deployments.

Problem

  • rateLimitMap is isolated to a single Node.js process
  • Restarts reset all counters
  • Requests routed to different replicas are each rate-limited independently, so a client can exceed the intended threshold by the number of replicas

Proposed Solution

Migrate rateLimitMap to a shared store such as Redis or a Next.js KV provider:

  • Initialize a single Redis/KV client used by the proxy handler
  • Store per-identifier counters and expiry in the shared store
  • Use atomic increment + TTL operations (e.g., INCR + EXPIRE) to implement count/resetTime logic
  • Update checkRateLimit and shouldBypassRateLimit to read/write from the shared store

References

Raised by @Producdevity.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions