What is Jitter?
Jitter is the technique of adding randomness to retry timing. Instead of all rate-limited clients retrying at exactly the same time, jitter spreads retries across a time window. This prevents the thundering herd problem where synchronized retries cause traffic spikes.The Thundering Herd Problem
Without jitter, rate-limited clients retry simultaneously:- Network congestion: Burst of simultaneous requests
- Database contention: High OCC conflicts
- Resource spikes: CPU and memory usage peaks
- Cascading failures: The spike might trigger more rate limits
Solution: Add Jitter to Retries
Add randomness to the retry delay:Jitter Strategies
1. Full Jitter
Randomize across the entire retry window:2. Proportional Jitter
Add jitter proportional to the wait time:3. Decorrelated Jitter
Use the previous retry time to calculate the next:Fixed Window: Automatic Jitter
Thefixed window strategy includes built-in jitter for the window start time:
For the fixed window, we also introduce randomness by picking the start time of the window (from which all subsequent windows are based) randomly if config.start wasn’t provided. This helps from all clients flooding requests at midnight and paging you.From the source code (
shared.ts:146-149):
Custom Window Start
You can specify an exact window start time to control reset timing:Client-Side Retry Pattern
Implementing jittered retries on the client:Server-Side Retry with Scheduler
Usectx.scheduler with jitter for server-side retries:
Jitter vs Reservations
Use Jitter When:
- You want clients to retry independently
- The order of operations doesn’t matter
- You’re okay with some operations failing
- You need simple retry logic
Use Reservations When:
- You need guaranteed execution
- Order matters (fair queueing)
- You can’t afford operation failures
- You’re dealing with large batch operations
Complete Example
Best Practices
- Always add jitter: Don’t rely on clients retrying at the same time
- Use appropriate jitter size: Balance between spreading load and user experience
- Combine with exponential backoff: For repeated failures, increase delay exponentially
- Consider using reservations: For critical operations that must succeed
- Monitor retry patterns: Track jitter effectiveness in your metrics
Jitter is most effective when combined with other rate limiting strategies. See Sharding for handling high throughput and Reservations for preventing starvation.