Skip to content

feat: implement token bucket rate limiting with burst handling and ti…#294

Merged
Smartdevs17 merged 3 commits intoSmartdevs17:mainfrom
distributed-nerd:feat/token-bucket-rate-limiting
Apr 29, 2026
Merged

feat: implement token bucket rate limiting with burst handling and ti…#294
Smartdevs17 merged 3 commits intoSmartdevs17:mainfrom
distributed-nerd:feat/token-bucket-rate-limiting

Conversation

@distributed-nerd
Copy link
Copy Markdown
Contributor

feat: Token bucket rate limiting with burst handling and tier awareness

Replaces the fixed-window tieredRateLimit with a proper token bucket algorithm that supports burst handling, per-endpoint limits, user tier differentiation, and distributed state via Redis.

Changes

backend/src/middleware/rate-limit.ts (new)

  • Token bucket implementation with configurable capacity, refillRate, and burstAllowance per tier
  • Tokens refill continuously at refillRate/s up to capacity + burstAllowance
  • Redis path uses an atomic Lua script — safe for distributed/multi-instance deployments
  • Falls back to in-memory store on Redis failure (fail-open) with a console warning to handle clock skew and Redis outages gracefully
  • Sets X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-RateLimit-Policy, X-RateLimit-Tier headers on every response
  • Returns 429 with Retry-After header and structured error body when bucket is empty
  • In-memory ring-buffer analytics (recordAnalyticsEvent, getAnalyticsSummary)

backend/src/routes/rate-limit-analytics.ts (new)

  • GET /api/v1/rate-limit/analytics?windowMs=60000
  • Returns total/blocked request counts broken down by tier and endpoint

backend/src/index.ts

  • Removed old fixed-window tieredRateLimit and express-rate-limit invoice limiter
  • Wired tokenBucketRateLimit as the global /api/ middleware
  • Invoice route gets its own stricter bucket config via endpointConfig override
  • Registered analytics route under /api/v1/rate-limit

backend/.env.example

  • Added commented-out vars for REDIS_URL and per-tier burst/refill config

Default tier limits

Tier Capacity Refill Burst
free 60 1 tok/s 10
pro 300 5 tok/s 50
enterprise 1200 20 tok/s 200

Per-endpoint overrides

Endpoint Free capacity Pro capacity Enterprise capacity
/api/v1/invoice 10 60 300
/api/v1/verification 20 100 500
/api/v1/stellar 30 150 600

Edge cases handled

  • Redis failure — fails open, logs warning, continues with in-memory
  • Clock skew — refill uses elapsed ms delta, not wall-clock windows
  • Burst traffic — one-time burst allowance above capacity, then normal refill applies
  • Distributed instances — atomic Lua script ensures no race conditions on Redis

closes #224

…er awareness

- Add TokenBucketRateLimit middleware (backend/src/middleware/rate-limit.ts)
  - Configurable capacity, refill rate, and burst allowance per tier
  - In-memory store with Redis fallback via atomic Lua script
  - Fails open on Redis errors to avoid availability impact
- Per-endpoint configs for /invoice, /verification, /stellar
- User tier differentiation: free / pro / enterprise
- X-RateLimit-* headers + Retry-After on 429 responses
- Ring-buffer analytics with getAnalyticsSummary()
- Add GET /api/v1/rate-limit/analytics endpoint
- Replace fixed-window tieredRateLimit in index.ts with token bucket
- Update .env.example with Redis and burst config vars
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 24, 2026

@shaaibu7 is attempting to deploy a commit to the smartdevs17's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 24, 2026

@distributed-nerd 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

@Smartdevs17
Copy link
Copy Markdown
Owner

⚠️ Merge conflicts detected

Hi @distributed-nerd,

This PR has merge conflicts that must be resolved before it can be merged.

To fix:

  1. Sync with the latest main branch: git fetch origin && git merge origin/main
  2. Resolve the conflicts in your code
  3. Push the resolved changes

Once resolved, the PR will be automatically merged on the next run.


🤖 Drips Wave Review Agent

2 similar comments
@Smartdevs17
Copy link
Copy Markdown
Owner

⚠️ Merge conflicts detected

Hi @distributed-nerd,

This PR has merge conflicts that must be resolved before it can be merged.

To fix:

  1. Sync with the latest main branch: git fetch origin && git merge origin/main
  2. Resolve the conflicts in your code
  3. Push the resolved changes

Once resolved, the PR will be automatically merged on the next run.


🤖 Drips Wave Review Agent

@Smartdevs17
Copy link
Copy Markdown
Owner

⚠️ Merge conflicts detected

Hi @distributed-nerd,

This PR has merge conflicts that must be resolved before it can be merged.

To fix:

  1. Sync with the latest main branch: git fetch origin && git merge origin/main
  2. Resolve the conflicts in your code
  3. Push the resolved changes

Once resolved, the PR will be automatically merged on the next run.


🤖 Drips Wave Review Agent

@gitguardian
Copy link
Copy Markdown

gitguardian Bot commented Apr 29, 2026

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
32145778 Triggered Generic Password aaed709 backend/src/relayer/tests/signature.test.ts View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@distributed-nerd
Copy link
Copy Markdown
Contributor Author

@Smartdevs17 conflicts resolved

@Smartdevs17 Smartdevs17 merged commit 20456fc into Smartdevs17:main Apr 29, 2026
8 of 17 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.

Security: Implement Advanced Rate Limiting with Token Bucket Algorithm

3 participants