← All posts
Rate Limiting with a Token Bucket
A token bucket gives you smooth rate limits with burst tolerance in about a dozen lines. Here is the whole thing.
A token bucket allows short bursts while enforcing a steady long-run rate — exactly what most APIs want.
class TokenBucket {
private tokens: number;
constructor(private capacity: number, private refillPerSec: number, private last = Date.now()) {
this.tokens = capacity;
}
take(): boolean {
const now = Date.now();
this.tokens = Math.min(this.capacity, this.tokens + ((now - this.last) / 1000) * this.refillPerSec);
this.last = now;
if (this.tokens < 1) return false;
this.tokens -= 1;
return true;
}
}Per-user buckets belong in shared storage (Redis), not process memory, or each instance enforces its own separate limit.
More to read