Upstash rate limiting
@repo/rate-limit protects your API routes from abuse using Upstash Ratelimit backed by Upstash Redis. Use this for anything running in Next.js (API routes, server actions, middleware).
apps/api/routes/chat.ts
slidingWindow:
apps/api/routes/auth.ts
apps/api/lib/cache.ts
Convex rate limiting
For rate limiting inside Convex functions (mutations, queries, actions), use the@convex-dev/rate-limiter component. It runs inside the Convex transaction, so limits roll back if your mutation fails.
Install
packages/backend/convex/convex.config.ts
Define limits
packages/backend/convex/rateLimits.ts
- Token bucket: tokens accumulate over time, allowing bursts up to
capacity. - Fixed window: hard limit per time period, resets each window.
Use in mutations
packages/backend/convex/chat/streaming.ts
throws option raises a ConvexError when the limit is exceeded. Without it, limit() returns { ok, retryAfter } for manual handling.
You can also check without consuming (rateLimiter.check) and reset limits after events like successful login (rateLimiter.reset).