Skip to content

Rate limiting is enforced per-instance, multiplying the documented quota by replica count #787

Description

@fejilaup-cloud

Complexity rank: 8 of 15
Priority: High
Selection criteria satisfied: 2 (correctness under real deployment)
Scope tier: Standard

Description:
docs/api-reference.md:1101-1127 documents a production rate-limit policy (global/IP/user tiers, backoff) with no caveat about deployment topology. api-server/src/rate_limit.rs:137-153 holds Store { ips, users, violations, user_tiers } in Arc<Mutex<Store>> owned per RateLimitMiddleware instance — the doc comment at :147-148 even frames per-instance independence as intentional ("Instances are application-owned, making tests and multiple server instances independent"). Behind a load balancer running N replicas, a client whose requests are distributed round-robin effectively gets N× the documented per-IP or per-user quota, since each replica enforces its own bucket with no shared counter. The existing test concurrent_requests_cannot_overspend_bucket (rate_limit.rs:512) only proves thread-safety within a single instance — it cannot and does not prove cross-instance enforcement.

Tasks:

  • Replace the per-process Store (or add an alternate backend) with a shared counter store (e.g. Redis-backed token buckets using atomic INCR/Lua scripts) so limits are enforced across all replicas consistently.
  • Preserve the existing token-bucket algorithm's semantics (burst allowance, refill rate, tiering) — only the counter storage location changes.
  • Keep the current in-process store available as a fallback/test-only mode, clearly labeled as not safe for multi-instance deployment.
  • Add a test proving two independent RateLimitMiddleware instances pointed at the same shared backend correctly enforce one combined quota for the same client, not two independent ones.
  • Add a test proving the existing single-instance behavior (thread-safety, tiering, backoff) is unchanged.
  • Preserve set_user_tier and all other public API surface.
  • Confirm docs/api-reference.md:1101-1127 explicitly states the deployment assumption (shared backend required for multi-instance correctness) rather than leaving it implicit.

Metadata

Metadata

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third Campaign

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions