Overview
While global rate limits apply to all requests, per-user rate limits allow you to enforce limits specific to individual users, sessions, teams, or any other identifier.Using the key Parameter
Pass akey to limit() to create an isolated rate limit for that specific key:
Per-User Rate Limiting
The most common pattern is to rate limit actions per authenticated user:Real Example from Source Code
Here’s the actual example from the Convex Rate Limiter repository:Per-Session Rate Limiting
For rate limiting based on browser sessions or devices:Per-Team Rate Limiting
For multi-tenant applications where rate limits apply per team or organization:Combining with Authentication
Common patterns for authenticated vs anonymous users:Fallback to Anonymous
Stricter Limits for Anonymous Users
- Separate Rate Limits
- Custom Count
Key Best Practices
Use stable identifiers
Use stable identifiers
Keys should be stable and unique. Good choices:
- User IDs from your auth system
- Session IDs
- Team/organization IDs
- IP addresses (for anonymous users)
- Usernames (can change)
- Email addresses (can change)
- Display names
Sanitize user-provided keys
Sanitize user-provided keys
If the key comes from user input, validate it:
Consider key cardinality
Consider key cardinality
Each unique key creates a separate rate limit bucket in the database. Be mindful of:
- How many unique keys you’ll have
- Whether old keys can be cleaned up
- Storage implications for high-cardinality keys
Testing Different Keys
From the source code’s test suite:Next Steps
- Learn about Custom Counts for non-uniform consumption
- Understand Error Handling with
throws - Explore Checking Limits without consuming tokens