Documentation Index
Fetch the complete documentation index at: https://mintlify.com/get-convex/rate-limiter/llms.txt
Use this file to discover all available pages before exploring further.
Why Sharding?
When many requests happen simultaneously, they all try to modify the same rate limit values in the database. Because Convex provides strong transactions, requests will never overwrite each other incorrectly, so your rate limiter will never allow more requests than configured. However, high contention for these values causes optimistic concurrency control (OCC) conflicts. Convex automatically retries these conflicts with backoff, but it’s better to avoid them in the first place.How Sharding Works
Sharding breaks up the total capacity into individual buckets, or “shards”. When consuming capacity, the rate limiter checks a random shard. While you might occasionally get rate limited when capacity exists elsewhere, you’ll never violate the rate limit’s upper bound.Power of Two Technique
The implementation uses an advanced load balancing technique: it checks two random shards and uses the one with more capacity. This keeps shards relatively balanced. The rate limiter will also combine the capacity of both shards if neither has enough on their own, based on the power of two choices technique.Calculating Optimal Shard Count
Use this formula to estimate the number of shards needed:Best Practices
- Each shard should have at least 5-10 capacity (ideally 10 or more)
- For the example above: 1000 rate / 10 shards = 100 capacity per shard ✓
- Don’t expect normal traffic to exceed ~20 QPS with this configuration
Scaling a Rate Limit
If you want a rate like{ rate: 100, period: SECOND } and you’re flexible on the overall period, you can shard it by increasing both the rate and period proportionally:
Trade-offs
When to Use Sharding
Use sharding when:- You expect high concurrent load (>10 QPS per rate limit)
- You’re experiencing OCC conflicts
- You need to scale beyond single-shard throughput
- Traffic is low (< 5 QPS)
- You need exact capacity guarantees at all times
- Each shard would have < 5 capacity