API Rate Limiting: Complete Implementation Guide for Developers

Rate limiting is the unsung hero of API stability. Too lenient, and a single misbehaving client can take down your entire infrastructure. Too strict, and you alienate legitimate users and break integrations.

๐Ÿ”
Recommended

๐Ÿ” Protect your API keys from abuse

1Password allows you to securely store and share API credentials across your dev team while maintaining strict access control.

Try 1Password Free โ†’

Whether you're building a public API for thousands of developers or an internal microservice architecture, implementing a robust rate limiting strategy is non-negotiable for production reliability.

Staff Pick

๐Ÿ“ก Monitor your APIs โ€” know when they go down before your users do

Better Stack checks uptime every 30 seconds with instant Slack, email & SMS alerts. Free tier available.

Start Free โ†’

Affiliate link โ€” we may earn a commission at no extra cost to you

What is API Rate Limiting?

Rate limiting is the process of controlling the number of requests a client can make to an API within a specific timeframe. It prevents resource exhaustion, mitigates DoS attacks, and ensures fair usage across all clients.

๐Ÿ“ก
Recommended

๐Ÿ“ก Monitor your rate limits in real-time

Better Stack provides detailed monitoring and alerting for your API endpoints, letting you know before your rate limits cause an outage.

Try Better Stack Free โ†’

Common Rate Limiting Algorithms

1. Fixed Window

The simplest approach. A counter is reset at the start of every window (e.g., 1,000 requests per hour).

Pros: Easy to implement.

Cons: The "burst" problem. A client can send 1,000 requests at the end of window A and 1,000 at the start of window B, effectively doubling the rate for a short period.

2. Sliding Window Log

Tracks every request timestamp in a log. When a new request comes in, it filters out timestamps older than the current window.

Pros: Extremely accurate.

Cons: High memory overhead to store every request timestamp.

3. Token Bucket

Tokens are added to a bucket at a fixed rate. Each request consumes a token. If the bucket is empty, the request is rate-limited.

Pros: Allows for controlled bursts while maintaining a long-term average rate.

Cons: Slightly more complex to implement than fixed windows.

4. Leaky Bucket

Requests enter a bucket and are processed (leak) at a constant, steady rate. If the bucket overflows, requests are dropped.

Pros: Smoothes out traffic spikes completely.

Cons: Can add latency to requests even when the system is under-utilized.

Implementation Example: Token Bucket in TypeScript

class TokenBucket {
  private tokens: number;
  private lastRefill: number;
  private readonly capacity: number;
  private readonly refillRate: number; // tokens per ms

  constructor(capacity: number, refillRate: number) {
    this.capacity = capacity;
    this.refillRate = refillRate;
    this.tokens = capacity;
    this.lastRefill = Date.now();
  }

  refill() {
    const now = Date.now();
    const delta = now - this.lastRefill;
    this.tokens = Math.min(this.capacity, this.tokens + delta * this.refillRate);
    this.lastRefill = now;
  }

  async take(): Promise<boolean> {
    this.refill();
    if (this.tokens >= 1) {
      this.tokens -= 1;
      return true;
    }
    return false;
  }
}

// Usage: 10 requests burst, refills at 1 request per second
const limiter = new TokenBucket(10, 1 / 1000);
const allowed = await limiter.take();
if (!allowed) {
  // Return 429 Too Many Requests
}

Best Practices for Rate Limiting

Stop guessing your API health

Combine your rate limiting strategy with proactive monitoring. Know exactly when your users are hitting limits and when your system is under stress.

Visit API Status Check โ†’

Related guides:

๐ŸŒ Can't Access API Stability?

If API Stability is working for others but not for you, it might be an ISP or regional issue. A VPN can help bypass network-level blocks and routing problems.

๐Ÿ”’

Troubleshoot with a VPN

Connect from a different region to test if the issue is local to your network. Also protects your connection on public Wi-Fi.

Try NordVPN โ€” 30-Day Money-Back Guarantee
๐Ÿ”‘

Secure Your API Stability Account

Service outages are a common time for phishing attacks. Use a password manager to keep unique, strong passwords for every account.

Try NordPass โ€” Free Password Manager
Quick ISP test: Try accessing API Stability on mobile data (Wi-Fi off). If it works, the issue is with your ISP or local network.

๐Ÿ›  Tools We Use & Recommend

Tested across our own infrastructure monitoring 200+ APIs daily

Better StackBest for API Teams

Uptime Monitoring & Incident Management

Used by 100,000+ websites

Monitors your APIs every 30 seconds. Instant alerts via Slack, email, SMS, and phone calls when something goes down.

โ€œWe use Better Stack to monitor every API on this site. It caught 23 outages last month before users reported them.โ€

Free tier ยท Paid from $24/moStart Free Monitoring
1PasswordBest for Credential Security

Secrets Management & Developer Security

Trusted by 150,000+ businesses

Manage API keys, database passwords, and service tokens with CLI integration and automatic rotation.

โ€œAfter covering dozens of outages caused by leaked credentials, we recommend every team use a secrets manager.โ€

SEMrushBest for SEO

SEO & Site Performance Monitoring

Used by 10M+ marketers

Track your site health, uptime, search rankings, and competitor movements from one dashboard.

โ€œWe use SEMrush to track how our API status pages rank and catch site health issues early.โ€

From $129.95/moTry SEMrush Free
View full comparison & more tools โ†’Affiliate links โ€” we earn a commission at no extra cost to you

Alert Pro

14-day free trial

Stop checking โ€” get alerted instantly

Next time API Rate Limiting goes down, you'll know in under 60 seconds โ€” not when your users start complaining.

  • Email alerts for API Rate Limiting + 9 more APIs
  • $0 due today for trial
  • Cancel anytime โ€” $9/mo after trial