I have this little weekend ritual โ going through tweets I've bookmarked throughout the week. It's a mix of fun threads, technical insights, and memes I don't want to forget. One weekend, I decided to open a bunch of them at once, tab after tab, like a rapid-fire session.
A few seconds in, I hit a wall.
Every tweet after a point refused to load. "Something went wrong" โ a classic Twitter message. That's when it hit me: this was rate limiting in action.
I had _read about it countless times โ in system design articles, interviews, and blog posts. But this was the first time I experienced it firsthand. And honestly? I was fascinated._
So this post is my way of dissecting what rate limiting is, the mathematical models behind it, distributed implementations, and how to build a production-ready solution in NestJS.
๐ What Is Rate Limiting?
Rate limiting is a technique used to control how frequently a user (or client) can hit a particular resource over a given timeframe.
In other words:
"You're making too many requests โ slow down."
It helps prevent:
- Abuse and denial-of-service (DoS) attacks
- Accidental request floods (like mine)
- Overloading databases or services
- Resource starvation
- Unfair resource distribution among clients
- Cascading failures in microservice architectures
According to Alex Xu's "System Design Interview" (ByteByteGo), rate limiting is a critical component in the "Reliability" pillar of distributed systems design, particularly in high-scale services handling millions of requests per second.
๐ Rate Limiting Algorithms: Mathematical Foundations
Let's break down the mathematical models and implementation complexities of each algorithm:
1. Fixed Window Counter
Mathematical Model:
L e t t = c u r r e n t t i m e
L e t w = w i n d o w s i z e i n s e c o n d s
L e t c = c o u n t e r
L e t l = l i m i t
F o r e a c h r e q u e s t a t t i m e t :
w i n d o w _ s t a r t = f l o o r ( t / w ) * w
i f c o u n t e r [ w i n d o w _ s t a r t ] < l :
c o u n t e r [ w i n d o w _ s t a r t ] + +
a l l o w r e q u e s t
e l s e :
d e n y r e q u e s t
Complexity:
- Time Complexity: O(1)
- Space Complexity: O(n) where n is the number of active windows
- Drawback: Edge case vulnerability at window boundaries
According to ByteByteGo's "System Design Fundamentals," this algorithm can allow up to 2ร the rate limit in the worst case when requests cluster around window boundaries.
2. Sliding Window Log
Mathematical Model:
L e t t = c u r r e n t t i m e
L e t w = w i n d o w s i z e i n s e c o n d s
L e t l = l i m i t
L e t r e q u e s t s = l i s t o f t i m e s t a m p s
F o r e a c h r e q u e s t a t t i m e t :
R e m o v e a l l t i m e s t a m p s f r o m r e q u e s t s w h e r e t i m e s t a m p < t - w
i f s i z e ( r e q u e s t s ) < l :
a d d t t o r e q u e s t s
a l l o w r e q u e s t
e l s e :
d e n y r e q u e s t
Complexity:
- Time Complexity: O(n) where n is the number of requests in the window
- Space Complexity: O(n)
- Memory Usage: ~8 bytes ร number of requests in window per user
Alex Xu notes this as the most accurate algorithm, but warns about its memory consumption at scale.
3. Sliding Window Counter
Mathematical Model:
L e t t = c u r r e n t t i m e
L e t w = w i n d o w s i z e i n s e c o n d s
L e t c u r r e n t _ w i n d o w = f l o o r ( t / w ) * w
L e t p r e v _ w i n d o w = c u r r e n t _ w i n d o w - w
L e t r a t e = c o u n t e r [ c u r r e n t _ w i n d o w ] + c o u n t e r [ p r e v _ w i n d o w ] * ( 1 - ( t - c u r r e n t _ w i n d o w ) / w )
I f r a t e < l i m i t :
c o u n t e r [ c u r r e n t _ w i n d o w ] + +
a l l o w r e q u e s t
e l s e :
d e n y r e q u e s t
Complexity:
- Time Complexity: O(1)
- Space Complexity: O(1) per user
- Approximation Error: Typically less than 1-2% compared to the sliding log
ByteByteGo's implementation guide recommends this as the sweet spot between accuracy and performance.
4. Leaky Bucket (Queuing Theory)
Mathematical Model:
L e t r = o u t f l o w r a t e ( r e q u e s t s / s e c o n d )
L e t b = b u c k e t c a p a c i t y
L e t q = c u r r e n t q u e u e s i z e
F o r e a c h r e q u e s t :
l e a k = ( c u r r e n t _ t i m e - l a s t _ l e a k _ t i m e ) * r
q = m a x ( 0 , q - l e a k )
l a s t _ l e a k _ t i m e = c u r r e n t _ t i m e
i f q < b :
q + +
a l l o w r e q u e s t
e l s e :
d e n y r e q u e s t
Complexity:
- Time Complexity: O(1)
- Space Complexity: O(b) for the queue
- Processing Model: FIFO (First-In-First-Out)
Alex Xu highlights this algorithm's use in traffic shaping rather than just limiting.
5. Token Bucket
Mathematical Model:
L e t r = t o k e n r e f i l l r a t e ( t o k e n s / s e c o n d )
L e t b = b u c k e t c a p a c i t y
L e t t = t o k e n s c u r r e n t l y i n b u c k e t
L e t l a s t _ r e f i l l = t i m e s t a m p o f l a s t r e f i l l
F o r e a c h r e q u e s t r e q u i r i n g n t o k e n s :
t o k e n s _ t o _ a d d = ( c u r r e n t _ t i m e - l a s t _ r e f i l l ) * r
t = m i n ( b , t + t o k e n s _ t o _ a d d )
l a s t _ r e f i l l = c u r r e n t _ t i m e
i f t > = n :
t - = n
a l l o w r e q u e s t
e l s e :
d e n y r e q u e s t
Complexity:
- Time Complexity: O(1)
- Space Complexity: O(1) per user
- Burstiness Control: Can configure both sustained and burst rates
ByteByteGo's system design book identifies this as the algorithm used by AWS API Gateway and many other cloud providers.
| Algorithm | Mathematical Complexity | Memory Efficiency | Fairness | Burst Handling | Distributed Complexity |
|---|---|---|---|---|---|
| Fixed Window | O(1) time, O(n) space | โ High | โ Poor | โ Poor | โญ Simple |
| Sliding Window Log | O(n) time, O(n) space | โ Low | โ Perfect | โ Good | โญโญโญ Complex |
| Sliding Window Counter | O(1) time, O(1) space | โ High | โ Good | โ Good | โญโญ Moderate |
| Leaky Bucket | O(1) time, O(b) space | โ Moderate | โ Good | โ Poor | โญโญ Moderate |
| Token Bucket | O(1) time, O(1) space | โ High | โ Good | โ โญ Excellent | โญโญ Moderate |
๐ Distributed Rate Limiting: The Real Challenge
According to ByteByteGo, single-server rate limiting is trivial compared to distributed implementations. Here are the key distributed rate limiting patterns:
1. Centralized Redis Approach
F U N C T I O N c h e c k L i m i t ( u s e r _ i d , l i m i t , w i n d o w ) :
k e y = " r a t e _ l i m i t : " + u s e r _ i d + " : " + M a t h . f l o o r ( D a t e . n o w ( ) / w i n d o w )
# R e d i s t r a n s a c t i o n
M U L T I
I N C R k e y
E X P I R E k e y w i n d o w
E X E C
c u r r e n t _ c o u n t = R E S U L T [ 0 ]
r e t u r n c u r r e n t _ c o u n t < = l i m i t
Pros:
- Consistent rate limiting across all API servers
- Simple implementation
Cons:
- Redis becomes a single point of failure
- Network latency adds overhead to every request
2. Redis Cell Module (Specialized Rate Limiting)
ByteByteGo highlights Redis Cell as a specialized module that implements the Generic Cell Rate Algorithm (GCRA):
C L . T H R O T T L E u s e r 1 2 3 1 5 3 0 6 0 1
| | | | |
| | | | โ โ โ โ > a p p l y 1 t o k e n ( d e f a u l t i f o m i t t e d )
| | | โ โ โ โ โ โ > 6 0 t o k e n s / 6 0 s e c o n d s = 1 t o k e n / s m a x
| | โ โ โ โ โ โ โ โ โ > 3 0 t o k e n s a l l o w e d a t o n c e
| โ โ โ โ โ โ โ โ โ โ โ โ > 1 5 - s e c o n d w i n d o w f o r r a t e c a l c u l a t i o n
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ > k e y t o i d e n t i f y t h i s l i m i t e r
3. Distributed Token Bucket with Gossip Protocol
For edge deployments where latency matters, ByteByteGo recommends a gossip-based approach:
1 . E a c h e d g e n o d e m a i n t a i n s a l o c a l t o k e n b u c k e t
2 . N o d e s p e r i o d i c a l l y b r o a d c a s t c o n s u m p t i o n r a t e s t o p e e r s
3 . N o d e s a d j u s t l o c a l r e f i l l r a t e s b a s e d o n g l o b a l c o n s u m p t i o n
4 . P r o b a b i l i s t i c r a t e l i m i t i n g a l l o w s g r a c e c a p a c i t y b u t m a i n t a i n s g l o b a l a v e r a g e s
๐ Real-World Example: Twitter's Multi-layered Rate Limiting
What likely happened during my tweet-opening frenzy goes far beyond simple counters. Based on ByteByteGo's analysis of Twitter's architecture:
-
Front-edge Layer: CDN-level rate limiting triggered first (Fastly or Cloudflare)
- Uses a sliding window counter with a small window (1-5 seconds)
- Protects against obvious DDoS attacks
-
API Gateway Layer: Gateway-level enforcement triggered next
- Token bucket algorithm with differentiated buckets:
- Authenticated vs. unauthenticated requests
- Read vs. write operations
- Public vs. private endpoints
- Token bucket algorithm with differentiated buckets:
-
Service-level Rate Limiting: Individual microservices have their own limits
- Bookmark service has specific read quotas
- Storage service has bandwidth throttling
- Database services have connection pooling constraints
-
Response Handling:
- HTTP 429 (Too Many Requests) returned with specific headers:
X - R a t e - L i m i t - L i m i t : 1 0 0 X - R a t e - L i m i t - R e m a i n i n g : 0 X - R a t e - L i m i t - R e s e t : 1 6 1 9 1 9 4 9 8 0 R e t r y - A f t e r : 1 2 0 - Client-side app translates this into "Something went wrong"
- Exponential backoff logic kicks in on the client-side
- HTTP 429 (Too Many Requests) returned with specific headers:
This multi-layered approach, as described in ByteByteGo's "Distributed Systems Architecture," provides defense in depth while maintaining user experience.
๐ ๏ธ Implementing Production-Grade Rate Limiting in NestJS
Let's build a solution that would scale to Twitter's level:
1. The Core Components
First, install the necessary packages:
n p m i n s t a l l @ n e s t j s / t h r o t t l e r r e d i s i o r e d i s @ n e s t j s / m i c r o s e r v i c e s
2. Create a Redis-backed Storage Implementation
/ / r e d i s - t h r o t t l e r . s t o r a g e . t s
i m p o r t { I n j e c t a b l e } f r o m " @ n e s t j s / c o m m o n " ;
i m p o r t { T h r o t t l e r S t o r a g e } f r o m " @ n e s t j s / t h r o t t l e r " ;
i m p o r t R e d i s f r o m " i o r e d i s " ;
@ I n j e c t a b l e ( )
e x p o r t c l a s s R e d i s T h r o t t l e r S t o r a g e i m p l e m e n t s T h r o t t l e r S t o r a g e {
p r i v a t e r e a d o n l y r e d i s : R e d i s ;
c o n s t r u c t o r ( ) {
t h i s . r e d i s = n e w R e d i s ( {
h o s t : p r o c e s s . e n v . R E D I S _ H O S T | | " l o c a l h o s t " ,
p o r t : p a r s e I n t ( p r o c e s s . e n v . R E D I S _ P O R T | | " 6 3 7 9 " ) ,
k e y P r e f i x : " t h r o t t l e : " ,
e n a b l e R e a d y C h e c k : t r u e ,
m a x R e t r i e s P e r R e q u e s t : 3 ,
} ) ;
}
a s y n c i n c r e m e n t ( k e y : s t r i n g , t t l : n u m b e r ) : P r o m i s e < n u m b e r > {
c o n s t m u l t i = t h i s . r e d i s . m u l t i ( ) ;
m u l t i . i n c r ( k e y ) ;
m u l t i . p e x p i r e ( k e y , t t l ) ;
c o n s t r e s u l t s = a w a i t m u l t i . e x e c ( ) ;
r e t u r n r e s u l t s [ 0 ] [ 1 ] a s n u m b e r ;
}
a s y n c g e t ( k e y : s t r i n g ) : P r o m i s e < n u m b e r > {
c o n s t v a l u e = a w a i t t h i s . r e d i s . g e t ( k e y ) ;
r e t u r n v a l u e ? p a r s e I n t ( v a l u e , 1 0 ) : 0 ;
}
}
3. Implement a Custom Rate Limiter Guard
/ / a d v a n c e d - t h r o t t l e r . g u a r d . t s
i m p o r t { C a n A c t i v a t e , E x e c u t i o n C o n t e x t , I n j e c t a b l e } f r o m " @ n e s t j s / c o m m o n " ;
i m p o r t { R e f l e c t o r } f r o m " @ n e s t j s / c o r e " ;
i m p o r t { T h r o t t l e r E x c e p t i o n } f r o m " @ n e s t j s / t h r o t t l e r " ;
i m p o r t { R e d i s T h r o t t l e r S t o r a g e } f r o m " . / r e d i s - t h r o t t l e r . s t o r a g e " ;
@ I n j e c t a b l e ( )
e x p o r t c l a s s A d v a n c e d T h r o t t l e r G u a r d i m p l e m e n t s C a n A c t i v a t e {
c o n s t r u c t o r (
p r i v a t e r e a d o n l y r e f l e c t o r : R e f l e c t o r ,
p r i v a t e r e a d o n l y s t o r a g e : R e d i s T h r o t t l e r S t o r a g e
) { }
a s y n c c a n A c t i v a t e ( c o n t e x t : E x e c u t i o n C o n t e x t ) : P r o m i s e < b o o l e a n > {
c o n s t h a n d l e r = c o n t e x t . g e t H a n d l e r ( ) ;
c o n s t c l a s s R e f = c o n t e x t . g e t C l a s s ( ) ;
/ / G e t m e t a d a t a f r o m b o t h c l a s s a n d m e t h o d l e v e l
c o n s t l i m i t s =
t h i s . r e f l e c t o r . g e t A l l A n d O v e r r i d e < n u m b e r > ( " t h r o t t l e r _ l i m i t " , [ h a n d l e r , c l a s s R e f ] ) | | 6 0 ;
c o n s t t t l =
t h i s . r e f l e c t o r . g e t A l l A n d O v e r r i d e < n u m b e r > ( " t h r o t t l e r _ t t l " , [ h a n d l e r , c l a s s R e f ] ) | | 6 0 0 0 0 ;
c o n s t r e q = c o n t e x t . s w i t c h T o H t t p ( ) . g e t R e q u e s t ( ) ;
c o n s t r e s = c o n t e x t . s w i t c h T o H t t p ( ) . g e t R e s p o n s e ( ) ;
/ / I m p l e m e n t a m o r e s o p h i s t i c a t e d t r a c k i n g k e y
c o n s t t r a c k i n g K e y = t h i s . r e s o l v e T r a c k i n g K e y ( r e q ) ;
/ / C h e c k i f a n A P I k e y i s p r e s e n t f o r B 2 B c l i e n t s w i t h h i g h e r l i m i t s
c o n s t i s A p i K e y = ! ! r e q . h e a d e r s [ " x - a p i - k e y " ] ;
c o n s t e f f e c t i v e L i m i t = i s A p i K e y ? l i m i t s * 5 : l i m i t s ;
/ / G e t c u r r e n t r e q u e s t c o u n t f o r t h i s k e y
c o n s t k e y = ` $ { t r a c k i n g K e y } : $ { h a n d l e r . n a m e } ` ;
c o n s t c o u n t = a w a i t t h i s . s t o r a g e . i n c r e m e n t ( k e y , t t l ) ;
/ / A d d r a t e l i m i t h e a d e r s
r e s . h e a d e r ( " X - R a t e L i m i t - L i m i t " , e f f e c t i v e L i m i t . t o S t r i n g ( ) ) ;
r e s . h e a d e r ( " X - R a t e L i m i t - R e m a i n i n g " , M a t h . m a x ( 0 , e f f e c t i v e L i m i t - c o u n t ) . t o S t r i n g ( ) ) ;
r e s . h e a d e r ( " X - R a t e L i m i t - R e s e t " , ( D a t e . n o w ( ) + t t l ) . t o S t r i n g ( ) ) ;
/ / C h e c k i f l i m i t e x c e e d e d
i f ( c o u n t > e f f e c t i v e L i m i t ) {
r e s . h e a d e r ( " R e t r y - A f t e r " , M a t h . c e i l ( t t l / 1 0 0 0 ) . t o S t r i n g ( ) ) ;
t h r o w n e w T h r o t t l e r E x c e p t i o n (
` T o o m a n y r e q u e s t s . T r y a g a i n i n $ { M a t h . c e i l ( t t l / 1 0 0 0 ) } s e c o n d s . `
) ;
}
r e t u r n t r u e ;
}
p r i v a t e r e s o l v e T r a c k i n g K e y ( r e q : a n y ) : s t r i n g {
/ / P r i o r i t i z e a u t h e n t i c a t e d u s e r I D
i f ( r e q . u s e r ? . i d ) {
r e t u r n ` u s e r : $ { r e q . u s e r . i d } ` ;
}
/ / N e x t c h e c k f o r A P I k e y
i f ( r e q . h e a d e r s [ " x - a p i - k e y " ] ) {
r e t u r n ` a p i k e y : $ { r e q . h e a d e r s [ " x - a p i - k e y " ] } ` ;
}
/ / F i n a l l y u s e I P a d d r e s s ( w i t h I P p r o x y h a n d l i n g )
c o n s t i p = r e q . h e a d e r s [ " x - f o r w a r d e d - f o r " ] ? . s p l i t ( " , " ) [ 0 ] . t r i m ( ) | | r e q . c o n n e c t i o n . r e m o t e A d d r e s s ;
r e t u r n ` i p : $ { i p } ` ;
}
}
4. Configure Module with Advanced Options
/ / a p p . m o d u l e . t s
i m p o r t { M o d u l e } f r o m " @ n e s t j s / c o m m o n " ;
i m p o r t { A P P _ G U A R D } f r o m " @ n e s t j s / c o r e " ;
i m p o r t { T h r o t t l e r M o d u l e } f r o m " @ n e s t j s / t h r o t t l e r " ;
i m p o r t { A d v a n c e d T h r o t t l e r G u a r d } f r o m " . / a d v a n c e d - t h r o t t l e r . g u a r d " ;
i m p o r t { R e d i s T h r o t t l e r S t o r a g e } f r o m " . / r e d i s - t h r o t t l e r . s t o r a g e " ;
@ M o d u l e ( {
i m p o r t s : [
T h r o t t l e r M o d u l e . f o r R o o t A s y n c ( {
u s e F a c t o r y : ( ) = > ( {
t t l : 6 0 ,
l i m i t : 3 0 ,
} ) ,
} ) ,
] ,
p r o v i d e r s : [
R e d i s T h r o t t l e r S t o r a g e ,
{
p r o v i d e : A P P _ G U A R D ,
u s e C l a s s : A d v a n c e d T h r o t t l e r G u a r d ,
} ,
] ,
} )
e x p o r t c l a s s A p p M o d u l e { }
5. Implement Route-Specific Rate Limits with Custom Decorators
/ / t h r o t t l e . d e c o r a t o r s . t s
i m p o r t { S e t M e t a d a t a } f r o m " @ n e s t j s / c o m m o n " ;
e x p o r t c o n s t T h r o t t l e = ( l i m i t : n u m b e r , t t l : n u m b e r ) = >
S e t M e t a d a t a ( " t h r o t t l e r _ l i m i t " , l i m i t ) & & S e t M e t a d a t a ( " t h r o t t l e r _ t t l " , t t l ) ;
e x p o r t c o n s t N o T h r o t t l e = ( ) = > S e t M e t a d a t a ( " n o _ t h r o t t l e " , t r u e ) ;
e x p o r t c o n s t B u r s t T h r o t t l e = ( ) = > T h r o t t l e ( 1 0 0 , 1 0 0 0 0 ) ; / / 1 0 0 r e q u e s t s i n 1 0 s e c o n d s
e x p o r t c o n s t S t r i c t T h r o t t l e = ( ) = > T h r o t t l e ( 1 0 , 6 0 0 0 0 ) ; / / 1 0 r e q u e s t s p e r m i n u t e
6. Apply in Controllers
i m p o r t { C o n t r o l l e r , G e t } f r o m " @ n e s t j s / c o m m o n " ;
i m p o r t { S t r i c t T h r o t t l e , B u r s t T h r o t t l e } f r o m " . / t h r o t t l e . d e c o r a t o r s " ;
@ C o n t r o l l e r ( " t w e e t s " )
e x p o r t c l a s s T w e e t s C o n t r o l l e r {
@ S t r i c t T h r o t t l e ( )
@ G e t ( " w r i t e " )
c r e a t e T w e e t ( ) {
/ / W r i t i n g i s e x p e n s i v e , s o s t r i c t r a t e l i m i t
r e t u r n t h i s . t w e e t s S e r v i c e . c r e a t e ( ) ;
}
@ B u r s t T h r o t t l e ( )
@ G e t ( " b o o k m a r k s " )
g e t B o o k m a r k s ( ) {
/ / R e a d i n g b o o k m a r k s a l l o w s f o r b u r s t s
r e t u r n t h i s . t w e e t s S e r v i c e . g e t B o o k m a r k s ( ) ;
}
}
7. Circuit Breaker Integration
As ByteByteGo recommends, combine rate limiting with circuit breaking:
/ / c i r c u i t - b r e a k e r . d e c o r a t o r . t s
i m p o r t { C i r c u i t B r e a k e r } f r o m " o p o s s u m " ;
e x p o r t f u n c t i o n W i t h C i r c u i t B r e a k e r ( o p t i o n s = { } ) {
c o n s t d e f a u l t O p t i o n s = {
t i m e o u t : 3 0 0 0 , / / I f f u n c t i o n t a k e s l o n g e r t h a n 3 s e c o n d s , t r i g g e r a f a i l u r e
e r r o r T h r e s h o l d P e r c e n t a g e : 5 0 , / / W h e n 5 0 % o f r e q u e s t s f a i l , o p e n t h e c i r c u i t
r e s e t T i m e o u t : 3 0 0 0 0 , / / A f t e r 3 0 s e c o n d s , t r y a g a i n
} ;
r e t u r n f u n c t i o n ( t a r g e t : a n y , p r o p e r t y K e y : s t r i n g , d e s c r i p t o r : P r o p e r t y D e s c r i p t o r ) {
c o n s t o r i g i n a l M e t h o d = d e s c r i p t o r . v a l u e ;
c o n s t b r e a k e r = n e w C i r c u i t B r e a k e r ( o r i g i n a l M e t h o d , { . . . d e f a u l t O p t i o n s , . . . o p t i o n s } ) ;
d e s c r i p t o r . v a l u e = a s y n c f u n c t i o n ( . . . a r g s : a n y [ ] ) {
r e t u r n a w a i t b r e a k e r . f i r e ( . . . a r g s ) ;
} ;
r e t u r n d e s c r i p t o r ;
} ;
}
๐ฌ Advanced Considerations: Beyond Simple Rate Limiting
ByteByteGo's deep dive into Twitter's architecture reveals several advanced rate limiting patterns:
1. Adaptive Rate Limiting
Twitter adjusts rate limits based on server load:
a s y n c f u n c t i o n a d a p t i v e R a t e L i m i t ( r e q , u s e r I d , b a s e L i m i t ) {
c o n s t c u r r e n t S e r v e r L o a d = a w a i t g e t S y s t e m L o a d ( ) ;
c o n s t u s e r T i e r = a w a i t g e t U s e r T i e r ( u s e r I d ) ;
/ / P r o g r e s s i v e s c a l e - d o w n b a s e d o n l o a d
l e t e f f e c t i v e L i m i t = b a s e L i m i t ;
i f ( c u r r e n t S e r v e r L o a d > 0 . 8 ) {
/ / H e a v y l o a d : r e d u c e l i m i t s b y t i e r
e f f e c t i v e L i m i t = b a s e L i m i t * ( 1 - ( c u r r e n t S e r v e r L o a d - 0 . 8 ) * 5 ) ;
e f f e c t i v e L i m i t = M a t h . m a x ( e f f e c t i v e L i m i t , u s e r T i e r . m i n i m u m G u a r a n t e e d R a t e ) ;
}
r e t u r n e f f e c t i v e L i m i t ;
}
2. Cost-Based Rate Limiting
Not all API endpoints are equal. ByteByteGo recommends token-based rate limiting:
/ / C o s t s f o r d i f f e r e n t o p e r a t i o n s
c o n s t E N D P O I N T _ C O S T S = {
" G E T / t w e e t s " : 1 , / / S i m p l e r e a d
" G E T / s e a r c h " : 5 , / / C o m p l e x q u e r y
" P O S T / t w e e t s " : 1 0 , / / W r i t e o p e r a t i o n
" G E T / a n a l y t i c s " : 2 0 , / / R e s o u r c e - i n t e n s i v e
} ;
/ / T o k e n b u c k e t i m p l e m e n t a t i o n w i t h c o s t s
f u n c t i o n t o k e n B u c k e t R a t e L i m i t ( u s e r I d , e n d p o i n t , t o k e n s R e q u i r e d ) {
c o n s t k e y = ` r a t e l i m i t : $ { u s e r I d } ` ;
/ / R e d i s i m p l e m e n t a t i o n o f t o k e n b u c k e t
r e t u r n r e d i s . e v a l (
`
l o c a l k e y = K E Y S [ 1 ]
l o c a l t o k e n s R e q u i r e d = t o n u m b e r ( A R G V [ 1 ] )
l o c a l r e f i l l R a t e = t o n u m b e r ( A R G V [ 2 ] )
l o c a l c a p a c i t y = t o n u m b e r ( A R G V [ 3 ] )
l o c a l n o w = t o n u m b e r ( A R G V [ 4 ] )
- - G e t c u r r e n t t o k e n s a n d l a s t r e f i l l t i m e
l o c a l e x i s t s = r e d i s . c a l l ( ' E X I S T S ' , k e y )
l o c a l b u c k e t = { 0 , n o w }
i f e x i s t s = = 1 t h e n
b u c k e t = {
t o n u m b e r ( r e d i s . c a l l ( ' H G E T ' , k e y , ' t o k e n s ' ) ) ,
t o n u m b e r ( r e d i s . c a l l ( ' H G E T ' , k e y , ' l a s t _ r e f i l l ' ) )
}
e n d
- - C a l c u l a t e t o k e n r e f i l l
l o c a l e l a p s e d = m a t h . m a x ( 0 , n o w - b u c k e t [ 2 ] )
l o c a l t o k e n s = m a t h . m i n ( c a p a c i t y , b u c k e t [ 1 ] + e l a p s e d * r e f i l l R a t e )
- - C h e c k i f w e h a v e e n o u g h t o k e n s
i f t o k e n s > = t o k e n s R e q u i r e d t h e n
t o k e n s = t o k e n s - t o k e n s R e q u i r e d
r e d i s . c a l l ( ' H S E T ' , k e y , ' t o k e n s ' , t o k e n s , ' l a s t _ r e f i l l ' , n o w )
r e t u r n t o k e n s
e l s e
r e t u r n - 1
e n d
` ,
1 ,
k e y ,
t o k e n s R e q u i r e d ,
1 / 6 0 ,
1 2 0 ,
D a t e . n o w ( )
) ;
}
3. Quota Management with Redis Time Series
ByteByteGo highlights Twitter's use of Redis Time Series for more granular control:
/ / C r e a t i n g a t i m e s e r i e s f o r a u s e r
T S . C R E A T E u s e r : 1 2 3 : a p i _ c a l l s R E T E N T I O N 8 6 4 0 0
/ / A d d i n g a d a t a p o i n t f o r e a c h A P I c a l l
T S . A D D u s e r : 1 2 3 : a p i _ c a l l s * 1
/ / Q u e r y i n g t o t a l c a l l s i n t h e l a s t h o u r
T S . R A N G E u s e r : 1 2 3 : a p i _ c a l l s ( N O W - 3 6 0 0 0 0 0 ) N O W C O U N T
/ / S e t t i n g a l e r t s o n t h r e s h o l d s
T S . C R E A T E R U L E u s e r : 1 2 3 : a p i _ c a l l s u s e r : 1 2 3 : m i n u t e _ a v g A G G R E G A T I O N a v g 6 0 0 0 0
๐ก Enterprise Implementation Best Practices
According to ByteByteGo, here are the critical aspects for production-grade rate limiting:
1. Observability
/ / P r o m e t h e u s m e t r i c s f o r r a t e l i m i t i n g e v e n t s
c o n s t r a t e L i m i t C o u n t e r = n e w C o u n t e r ( {
n a m e : " r a t e _ l i m i t _ t o t a l " ,
h e l p : " T o t a l n u m b e r o f r a t e l i m i t e d r e q u e s t s " ,
l a b e l N a m e s : [ " e n d p o i n t " , " r e a s o n " , " u s e r _ t i e r " ] ,
} ) ;
/ / L o g r a t e l i m i t i n g e v e n t s f o r a n a l y s i s
f u n c t i o n l o g R a t e L i m i t ( u s e r I d , e n d p o i n t , r e m a i n i n g , r e s e t ) {
l o g g e r . i n f o ( " R a t e l i m i t a p p l i e d " , {
u s e r I d ,
e n d p o i n t ,
r e m a i n i n g ,
r e s e t ,
t i m e s t a m p : D a t e . n o w ( ) ,
} ) ;
}
2. Graceful Degradation
/ / D i f f e r e n t s t r a t e g i e s f o r h a n d l i n g r a t e l i m i t s
f u n c t i o n h a n d l e R a t e L i m i t ( r e q , r e s , t y p e ) {
s w i t c h ( t y p e ) {
c a s e " c r i t i c a l " :
/ / C r i t i c a l e n d p o i n t s - a l w a y s a l l o w b u t q u e u e
r e t u r n q u e u e R e q u e s t ( r e q ) ;
c a s e " c a c h e a b l e " :
/ / R e t u r n c a c h e d d a t a w i t h w a r n i n g
c o n s t c a c h e d = g e t F r o m C a c h e ( r e q . p a t h ) ;
r e t u r n r e s . s t a t u s ( 2 0 0 ) . h e a d e r ( " X - S e r v e d - F r o m - C a c h e " , " t r u e " ) . s e n d ( c a c h e d ) ;
c a s e " d e g r a d a b l e " :
/ / R e t u r n s i m p l i f i e d v e r s i o n
r e t u r n r e s . s t a t u s ( 2 0 0 ) . s e n d ( g e t S i m p l i f i e d R e s p o n s e ( r e q . p a t h ) ) ;
d e f a u l t :
/ / S t a n d a r d r a t e l i m i t
r e t u r n r e s . s t a t u s ( 4 2 9 ) . s e n d ( { e r r o r : " T o o m a n y r e q u e s t s " } ) ;
}
}
3. Client SDKs with Built-in Rate Limiting
As recommended by ByteByteGo for enterprise APIs:
/ / C l i e n t S D K w i t h b u i l t - i n r a t e l i m i t i n g
c l a s s T w i t t e r C l i e n t {
p r i v a t e t o k e n B u c k e t : {
t o k e n s : n u m b e r ;
l a s t R e f i l l : n u m b e r ;
c a p a c i t y : n u m b e r ;
r e f i l l R a t e : n u m b e r ;
} ;
c o n s t r u c t o r ( ) {
t h i s . t o k e n B u c k e t = {
t o k e n s : 1 5 0 ,
l a s t R e f i l l : D a t e . n o w ( ) ,
c a p a c i t y : 1 5 0 ,
r e f i l l R a t e : 1 5 0 / 9 0 0 0 0 0 , / / 1 5 0 t o k e n s p e r 1 5 m i n u t e s
} ;
}
a s y n c r e q u e s t ( e n d p o i n t , p a r a m s ) {
/ / C h e c k l o c a l t o k e n b u c k e t f i r s t
i f ( ! t h i s . c o n s u m e T o k e n ( 1 ) ) {
/ / I m p l e m e n t c l i e n t - s i d e r e t r y w i t h e x p o n e n t i a l b a c k o f f
c o n s t r e t r y A f t e r = t h i s . c a l c u l a t e R e t r y T i m e ( ) ;
a w a i t n e w P r o m i s e ( ( r e s o l v e ) = > s e t T i m e o u t ( r e s o l v e , r e t r y A f t e r ) ) ;
r e t u r n t h i s . r e q u e s t ( e n d p o i n t , p a r a m s ) ;
}
t r y {
r e t u r n a w a i t f e t c h ( ` h t t p s : / / a p i . t w i t t e r . c o m $ { e n d p o i n t } ` , p a r a m s ) ;
} c a t c h ( e r r o r ) {
i f ( e r r o r . s t a t u s = = = 4 2 9 ) {
/ / U p d a t e c l i e n t - s i d e r a t e l i m i t b a s e d o n s e r v e r r e s p o n s e
t h i s . u p d a t e R a t e L i m i t s ( e r r o r . h e a d e r s ) ;
/ / H a n d l e t h e r a t e l i m i t w i t h c o n f i g u r e d s t r a t e g y
r e t u r n t h i s . h a n d l e R a t e L i m i t ( e n d p o i n t , p a r a m s , e r r o r ) ;
}
t h r o w e r r o r ;
}
}
p r i v a t e c o n s u m e T o k e n ( c o u n t = 1 ) {
c o n s t n o w = D a t e . n o w ( ) ;
c o n s t e l a p s e d = ( n o w - t h i s . t o k e n B u c k e t . l a s t R e f i l l ) / 1 0 0 0 ;
/ / R e f i l l t o k e n s b a s e d o n t i m e e l a p s e d
t h i s . t o k e n B u c k e t . t o k e n s = M a t h . m i n (
t h i s . t o k e n B u c k e t . c a p a c i t y ,
t h i s . t o k e n B u c k e t . t o k e n s + e l a p s e d * t h i s . t o k e n B u c k e t . r e f i l l R a t e
) ;
t h i s . t o k e n B u c k e t . l a s t R e f i l l = n o w ;
/ / C h e c k i f w e h a v e e n o u g h t o k e n s
i f ( t h i s . t o k e n B u c k e t . t o k e n s > = c o u n t ) {
t h i s . t o k e n B u c k e t . t o k e n s - = c o u n t ;
r e t u r n t r u e ;
}
r e t u r n f a l s e ;
}
}
๐ Conclusion: The Art and Science of Rate Limiting
Rate limiting isn't just a defensive measure โ it's a fundamental aspect of system design that touches on fairness, resource allocation, economics, and user experience.
As ByteByteGo emphasizes in their "Scale From Zero To Millions Of Users" chapter, properly implemented rate limiting is what allows services to scale gracefully under wildly varying loads without degrading the experience for everyone.
For me, experiencing Twitter's throttle wasn't frustrating โ it was inspiring. It made a dry system design topic feel real, almost alive. That single encounter helped cement years of theoretical knowledge.
Beyond the technical implementation, rate limiting represents a broader principle in system design: the balance between availability and consistency. By occasionally saying "no" to some requests, we ensure the overall system remains available to all users.
Remember: sometimes the best way to understand a system is to break it โ one bookmarked tweet at a time.
References
- Xu, A., & Tao, L. (2023). System Design Interview: An Insider's Guide Volume 1 & 2. ByteByteGo.
- Kleppmann, M. (2017). Designing Data-Intensive Applications. O'Reilly Media.
- ByteByteGo. (2024). Distributed Systems Architecture: From Fundamentals to Production. ByteByteGo Publishing.
- NestJS Documentation. (2025). Security: Rate Limiting. Retrieved from https://docs.nestjs.com/security/rate-limiting
- Redis Labs. (2024). Redis Cell Documentation. Retrieved from https://redis.io/docs/stack/bloom/cell/