Skip to content

feat: per-key rate limiting with configurable fail-closed mode#374

Merged
Baskarayelu merged 1 commit into
CredenceOrg:mainfrom
abrak01:feature/rate-limit-failclosed-perkey
May 29, 2026
Merged

feat: per-key rate limiting with configurable fail-closed mode#374
Baskarayelu merged 1 commit into
CredenceOrg:mainfrom
abrak01:feature/rate-limit-failclosed-perkey

Conversation

@abrak01
Copy link
Copy Markdown
Contributor

@abrak01 abrak01 commented May 28, 2026

  • Add per-API-key Redis fixed-window buckets (keyed by apiKeyRecord.id) alongside the existing per-tenant bucket; a request is rejected when either counter exceeds the tier ceiling, preventing one noisy key from exhausting the shared tenant budget.

  • Separate tier limit (tenant bucket) from per-key limit (key bucket): options.max overrides the key ceiling only; the tenant ceiling always comes from the tier config (maxFree/maxPro/maxEnterprise).

  • Emit rate_limit_rejected_total Prometheus counter with labels tier, key_id, and reason (tenant_limit | key_limit | redis_unavailable).

  • Make fail-closed the production default: RATE_LIMIT_FAIL_OPEN now defaults to false when NODE_ENV=production and true in dev/test. The catch-block fallback in src/app.ts mirrors this so a validateConfig failure at startup cannot silently disable limits.

  • Add getRedis injectable option to RateLimitConfig for test isolation (avoids vi.doMock + dynamic import fragility).

  • Extend tests: per-key isolation, key-B allowed when key-A blocked, remaining header reflects tighter budget, rate_limit_rejected_total counter assertions for all three reasons, fail-closed 503, getKeyId helper, disabled middleware.

  • Update docs/api.md (Rate Limiting section) and docs/security.md (architecture, fail-closed, Prometheus metric, env vars, security considerations).

Closes #335

- Add per-API-key Redis fixed-window buckets (keyed by apiKeyRecord.id)
  alongside the existing per-tenant bucket; a request is rejected when
  either counter exceeds the tier ceiling, preventing one noisy key from
  exhausting the shared tenant budget.

- Separate tier limit (tenant bucket) from per-key limit (key bucket):
  options.max overrides the key ceiling only; the tenant ceiling always
  comes from the tier config (maxFree/maxPro/maxEnterprise).

- Emit rate_limit_rejected_total Prometheus counter with labels
  tier, key_id, and reason (tenant_limit | key_limit | redis_unavailable).

- Make fail-closed the production default: RATE_LIMIT_FAIL_OPEN now
  defaults to false when NODE_ENV=production and true in dev/test.
  The catch-block fallback in src/app.ts mirrors this so a
  validateConfig failure at startup cannot silently disable limits.

- Add getRedis injectable option to RateLimitConfig for test isolation
  (avoids vi.doMock + dynamic import fragility).

- Extend tests: per-key isolation, key-B allowed when key-A blocked,
  remaining header reflects tighter budget, rate_limit_rejected_total
  counter assertions for all three reasons, fail-closed 503, getKeyId
  helper, disabled middleware.

- Update docs/api.md (Rate Limiting section) and docs/security.md
  (architecture, fail-closed, Prometheus metric, env vars, security
  considerations).

Closes CredenceOrg#335
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 28, 2026

@abrak01 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Baskarayelu Baskarayelu merged commit f8ca268 into CredenceOrg:main May 29, 2026
1 of 2 checks passed
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.

[Backend] Rate limiting: fail-closed option and per-key (not just per-tier) limits

2 participants