Skip to content

14.10 API Design & Recipient Rate Limiting ​

1. Token Bucket vs Leaky Bucket vs Sliding Window ​

AlgorithmSmooth Bursts?Memory FootprintEnforcement LocationFit for OmniPost
Token Bucket (Chosen)Yes (allows burst up to capacity)$O(1)$ per key (tokens, timestamp)queues/dispatcher.py:24Optimal for provider API quotas
Leaky BucketNo (enforces strict constant rate)$O(1)$Traffic shaping queuesBetter for egress smoothing
Sliding Window LogYes$O(N)$ (stores every timestamp)Redis Sorted SetsMemory intensive
Fixed Window CounterVulnerable to boundary spikes$O(1)$Counter cachePoor burst protection

2. Say It Out Loud ​

"We implement token bucket rate limiting in queues/dispatcher.py to protect upstream carrier quotas. The token bucket smoothly accommodates legitimate bursts of OTP requests while strictly bounding average delivery throughput to provider limits (such as Twilio's 100 SMS per second). Unlike fixed-window limiters, the token bucket eliminates traffic spikes at window boundaries."